Some features about Java threading Start-up interrupts and how to exit

Source: Internet
Author: User

or from a question.

public class Testthread {public static void main (string[] args) {//Test1thread T1 = new Thread () {public void run () {try {in t i = 0;while (i++<100000000) {}system.out.println ("A1");} catch (Exception e) {System.out.println ("B1");}}; T1.start (); T1.interrupt ();//Test2thread t2 = new Thread () {public void run () {try {thread.sleep (5000); System.out.println ("A2");} catch (Exception e) {System.out.println ("B2");}}; T2.start (); T2.interrupt ();//Test3thread t3 = new Thread () {public void run () {try {this.wait (50000); System.out.println ("A3");} catch (Exception e) {System.out.println ("B3");}}; T3.start (); T3.interrupt ();//Test1thread T4 = new Thread () {public void run () {try {synchronized (this) {this.wait (50000);} System.out.println ("A4");} catch (Exception e) {System.out.println ("B4");}}; T4.start (); T4.interrupt ();//Test5try {T4.start (); System.out.println ("A5");} catch (Exception e) {System.out.println ("B5");}}}
After the actual operation, we found that the result is

B3
B5
B4
B2
A1
Why is that? In fact, the API documentation is described in detail as follows:

If a thread is blocked in a call to a Object class wait() , wait(long) or wait(long, int) method, or a class,, join() join(long) join(long, int) , sleep(long) or sleep(long, int) method, its break state will be cleared and it will also receive one InterruptedException .

So after interrupt executes, the corresponding thread discards the sleep operation and throws an exception. So this will output b2,b3.


With regard to B4, after the thread enters the this.wait segment code, the object lock is freed due to the wait attribute. Then until interrupt executes, the object lock is acquired before the wait is discarded and an exception is thrown

With regard to B5, the start method of the same thread object can only be called once, and when the start is started it will be thrown IllegalThreadStateException , and if it has ended, it is possible that the object has been recycled, so start is more unlikely

About A1, it involves what changed after interrupt execution. In fact, after the thread interrupt, it just changes the interrupt state of the threads, and if the thread does not detect the state, the thread will continue to execute. Until the internal task is complete. When we print A1 at the same time, I will find that I have reached the exit condition when the value is 100000001. So how do I get the interrupt thread to exit, by doing the following

Thread t1 = new Thread () {public void run () {try {int i = 0;while (! Thread.CurrentThread (). isinterrupted ()) {while (i++<100000000) {}}system.out.println ("A1" +i);} catch (Exception e) {System.out.println ("B1");}}; T1.start (); T1.interrupt ();
Conclusion:

When the thread is currently in the running state, if the isinterrupted flag is not judged, nothing actually happens

When a thread is in a blocking state (sleep wait join), execution interrupt exits the blocking state prematurely because of an exception thrown

When a sync lock is present, wait will release the sync lock, and interrupt will not cause an exception if the sync lock is not taken

If you want to know more, you can refer to

Thread Exit related

Some features about Java threading Start-up interrupts and how to exit

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.