I used to think that the "interrupt" of a thread is to stop the thread.
If you think so, you are not getting started with multithreaded programming.
In Java, thread interruption (Interrupt) only changes the thread interruption status.
The result is uncertain, and sometimes it is the only means to continue the execution of the stopped thread.
Instead, it is a means of stopping the thread.
For a thread that executes general logic, if it calls its interrupt () method, there is no
For example, thread a is executing:
While (condition) x ++;
If other threads call A. Interrupt (); then the thread running on Object A is not affected. If
Test the interrupt status of thread a in other threads. It has changed, but does not stop the thread.
When the interrupt () method is called on a thread object, the wait, join, and sleep methods are actually affected. Of course, these three methods
Methods include their overload methods.
Note: [the above three methods will throw interruptedexception],
After a thread calls interrupt (), it will not throw an interruptedexception exception, so you can see
Interrupt () does not throw this exception, So I mentioned that if thread a is executing while (condition) x ++;
You call a. Interrupt (); then the thread continues to run normally.
However, if a thread is called interrupt (), its status is interrupted.
The thread of wait, join, and sleep changes the running result of the thread.
(1) sleep method and interrupt method
If thread a is like the following, the thread's sleep () method is used to pause.
- Thread. Sleep (10000 );
If you want to cancel the waiting status, you can call
- A. Interrupt (); // A is an instance of thread.
When the thread in sleep is called interrupt (), it will discard the pause state and throw interruptedexception. The exception is thrown, Which is thread.
When you execute interrupt (), you do not need to get the lock of the thread instance. Any thread can call other threads interrupt () at any time ().
Add a code with the same effect as thread. Sleep:
Public static void amethod (long x) throws interruptedexcetion {
If (X! = 0 ){
Object o = new object ();
Synchronized (o ){
O. Wait (X );
}
}
}
(2) wait and interrupt Methods
For the thread waiting for wake-up by Y/notifyall in wait, the thread has actually "paused" because
It is in the lounge of an object. If its interrupt status is changed, it will throw
Interruptedexception exception.
However, this interruptedexception exception is not thrown by the thread, but by the wait method, that is, inside the wait method of the object
It constantly checks the status of the thread resting on this object. if it finds that the status of the thread is set to interrupted, it will throw
Interruptedexception, which means that this thread can no longer wait, and its meaning is equivalent to awakening it.
After thread B executes the following statement, thread a will throw an interruptedexception exception, as in sleep.
- A. Interrupt ();
However, be careful when locking. when a thread enters the waiting area, the lock will be lifted. When interrupt () is called for the waiting thread (note that the waiting thread calls its own interrupt ()), the system will first obtain the lock again and then throw an exception. an exception cannot be thrown before the lock is obtained.
The only difference here is that the thread awakened by notify/all will continue to execute the statements below wait, while
The thread is interrupted, and the control is handed over to the catch statement. Some normal logic should be put into the catch to run.
But sometimes this is the only method. For example, if a thread a waits for awakening in the wait of a certain object B, other threads Must
Only when the monitoring lock of object B is obtained can B. Y () [all] be called. Otherwise, thread a cannot be awakened, but any thread can
Call a. Interrupt (); unconditionally to achieve this goal. You only need to put the wake-up logic in catch.
Like every y/All, it is still necessary to get the monitoring lock of object B to continue to execute the condition of thread.
(3) join and interrupt Methods
Interrupt () can be used to cancel a thread waiting for other threads to end with join. because join () does not need to be locked, like sleep (), it will jump to the Catch Block immediately.
Note that the interrupt () method must be called by a blocked thread. for example, thread a is called as thread t. join (). then a will wait for t to execute T. the code after join, a will block. When called in thread B as the. Interrupt () method, thread a throws an interruptedexception exception.
(4) The interrupt method only changes the interrupt status.
Interrupt () does not interrupt a running thread. In fact, this method throws an interrupt signal when the thread is blocked, so that the thread can exit the blocking state. More specifically, if the thread is. wait, thread. join and thread. sleep is blocked by one of the three methods. After interrupt (), it receives an interrupt exception (interruptedexception) to terminate the blocked state early.
If the thread is not blocked, calling interrupt () will not work; otherwise, the thread will get an exception (the thread must be prepared to handle this situation in advance) and then escape from the blocking state.
When thread a executes sleep, wait, and join, thread B calls the interrupt method of A. Indeed, at this time, thread a throws an interruptedexception exception. but this is actually in sleep, wait, join, these methods will constantly check the value of the interrupt status, and they throw the interruptedexception.
If thread a is executing some specified operations, such as assignment, for, while, if, or call method, it will not check the interrupt status, so thread a will not throw interruptedexception, and will always execute their own operations. when thread a is finally executed to wait (), sleep (), join (), it will immediately throw interruptedexception.
If the sleep (), wait (), join () methods are not called, or interruptedexception is thrown if the thread does not check the interrupt status.
For books that generally introduce the multithreading mode, they will introduce the following: when a thread is interrupted
Wait, sleep, and join will throw an exception.
System. out. println ("now interrupt"); <br/> thread. currentthread (). interrupt (); <br/> system. out. println ("now sleep"); <br/> thread. sleep (1000 );
Now interrupt <br/> now sleep <br/> exception in thread "Main" Java. lang. interruptedexception: sleep interrupted <br/> at java. lang. thread. sleep (native method) <br/> at namenode. namenode. main (namenode. java: 273)
Yes, but what does it mean? If you know that the thread is already in
Disconnected, why do we need to let it enter these three methods? Of course, this is sometimes required, but most of the time it is not.
The reason for doing this, so I mainly introduced how to call the interrupt () method on the thread that has already called the three methods to make it
The "pause" status of this method is restored. This restoration can contain two purposes:
1. [enable the thread to continue execution], that is, the logic after waking up in the catch statement, or the catch statement
Back to the normal logic. In short, it is from the wait, sleep, join pause State to live.
2. [You can directly stop the thread running]. Of course, nothing is handled in catch, or return, then the process will be completed.
The mission of the current thread, which can immediately "stop" in the "paused" state above ".