美文网首页
停止线程(stop方法已经过时了)

停止线程(stop方法已经过时了)

作者: 东风谷123Liter | 来源:发表于2018-07-12 10:24 被阅读0次
  • 为什么stop被放弃了:因为stop里面有一些bug!!

    • image.png
  • 如何停止线程?

    • 只有一种Run方法结束。(使用interrupt(中断)方法!该方法时结束线程的冻结状态,使线程回到运行状态来。)

    • 开启线程运行,运行代码通常是循环结构。

    • 只要控制好线程就能让Run方法结束。

  • 一般情况:

class StopThread implements Runnable{
    private boolean flag = true;
    public void run(){
        while(flag){
            System.out.println(Thread.currentThread().getName()+".....run");
        }
    }
    public void changeFlag(){    //改变锁中线程的标志位,以达到终止线程的作用。
        flag = false;
    }
}
class StopDemo{
    public static void main(String[] args){
        StopThread st = new StopThread();

        Thread t1 = new Thread(st);
        Thread t2 = new Thread(st);

        t1.start();
        t2.start();

        int num = 0;
        while(true){
            num++;
            if(num == 10){
                st.changeFlag();    //中断线程!
                break;                //退出程序!
            }
            System.out.println(Thread.currentThread().getName()+".............."+num);    //显示主线程的运行状况。
        }
    }
}
  • 运行结果:

    • image.png
  • 特殊情况:

    • 当线程处于了冻结状态,就不会读取标记,那么线程就不会结束。
  • 当没有指定方式让冻结线程恢复到运行状态时,这时想要对线程进行冻结线程进行清除。

    • 强制让冻结线程恢复到运行状态中来,这样就可以让线程操作标记让线程结束。
  • 代码:

//特殊情况!!!同步!当线程处于了冻结状态,就不会读区标记,就不会结束线程
//解决办法:InterruptedException,如果用循环停止线程,wait(),sleep()就是异常!
class StopThread implements Runnable{
    private boolean flag = true;
    public synchronized void run(){
        while(flag){
            try{
                wait();
            }
            catch(InterruptedException e){  //将冻结状态的线程强制恢复到运行状态。
                System.out.println(Thread.currentThread().getName()+".....Exception");
                flag = false;
            }
            System.out.println(Thread.currentThread().getName()+".....run");
        }
    }
    public void changeFlag(){
        flag = false;
    }
}
class StopDemo{
    public static void main(String[] args){
        StopThread st = new StopThread();

        Thread t1 = new Thread(st);
        Thread t2 = new Thread(st);

        t1.start();
        t2.start();

        int num = 0;
        while(true){
            num++;
            if(num == 100){
                //st.changeFlag();
                t1.interrupt(); //中断线程t1,
                t2.interrupt();
                break;
            }
            System.out.println(Thread.currentThread().getName()+".............."+num);
        }
    }
}
  • 结果:

    • image.png
  • Interrupt( ) :

    • image.png

相关文章

  • 停止线程(stop方法已经过时了)

    为什么stop被放弃了:因为stop里面有一些bug!! image.png 如何停止线程? 只有一种Run方法结...

  • java20(多线程--06 停止线程与守护线程与死锁)

    停止线程: stop方法已过时。那么该如何停止线程呢?只有一种,run方法结束 开启多线程运行,运行代码通常都是循...

  • 如何停止线程

    stop方法已经过时如何停止线程? 只有一种,run方法结束。开启多线程运行,运行代码通常是循环结构。只要控制住循...

  • java多线程之interrupted()和isInterrup

    关于线程终止方法interrupt() 由于stop()方法已经过时和废弃,是之前JDK设计有缺陷的方法,所以我们...

  • Java错误的停止线程的方法

    1. 被弃用的 stop、suspend 和 resume 方法 用 stop() 来停止线程,会导致线程运行一半...

  • Android中终止线程的办法

    1.在以前的java版本里面,线程结束提供了stop方法;但是现在已经过时了,不再推荐使用此方法,也就是不再使用这...

  • 高并发(3)- 多线程的停止

    前言 上篇文章讲解了多线程的两种创建方法,那么本文就带来多线程的停止方法。 一、线程的停止 1.stop方法 st...

  • 为什么不能用stop停止线程

    为什么废弃stop()? java里线程停止有一个方法,叫stop(),但是很早就被废弃了,因为会导致很多问题。比...

  • 线程交互

    (1)在线程中停止另一个线程 thread.stop(); 强制停止 (不安全) 由于stop的时候线程可能处于...

  • java笔记--多线程总结

    停止线程的方法: 1 stop方法。 2 run方法结束。 怎么控制线程的任务结束呢?任务中都会有循环结构,只要控...

网友评论

      本文标题:停止线程(stop方法已经过时了)

      本文链接:https://www.haomeiwen.com/subject/orylpftx.html