2008-06-30

Decent pool worker thread loop

UPD as of April 2011. The code below is of course more or less okay, but Google's Guava has nice AbstractExecutionThreadService and couple of even more nicely elaborated worker primitives. I guess we don't need to implement that stuff by hand anymore...

    public void run() {
       boolean interrupted = false;
       
       while (!interrupted && !Thread.currentThread().isInterrupted()) {
           try {
               runInternal(); //  do your stuff here
           } catch (Throwable t) {
               //  recommended to use your favorite logging system to complain
               interrupted |= t instanceof InterruptedException;   //  recommended to unwrap to root cause here
           }
       }
   }

No comments: