简体   繁体   English

中断,关闭方法和ExecutorService

[英]Interrupts, shutdown method and ExecutorService

Directly from this java tutorial: 直接来自这个java教程:

To support immediate shutdown, tasks should handle interrupts correctly 为了支持立即关闭,任务应该正确处理中断

Would anybody be able to articulate the point? 有人能说清楚这一点吗? What's the relation between tasks and interrupts and the shutdown method? 任务和中断与关机方法之间的关系是什么? Does it only mean if there is a task waiting for something it must throw InterruptedException ? 只是意味着如果有任务等待它必须抛出InterruptedException吗?

Thanks in advance. 提前致谢。

Imagine you ran this task with ExecutorService 想象一下,您使用ExecutorService运行此任务

class Task implements Runnable() {
   public void run() {
       while(true) {
       }
   }
}

now you want to shutdown the ExecutorService with shutdownNow(). 现在要使用shutdownNow()关闭ExecutorService。 ExecutorService will call interrupt() on the Thread running the task, but since the task does not respond to interrupts it will continue running and ExecutorService will never shutdown. ExecutorService将在运行任务的线程上调用interrupt(),但由于任务不响应中断,因此它将继续运行,ExecutorService将永远不会关闭。

The following change will make this task interruptible 以下更改将使此任务可中断

       while(!Thread.interrupted()) {
       }

and will allow ExecutorService to shutdown 并将允许ExecutorService关闭

What it simply means is that you timely keep checking the interrupt status in your code. 它的简单含义是您及时检查代码中的interrupt状态。 This can be checked using Thread.currentThread().isInterrupted() . 这可以使用Thread.currentThread().isInterrupted()来检查。

In Java unlike other Unix calls, you only have interrupt to communicate with other thread. 在Java中,与其他Unix调用不同,您只有中断与其他线程通信。 Hence you cannot send signals etc. If you wish to notify another thread to stop whatever it is doing as it might not be required then one of the legitimate way for communication is via interrupting that thread. 因此,您无法发送信号等。如果您希望通知另一个线程停止正在执行的操作,因为它可能不需要,那么通信的合法方式之一就是通过中断该线程。

What happens when you interrupt a thread? 中断线程会发生什么?

Every thread has a boolean internally which is false by default. 每个线程在内部都有一个布尔值,默认情况下为false When you interrupt a thread, this boolean is set as true. 当您中断线程时,此布尔值设置为true。 So hence the documentation asks you to keep checking timely the status of the boolean. 因此,文档要求您及时检查布尔值的状态。 If true , then the convention is to stop doing any task undertaken and to come out of loop or whatever. 如果true ,那么惯例是停止执行任何任务并退出循环或其他任何事情。

Though interrupting a thread is just a notification. 虽然interrupting一个线程只是一个通知。 But by convention in Java, it is taken as a request for shutdown. 但按照Java中的惯例,它被视为关闭请求。 You can treat this interrupt status as anything you want (ex: requesting to send mail's, take db dump etc) but it is not recommended as most of the internal java libraries are built treating it as request for shutdown. 您可以将此interrupt状态视为您想要的任何内容(例如:请求发送邮件,进行数据库转储等)但不建议这样做,因为大多数内部Java库都是根据请求关闭而构建的。

What is InterruptedException for?

In java.util.concurrent package, using some class if a thread gets blocked (in await or getLock or in blockign queue etc) and you interrupt that thread. java.util.concurrent包中,如果线程被阻塞(在awaitgetLock或blockign队列中等),则使用某个类并中断该线程。 The gets unblocked the library throws InterruptedException to notify you that the thread was blocked. 取消阻止库被抛出InterruptedException以通知您线程被阻止。

References: 参考文献:

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

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