Java多线程技能(七)——停止线程

Java多线程技能(七)——停止线程,第1张

目录
  • 1.在沉睡中停止
  • 2.能停止的线程——暴力停止

1.在沉睡中停止

  线程在sleep()状态下停止线程。先用sleep()方法再用interrupt()停止。
新创建项目deadsleep,类MyThread.java代码如下:

public class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
        try {
            System.out.println("run begin");
            Thread.sleep(200000); //因为sleep()是静态方法,所以最好的调用方法就是 Thread.sleep()
            System.out.println("run end");
        } catch (InterruptedException e) {
            System.out.println("在沉睡中被停止!进入catch!"+this.isInterrupted());
            e.printStackTrace();
        }
    }
}

运行类Run.java代码如下:

public class Run {
    public static void main(String[] args) {
        try {
            MyThread thread = new MyThread();
            thread.start();
            Thread.sleep(200); //先调用sleep方法
            thread.interrupt(); //再调用interrupt方法
        } catch (InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
        System.out.println("end!");
    }
}

运行结果如下图所示:

  通过上面的例子我们知道,我们是先让MyThread对象线程进入到睡眠状态,然后我们再调用interrupt()方法去停止它。从结果看,如果在sleep状态下停止某一线程,就会使其进入catch语句,抛出InterruptedException 异常,并且清除停止状态值,使之变成false。

  接下来再看另外一个例子。先interrupt()停止再用sleep()方法。
新创建项目deadsleep_1,类MyThread.java代码如下:

public class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
        try {
            for(int i=0;i<100000;i++){
                System.out.println("i="+(i+1));
            }
            System.out.println("run begin");
            Thread.sleep(200000);
            System.out.println("run end");
        } catch (InterruptedException e) {
            System.out.println("先停止,再遇到了sleep!进入catch!");
            e.printStackTrace();
        }
    }
}

运行类Run.java代码如下:

public class Run {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        thread.interrupt(); //先运行interrupt()方法
        System.out.println("end!");
    }
}

运行结果如所示:
这是先执行了interrupt停止线程,所以开始就输出end

end!
i=1
i=2
i=3
i=4
i=5
i=6

下面图片显示是程序遇到了sleep()方法,然后提示异常
  从上面运行结果可知,当我们先执行interrupt()方法时,myThread线程会一直运行到sleep()方法时才进入catch语句中,不会立刻停止。

2.能停止的线程——暴力停止

  使用stop()方法停止线程则是非常暴力的。stop方法会强制停止一个正在运行的线程,无论此时线程是何种状态。
先创建项目useStop,类MyThread.java代码如下:

 public class MyThread extends Thread {
    private int i = 0;

    @Override
    public void run() {
        try {
            while (true) {
                i++;
                System.out.println("i=" + i);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行类Run.java代码如下:

public class Run {
    public static void main(String[] args) {
        try {
            MyThread thread = new MyThread();
            thread.start();
            Thread.sleep(8000); //休眠8秒钟
            thread.stop(); //再调用stop()方法
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行结果如下所示:

i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8

  根据代码及运行结果分析,在while循环中应该无限循环下去,但是让main线程暂停8秒后,main线程再调用了stop()方法,暴力停止了线程,所以打印停止。stop方法可以停止线程。


以上代码下载请点击该链接:https://github.com/Yarrow052/Java-package.git

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/877694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存