How to terminate a thread in JAVA

Source: Internet
Author: User

In Java multi-thread programming, java. lang. the Thread type includes the start (), stop (), stop (Throwable) and suspend (), destroy () and resume () methods of some columns (). Through these methods, we can perform convenient operations on the thread, but among these methods, only the start () method is retained.

In a Sun article titled Why are Thread. stop, Thread. suspend and Thread. resume Deprecated? The reason for dropping these methods is described in detail.

To terminate a thread, you can use the following methods:
1. The thread ends naturally after the run () method is executed. (This method is best)

2. End the thread by polling and sharing the flag. For example, while (flag) {}, the initial value of flag is set to true. When the end is required, set the flag value to false. (This method is not good either, because if the while (flag) {} method is blocked, the flag will be invalid)

Copy codeThe Code is as follows: public class SomeThread implements Runnable {

Private volatile boolean stop = false;

Public void terminate (){

Stop = ture;

}

Public void run (){

While (stop ){

//... Some statements

}

}

}

If the thread enters the Not Runnable state because of running sleep () or wait (), it will Not work if the flag is used for wait,

Public final void wait (long timeout)
Throws InterruptedException this method causes the current thread (called T) to place itself in the waiting set of the object, and then discard all the synchronization requirements on the object. That is, the current thread changes to the waiting state.

Standard use of wait ()

Synchronized (obj ){

While (<condition not met> ){

Obj. wait ();

}

Process of meeting conditions

}

If you want to stop it, you can use the third method:

3. When interrupt () is used, the program will throw the InterruptedException exception, thus leaving the execution thread away from the run () method,

For example:

Copy codeThe Code is as follows:

Public class SomeThread {

Public static void main (String [] args)

{

Thread thread = new Thread (new Runnable (){

Public void run (){

While (! Thread. interrupted ()){
// Process the work to be processed
Try {

System. out. println ("go to sleep ");

Thread. sleep (1000 );
} Catch (InterruptedException e ){
E. printStackTrace ();

System. out. println ("I am interrupted! ");
}

});

Thread. start ();

Thread. interrupt ();

}

}

The execution result is:

Go to sleep

I am interrupted!

Related Article

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.