简体   繁体   English

Future.cancel(true)下面发生了什么

[英]What is happening underneath the Future.cancel(true)

Suppose I have a Runnable instance: 假设我有一个Runnable实例:

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提交上述任务:

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

Now, I can cancel the task by task.cancel(true); 现在,我可以通过task.cancel(true);取消任务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() . 据我了解, task.cancel(true)将中断正在运行该任务的工作线程,例如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? 我的问题是:如果MyTask Runnable已开始运行, future.cancel(true)如何实际上停止我在run()代码并继续执行其余代码? 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. 我的意思是我不明白如何仅通过将中断标志设置为true来取消run()中的代码。

Future.cancel does not guarantee that your worker code will stop executing. Future.cancel不保证您的工作程序代码将停止执​​行。 What it does is set the interrupted flag and cause any blocking JDK calls to throw an InterruptedException. 它的作用是设置中断标志,并导致任何阻塞的JDK调用都抛出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. 否则,您可以选择吞下InterruptedException而不理会iterrupted标志,在这种情况下,cancel机制不会执行任何操作,只能将canceled标志设置为true。

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

There is a method called isInterrupted(), this tells the running code in the thread that it is interrupted by returning a true/false. 有一个称为isInterrupted()的方法,该方法通过返回true / false来告知线程中正在运行的代码。

This is usually checked by the methods like wait, sleep which you might invoke in the thread. 这通常由您可能在线程中调用的诸如wait,sleep之类的方法检查。 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. 但是,如果您不使用这些方法,则必须手动检查此方法[isInterrupted()],以确定是否有人中断了您的线程。

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...) 如果有任何机会,您可以决定执行什么操作(例如,让我们说:抛出InterruptedException或从循环中断等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM