Java Multi-thread interrupt () method is associated with sleep (), join (), wait ()

Source: Internet
Author: User

Java Multi-thread interrupt () method is associated with sleep (), join (), wait ()

About Java multithreading knowledge can see "Thinking in Java" in the multithreaded part and "Java Network Programming" 5th chapter of the Multithreading section below is the reference <<java multithreaded mode >> content.
1. Sleep () & Interrupt ()
Thread A is using sleep () pause: Thread.Sleep (100000);
If you want to cancel his wait state, you can call the Chengri line (for example, here is B)
A.interrupt ();
Let thread a give up the sleep operation, where a is a thread instance that threads a corresponds to
When you perform interrupt (), you do not need to get a lock on the thread instance. Any thread at any time can invoke another thread interrupt (). When the thread in sleep is called interrupt (), The paused state is discarded. and throw the interruptedexception. The exception is thrown, which is a thread.
2. Wait () & Interrupt ()
Thread A called wait () to enter the waiting state, or it can be canceled with interrupt ().
But it's time to be careful with the locking problem. The thread is in the waiting area, and the lock is lifted, and when interrupt () is invoked on the waiting thread (note that the waiting thread calls its own interrupt ()), the lock is retrieved before the exception is thrown. Before acquiring a lock, is unable to throw an exception.
3. Join () & Interrupt ()
When a thread waits for the other thread to end with a join (), the same can be done by using interrupt (). Because calling join () does not require a lock, it jumps immediately into the catch block, as with sleep (). Note that Tiaogan uses the interrupt () method, which must be a blocked thread to invoke its own interrupt method. such as thread A is invoked for threading T.join (). Then a will wait t after executing the code after the T.join, when called in thread B to a.interrupt () method, the Interruptedexception is thrown
4. Interrupt () just changed the state of interruption .
Interrupt () does not break a running thread. What this method actually does is to throw an interrupt signal when the thread is blocked, so that the threads can exit the blocked state. More precisely, if a thread is blocked by one of the three methods of Object.wait, Thread.Join and Thread.Sleep, it will receive an interrupt exception (Interruptedexception) to prematurely terminate the blocked state.
If the thread is not blocked, then the call to interrupt () will not work, otherwise the thread will get an exception (the thread must be prepared to handle this condition), and then flee the blocking state.
When thread a executes sleep,wait,join, thread B calls the interrupt method of a, and indeed a Interruptedexception exception is thrown at this time. But it's actually in sleep,wait, Join these methods will constantly check the value of the interrupt state, and throw the interruptedexception yourself.
If thread A is performing some of the specified actions, such as assignment, For,while,if, calling methods, and so on, it does not check the interrupt state, so thread A does not throw interruptedexception, and it always performs its own operation. When thread a finally executes to wait (), Sleep (), join (), will immediately throw interruptedexception.
If you do not call sleep (), wait (), join () These methods, or do not have an online Chengri to check the interrupt state themselves throw interruptedexception words, that interruptedexception will not be thrown out.
By the way, 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);
}
}
}

Turn from: http://blog.sina.com.cn/s/blog_4cd5d2bb0100sz1x.html

Java Multi-Thread interrupt (): http://blog.csdn.net/heyutao007/article/details/6254555

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.