首页 > 代码库 > Java 线程停止、暂停和继续

Java 线程停止、暂停和继续

Thread 类中停止线程的方法有 stop(),暂停和继续线程的方法有 suspend() 和 resume()。然而这些方法已经被废弃了。

异常法停止线程

上代码:

public class Test {    public static void main(String[] args) {            MyThread thread = new MyThread();            thread.start();            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            thread.interrupt();    }}class MyThread extends Thread {    @Override    public void run(){        boolean flag = true;        while (flag) {            if (this.isInterrupted()) {                System.out.println("线程即将停止");                try {                    throw new InterruptedException();                } catch (InterruptedException e) {                    flag = false;                }            }        }        System.out.println("已经跳出循环,线程停止");    }}

打印输出:

线程即将停止已经跳出循环,线程停止

run 方法执行完,线程自然就结束了。

 

使用 return 停止线程

public class Test2 {    public static void main(String[] args) {        MyThread2 thread = new MyThread2();        thread.start();        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        thread.interrupt();    }}class MyThread2 extends Thread {    @Override    public void run() {        while (true) {            if (this.isInterrupted()) {                System.out.println("线程停止");                return;            }        }    }}

打印输出:

线程停止

 

线程暂停和继续

线程暂停、继续和停止,上代码:

public class Test {    public static void main(String[] args) {        MyThread thread = new MyThread();        thread.start();        try {            Thread.sleep(1);            thread.mySuspend();            Thread.sleep(1);            thread.myResume();            Thread.sleep(1);            thread.myStop();        } catch (Exception e) {            e.printStackTrace();        }    }}class MyThread extends Thread {    private int counts = 0;    private int status = 1;    private final int SUSPEND = -1;    private final int RUNNING = 1;    @Override    public void run() {        boolean flag = true;        while (flag) {            if (this.isInterrupted()) {                System.out.println(++counts + " - 线程即将停止");                try {                    throw new InterruptedException();                } catch (InterruptedException e) {                    flag = false;                }            } else if (status == SUSPEND) {                System.out.println(++counts + " - 线程暂停");            } else if (status == RUNNING) {                System.out.println(++counts + " - 线程仍在继续");            }        }        System.out.println(++counts + " - 已经跳出循环,线程停止");    }    public void mySuspend() {        status = SUSPEND;    }    synchronized public void myResume() {        status = RUNNING;        this.notifyAll();    }    public void myStop() {        this.interrupt();    }}

打印输出:

1 - 线程仍在继续2 - 线程暂停3 - 线程暂停...42 - 线程暂停43 - 线程暂停44 - 线程仍在继续45 - 线程仍在继续46 - 线程仍在继续...87 - 线程仍在继续88 - 线程仍在继续89 - 线程仍在继续90 - 线程仍在继续91 - 线程即将停止92 - 已经跳出循环,线程停止

 

Java 线程停止、暂停和继续