Java-two ways to stop multithreading

Source: Internet
Author: User

How does a thread stop?
The Stop method is obsolete and looks at the description found to have other solutions.
Thread End: The Run method ends when the thread task code is executed.
How does the Run method end?
The Run method usually defines loops, as long as the loop is controlled.

//demonstrates stopping a thread. classDemoImplementsRunnable {Private BooleanFlag =true;  Public voidrun () { while(flag) {System.out.println (Thread.CurrentThread (). GetName ()+ "----->"); }    }    //The method of modifying the tag.      Public voidChangeflag () {flag=false; }}classMstopthreaddemo { Public Static voidMain (string[] args) {Demo d=NewDemo (); Thread T1=NewThread (d); Thread T2=NewThread (d);        T1.start ();        T2.start (); intx = 0;  while(true) {            if(++x = = 50) {D.changeflag ();//change the markup of the thread task code so that other threads also end.                  Break;//jump out of the loop, the main thread can end. } System.out.println ("Main-------->" +x); } System.out.println ("Over"); }}

Note: If the thread is frozen in the task, can it still judge the tag? No!
What to do? By reviewing the description of the Stop method, it is found to provide a workaround:
If the target thread waits for a long time, you should use the interrupt method to interrupt the wait
The so-called interrupt is not a stop thread.
The function of the interrupt is to clear the frozen state of the thread, allowing the thread to return to the running state (which allows the thread to re-qualify for CPU execution).
Because it is mandatory, there will be abnormal interruptedexception that can catch exceptions in the catch.
In exception handling, change the tag to end the loop and let the Run method end.

//demonstrates stopping a thread. classDemoImplementsRunnable {Private BooleanFlag =true;  Public synchronized voidRun () {//Don't do this. Add the synchronized to the Run method this has become a single-threaded one. This is done deliberately in order to test because wait () must have a owning lock         while(flag) {Try{wait ();//T1 T2//If you use Notify () must be in the same lock as wait (), then you can only use interrupt () If you are not in the same lock .            }            Catch(interruptedexception e) {System.out.println (Thread.CurrentThread (). toString ()+"....."+e.tostring ());  Changeflag (); Since the interrupt () method is mandatory to restore a thread to a running state, it is mandatory to throw an exception so that he is marked for modification when the exception is captured} System.out.println (Thread.currentthr EAD (). GetName ()+ "----->"); }    }    //The method of modifying the tag.      Public voidChangeflag () {flag=false; }}classMStopThreadDemo2 { Public Static voidMain (string[] args) {Demo d=NewDemo (); Thread T1=NewThread (d); Thread T2=NewThread (d); T1.start ();
T2.start ();
intx = 0; while(true){ if(++x = = 50){ //D.changeflag ();//change the markup of the thread task code so that other threads also end. //clears the interrupt state of the T1 thread object and forces it to revert to the running state. T1.interrupt (); //clears the interrupt state of the T2 thread object and forces it to revert to the running state. T2.interrupt (); Break;//jump out of the loop, the main thread can end. } System.out.println ("Main-------->" +x); } System.out.println ("Over"); }}

Java-two ways to stop multithreading

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.