Multi-Thread Programming Practice (4)

Source: Internet
Author: User
[Thread interruption]

To put it bluntly, at least half believe that the thread's "interruption" 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]. Remember this sentence and I will repeat it below.
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. 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 an exception.
This interruptedexception exception is not thrown by the thread, but is 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.

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.

2. For the thread in sleep, if you call thread. Sleep (one year); now you regret it and want to make it early
When you wake up, calling the interrupt () method is the only way to change its interrupt status so that it can be controlled from sleep.
The permission is transferred to the catch statement that handles exceptions, and then converted from the catch process to the normal logic. Similarly, in the join
You can also process it like this.

for books that generally introduce multithreading, they will introduce this as follows: When a thread is interrupted, an exception will be thrown when the
wait, sleep, and join method is entered.
yes, there is no error at all, but what does it mean? If you know that the thread is in the
disconnected state, why do you need to let it enter these three methods? Of course, this is sometimes necessary, but most of the time there is no reason for this
, so I have mainly introduced calling interrupt () on the thread that has already called these three methods () method to restore it from the "pause" Status of the
methods. this recovery can contain two purposes:
1. [The thread can continue to execute], that is, the logic after the catch statement is executed, or the catch statement
returns to the normal logic. in short, it is from the wait, sleep, join pause State to live.
II. [thread running can be stopped directly]. Of course, nothing is handled in catch, or return, then the mission of the current thread is completed.
, can immediately stop in the above "paused" status ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.