[concurrency-interest] BackPort: Threads not under control

Joe Bowbeer joe.bowbeer at gmail.com
Wed May 24 17:47:15 EDT 2006


On 5/24/06, Srini Pillai <SPILLAI at mail.nysed.gov> wrote:
> Is there a different way to trap the interrupt?

  if (Thread.interrupted())
      throw new InterruptedException();


On 5/24/06, Srini Pillai <SPILLAI at mail.nysed.gov> wrote:
> Joe,
>
> Thanks for the explaination. I found out that the task I was running
> (which is an I/O operation, a socket connection) is not interrupted
> properly. To respond to the interrupt call I had to place a dummy
> Thread.sleep(1) in the loop that is performing the I/O to trap the
> Interrupt. This throws the exception and stops the thread but I don't
> like the idea of having the Thread.sleep() there... Is there a different
> way to trap the interrupt ?
>
> Thanks,
> Srini
>
> >>> "Joe Bowbeer" <joe.bowbeer at gmail.com> 5/24/2006 3:17 PM >>>
> Srini,
>
> Strictly speaking, you want to call isTerminated instead of isShutdown
> at the end, but I suspect it will also return true.
>
> I suspect the source of your trouble is that the executing tasks are
> not responding to interrupts.
>
> invokeAll will cancel tardy tasks via task.cancel(true).  This will
> set the task's state to cancelled and will also interrupt the task's
> worker thread.  But the task's Callable/Runnable has to respond to the
> interrupt (or at least the cancellation) in order for it to actually
> stop.  Otherwise, if it ignores the interrupt, say, then it will run
> to completion regardless.  The additional cancel(true) handling in
> your code is probably wasted effort...
>
>
> How to tell if a Callable is responsive to interrupts?
>
> Check out the way the Callable<Integer> handles interrupt here:
>
> http://java.sun.com/docs/books/tutorial/essential/threads/pool.html
>
>     public Integer call() {
>         for (int i = 0; i <= 100; i += 20) {
>             //Perform some work...
>             System.out.format("Worker number: %d, percent complete:
> %d%n",
>                 workerNumber, i);
>             try {
>                 Thread.sleep((int)(Math.random() * 1000));
>             } catch (InterruptedException e) {}
>         }
>         return(workerNumber);
>     }
>
> This is a *good* example of how *not* to respond to an interrupt :-(
>
>
> To be responsive to interrupt, the task should be written as:
>
>     public Integer call() {
>         try {
>             for (int i = 0; i <= 100; i += 20) {
>                 //Perform some work...
>                 System.out.format("Worker number: %d, percent complete:
> %d%n",
>                     workerNumber, i);
>                 Thread.sleep((int)(Math.random() * 1000));
>             }
>         } catch (InterruptedException e) {}
>         return(workerNumber);
>     }
>
>
> OR I prefer to simply declare the InterruptedException:
>
>     public Integer call() throws InterruptedException {
>         for (int i = 0; i <= 100; i += 20) {
>             //Perform some work...
>             System.out.format("Worker number: %d, percent complete:
> %d%n",
>                 workerNumber, i);
>             Thread.sleep((int)(Math.random() * 1000));
>         }
>         return(workerNumber);
>     }
>
> --Joe
>



More information about the Concurrency-interest mailing list