Java------stop thread

Source: Internet
Author: User

Stopping a thread

If there is a stop method in the thread class that can be used to stop the thread, but it is obsolete, how do you stop the thread?

The thread runs in fact the code in the Run method, so as long as the run method is stopped, the thread stops.

The general thread of the operation and the cycle of the combination, it is good to do. If you modify the tag, the Run method ends.

Class Stopthread implements Runnable {private Boolean flag = True;public void Run () {while (flag) {System.out.println (thre Ad.currentthread (). GetName () + "-----");}} public void Changeflag () {flag = false;}} public class Stop {public static void main (string[] args) {int num = 0; Stopthread s = new Stopthread (); thread T1 = new thread (s); Thread t2 = new Thread (s); T1.start (); T2.start (); while (true) {if (num++ = =) {S.changeflag (); System.out.println ("main");}}}

However, this method does not apply to a frozen state.

The tag is not read when the thread is in a frozen state, and of course it does not end.

When there is no specified way to get the frozen thread back to the running state, the freeze needs to be purged, forcing the thread to return to the running state so that the token can be changed so that the thread ends.

The thread freezes in sync and how does it end? Interrupt

Api:

Interrupt

public void Interrupt ()

The thread is disconnected.

If the current thread does not interrupt itself (which in any case is allowed), then the thread's checkAccess method is called, which may throw SecurityException.

If the thread is calling the wait (),Wait (long) , or wait (long, int) method of the Object class, or the class's join (), When a join (long),join (long, int),Sleep (long) , or sleep (long, int) method is blocked, its break state is cleared. It will also receive a interruptedexception.

If the thread is blocked in an I/O operation on an interruptible channel , the channel is closed, the interrupt state of the thread is set, and the thread receives a closedbyinterruptexception.

If the thread is blocked in a Selector , the interrupt state of the thread is set, it is returned immediately from the selection operation, and may have a value other than 0, as if the wakeup method of the selector was called.

If none of the previous conditions are saved, the interrupt state of the thread is set.

Breaking a thread that is not active does not require any action.

Thrown:

SecurityException -If the thread cannot be modified by the current thread

Class Stopthread implements Runnable {private Boolean flag = True;public synchronized void run () {while (flag) {try {wait ( );} catch (Interruptedexception e) {//TODO auto-generated catch block//e.printstacktrace (); System.out.println ("Exception");} System.out.println (Thread.CurrentThread (). GetName () + "-----");}} public void Changeflag () {flag = false;}} public class Stop {public static void main (string[] args) {int num = 0; Stopthread s = new Stopthread (); thread T1 = new thread (s); Thread t2 = new Thread (s); T1.start (); T2.start (); while (true) {if (num++ = =) {//S.changeflag (); T1.interrupt (); t2.inter Rupt (); break;} System.out.println ("main");}}}

By calling the interrupt method, the thread has been restored from a frozen state to a running state, but has only been run once.

Using interrupt throws an exception, and when the exception is thrown, the state is changed, allowing the thread to stop.


Class Stopthread implements Runnable {private Boolean flag = True;public synchronized void run () {while (flag) {try {wait ( );} catch (Interruptedexception e) {//TODO auto-generated catch block//e.printstacktrace (); System.out.println ("Exception"); flag = false;} System.out.println (Thread.CurrentThread (). GetName () + "-----");}} public void Changeflag () {flag = false;}} public class Stop {public static void main (string[] args) {int num = 0; Stopthread s = new Stopthread (); thread T1 = new thread (s); Thread t2 = new Thread (s); T1.start (); T2.start (); while (true) {if (num++ = =) {//S.changeflag (); T1.interrupt (); t2.inter Rupt (); break;} System.out.println ("main");}}}







Java------stop thread

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.