3 Methods of Java thread shutdown _java

Source: Internet
Author: User
Tags sleep volatile

Java thread shutdown, in general there are 3 kinds:

1. Use state bit, this simple, do not say more:

Copy Code code as follows:

public class Task extends Thread {

Private volatile Boolean flag= true;

public void Stoptask () {

Flag = false;

}

@Override

public void Run () {

while (flag) {

/* Do your no-block task * *

}

}

}




2. What happens when a thread waits for some event to be blocked? Of course, if the thread is blocked, it cannot check the shared variable and cannot stop. This can happen in many cases, such as calling Object.wait (), Thread.Sleep, and so on, and here are just a few examples. They are all likely to block threads permanently. Even if a timeout occurs, it is not feasible and inappropriate to wait until the timeout expires, so a mechanism is used to allow the thread to exit the blocked state earlier. This time you can use


Copy Code code as follows:

Thread.Interrupt ();

public class Blocktask extends Thread {

@Override

public void Run () {

try {

while (! Thread.interrupted ()) {

/* Do your block task*/

}

catch (Exception e) {

E.printstacktrace ();

}

}

}




But the code above may be a bit of a problem, perhaps using an example to make it clearer. How do you know that the code snippet is blocked? What exactly does the interrupt () function mean? First of all, the interrupted () method can only solve the blocking of running out of interruptedexception exception. Instead of shutting down the blocking thread, interrupt () is unblocking it. Here's an example of shutting down a thread block:


Copy Code code as follows:

public class Blocktask extends Thread {

@Override

public void Run () {

try {

Sleep (10000);

catch (Interruptedexception e) {

System.out.println ("If yout use interrupt you'll");

}


}

public static void Main (string[] args) throws Exception {

TODO auto-generated Method Stub

Blocktask task = new Blocktask ();

Task.start ();

Thread.Sleep (1000);

Task.interrupt ();


}

}




3. As stated above, interrupt () can only solve interruptedexception blocked threads, so how do you deal with some other IO blocking? This time Java will provide the appropriate way to close blocking. For example, the server may need to wait for a request, or a network application may have to wait for a remote host to respond, at which point the socket close () method can be used


Copy Code code as follows:

public class Sockettask extends Thread {

private volatile ServerSocket server;


public void Stoptask () {

try {

if (server!=null) {

Server.close ();

SYSTEM.OUT.PRINTLN ("Close task successed");

}

catch (IOException e) {

SYSTEM.OUT.PRINTLN ("Close task failded");

}

}

@Override

public void Run () {

try {

Server = new ServerSocket (3333);

catch (IOException e) {

E.printstacktrace ();

}

}

public static void Main (string[] args) throws Interruptedexception {


Sockettask task = new Sockettask ();

Task.start ();

Thread.Sleep (1000);

Task.stoptask ();

}

}

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.