Categories
Uncategorized

Cooperation in order to better interrupt thread

Chatted about the interruption, you may be most familiar example is the thread to sleep. Here is a thread to sleep demo, in this example, when we call sleep method, which will throw a need to capture interrupt exception, here catch the exception and return directly.

        for (int i = 0; i < somethings.size(); i++) {
            // 休眠 4 s
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                // 抛出中断异常
                return;
            }
            // 输出
            System.out.println(somethings.get(i));
        }

In addition InterruptedException interrupt exception, in addition to three interrupts related methods, three methods are associated with the thread.

  • thread#interrupt
  • Thread#interrupted
  • thread#isInterrupted

interrupt thread method for interrupting, but does not mean that this method will be able to direct the thread to stop.

Use interrupt interrupt thread below, where we expect immediate stop child thread interrupt output. But when the main thread calls child thread interrupt method, and the child thread has not been terminated, continues to print digital.

        Runnable interruptedTask=new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i 

The official explanation for the reference Java interrupted:

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate

Interrupted just shows that this thread can be stopped, but the thread depends entirely on whether to stop the threads themselves. Only the thread cooperate with each other in order to better stop the thread.

Each thread contains an internal flag to indicate interrupt status. interrupt method calling thread will set the status bit for Thread # sleep obstruction and other methods, will throw InterruptedException, and clears the interrupt flag.

We can use the thread # isInterrupted or Thread # interrupted checking interrupt status. Note, however, there are some difference between the two methods, Thread # interrupted as a static class method detecting clears the interrupt flag after an interruption to.

The above method as long as we add interrupt status can be judged to stop the thread.

        Runnable interruptedTask=new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i 

Interrupt best practices

Do not arbitrarily "eaten" interrupt exception

Since interrupt exception is a checked exception, we need to have to handle the exception. If we can guarantee that the exception does not affect the application, we can simply "eat" the exception. In other cases we need to correctly handle the exception.

The easiest way is to not handle the exception, directly thrown up interrupt exception, so that upper caller decide what to do.

但是有些情况下,却不适合上面的做法,这种情况下我们需要在 catch 中处理中断。如果实在不知道如何处理,那就是记录该异常,并使用日志方式输出。


Interruption will not stop blocking IO

We said above, some methods such as blocking Thread # sleep, interrupt exception will be thrown. But for Socket etc. blocking IO calls, and does not throw this exception. That will not stop calling interrupt blocking IO's.

This is because when the call Thread # sleep such as blocking method, Java thread state will transition from RUNNABLE to TIMED_WAITING or WATTING. And when the thread blocks read IO, Java threads actual status but still RUNNABLE. If you are still confused state of this thread, you can read the next article Interviewer: I say blocking I / O model will make the thread sleep, why Java thread state is RUNNABLE? In-depth understanding about thread state.

This article first appeared in studyidea.cn

I welcome the attention of the public number: Interpreter program, and getting daily dry push. If you are interested in my topic content, you can focus on my blog: studyidea.cn

Leave a Reply