码迷,mamicode.com
首页 > 编程语言 > 详细

Java 线程停止、暂停和继续

时间:2016-09-20 18:08:43      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

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 线程停止、暂停和继续

标签:

原文地址:http://www.cnblogs.com/xmsx/p/5888661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!