3

Suppose I have a Runnable instance:

class MyTask implements Runnable {

  public void run() {
     //some heavy calculation which takes time
      Thread.sleep(5000)
     //rest code
     ...
  }

}

Then, I use ExecutorService to submit the above task:

ExecutorService service = Executors.newFixedThreadPool(3);
Future<?> task = service.submit(new MyTask());

Now, I can cancel the task by task.cancel(true);. What I have understood is that the task.cancel(true) will interrupt the working thread in which this task is running, like Thread.currentThread().interrupt(). But this only sets a flag to tell that the working thread is interrupted.

My question is: if MyTask Runnable has started running, how actually does future.cancel(true) stops my code in run() continuing executing the rest code? Is there a periodical checking for the working thread's interrupted flag underneath? I mean I don't understand how the code in run() can be canceled by only set the interrupted flag to true.

user842225
  • 5,445
  • 15
  • 69
  • 119
  • 2
    `Future.cancel` will call `Thread.interrupt()`. In general case, this call isn't guaranteed to stop thread from doing it's own business. This is for same reason `Thread.stop()` and other unsafe methods are deprecated (see Javadoc for explanation). It's up to you to organize your `Runnables` so they can detect interrupted status in timely manner. – Victor Sorokin Oct 22 '14 at 20:45

2 Answers2

1

Future.cancel does not guarantee that your worker code will stop executing. What it does is set the interrupted flag and cause any blocking JDK calls to throw an InterruptedException. Your worker code may choose to rethrow the interrupted exception and periodically check the interrupted flag, in which case the cancel mechanism will work. Otherwise you may choose to swallow InterruptedException and disregard the iterrupted flag, in which case the cancel mechanism will do nothing but set the cancelled flag to true.

See http://www.ibm.com/developerworks/library/j-jtp05236/

Maxaon3000
  • 1,109
  • 8
  • 15
0

There is a method called isInterrupted(), this tells the running code in the thread that it is interrupted by returning a true/false.

This is usually checked by the methods like wait, sleep which you might invoke in the thread. If however you do not use these methods, then you will have to manually check this method [ isInterrupted() ] to determine whether someone has interrupted your thread.

If by any chance you get a true, you can decide what action to perform (let us say for example: throw a InterruptedException or break from a loop, etc...)

MJSG
  • 1,035
  • 8
  • 12
  • @ MJSG , to be more clear to me, do you mean in those methods like sleep(), wait(),etc, underneath, they are calling isInterrupted() to check whether the current thread is interrupted? And if it is true, they just break, right? – user842225 Oct 22 '14 at 20:33
  • They throw an InterruptedException if they find they've been interrupted. – Will Hartung Oct 22 '14 at 20:35
  • 1
    @user842225 See http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html and http://stackoverflow.com/questions/3590000/what-does-java-lang-thread-interrupt-do – isnot2bad Oct 22 '14 at 21:03