zoukankan      html  css  js  c++  java
  • 一次性搞明白 interrupt sleep wait区别

    若你不明白下面的问题,可以继续看。

    1.线程正在执行,interrupt可以打断吗?sleep可以打断吗?

    2.stop 和interrupt有什么区别?

    话不多说,上代码

     1 /**
     2  * Descritpion:
     3  *
     4  * @auther wangpeng
     5  * @create 2020/3/21 7:00
     6  */
     7 public class Demo {
     8 
     9     public static void main(String[] args) {
    10 
    11         MyThread myThread = new MyThread();
    12         myThread.start();
    13         myThread.interrupt();//这里会打断 输出吗?
    14 //        myThread.stop(); 
    15     }
    16     static class MyThread extends Thread{
    17         public void run(){
    18             while(true)
    19             {
    20                 System.out.println( "线程运行中.。。。。.." );
    21                 long time = System.currentTimeMillis();//去系统时间的毫秒数
    22                 while((System.currentTimeMillis()-time < 1000)) {
    23                     //这里采用双层嵌套 while循环 模拟程序循环1秒钟,不同于sleep(1000)会阻塞进程。
    24                 }
    25             }
    26         }
    27     }
    28 
    29 }
    View Code

    上面实例看出, interrupt()并没有中断一个正在运行的线程,或者说让一个running中的线程放弃CPU使用权。可以自己换成stop suspend() 和 resume() 方法试试。

    我们可以通过 Thread.currentThread().isInterrupted() 来检查中断状态:

     1 /**
     2  * Descritpion:
     3  *
     4  * @auther wangpeng
     5  * @create 2020/3/21 7:00
     6  */
     7 public class Demo2 {
     8 
     9     public static void main(String[] args) throws InterruptedException {
    10         MyThread myThread = new MyThread();
    11         myThread.start();
    12 
    13         //中断myThread
    14         TimeUnit.SECONDS.sleep(10);
    15         System.out.println(myThread.isInterrupted());
    16         myThread.interrupt();
    17         System.out.println(myThread.isInterrupted());
    18     }
    19     static class MyThread extends Thread{
    20         public void run(){
    21             try {
    22                 int waitTime =1;
    23                 while(!this.isInterrupted())
    24                      {
    25 
    26                     if(waitTime>5){ //执行5次后,等候5秒
    27                         System.out.println("第"+waitTime+"次,开始等待,"+this.isInterrupted());
    28                         TimeUnit.SECONDS.sleep(2);
    29                         System.out.println("---等候5秒结束,继续执行,线程状态+"+Thread.currentThread().getState()+",isInterrupted="+this.isInterrupted());
    30                     }
    31                     System.out.println(waitTime+","+this.isInterrupted());
    32                     waitTime++;
    33 
    34                     long time = System.currentTimeMillis();//去系统时间的毫秒数
    35                     while((System.currentTimeMillis()-time < 1000)) {
    36                         //这里采用双层嵌套 while循环 模拟程序循环1秒钟,不同于sleep(1000)会阻塞进程。
    37 
    38                     }
    39                 }
    40             } catch (InterruptedException e) {
    41                 //线程被interrupte, 写自己的业务处理
    42                 System.out.println("当前线程状态,"+Thread.currentThread().getState()+","+this.isInterrupted());
    43                 e.printStackTrace();
    44             }
    45         }
    46     }
    47 
    48 }
    View Code

    执行结果:

    调用interrupte后,isInterrupted()=ture,捕获异常后,isInterrupted()又是false了。但是同一个程序,4次执行结果不一致,你知道哪里的问题吗?

    如果换成wait然线程等待。会报如下错误,而非 slee interrupted错误。也就是说线程wait后,调用interrupt,线程会成为终止状态。

    而sleep方法后,调用interrupt方法,线程又会进入到运行中状态,或者等待运行中


    /**
    * Descritpion:
    *
    * @auther wangpeng
    * @create 2020/3/21 7:00
    */
    public class Demo2 {

    public static void main(String[] args) {
    MyThread myThread = new MyThread();
    myThread.start();

    //中断myThread
    myThread.interrupt();
    }
    static class MyThread extends Thread{
    public void run(){
    int waitTime =1;
    while(true)
    {
    System.out.println(waitTime+","+this.isInterrupted());
    if(waitTime>5){ //执行5次后,等候5
    try {
    TimeUnit.SECONDS.sleep(5);
    System.out.println(this.isInterrupted());
    System.out.println("---等候5秒结束,继续执行");
    } catch (InterruptedException e) {
    //线程被interrupte, 写自己的业务处理
    System.out.println("当前线程状态,"+Thread.currentThread().getState()+","+this.isInterrupted());
    e.printStackTrace();
    }
    }
    waitTime++;

    long time = System.currentTimeMillis();//去系统时间的毫秒数
    while((System.currentTimeMillis()-time < 1000)) {
    //这里采用双层嵌套 while循环 模拟程序循环1秒钟,不同于sleep(1000)会阻塞进程。

    }
    }
    }
    }

    }
    ==========================================================================           如果您觉得这篇文章对你有帮助,可以【关注我】或者【点赞】,希望我们一起在架构的路上,并肩齐行
    ==========================================================================
  • 相关阅读:
    7-1 城市间紧急救援 (25 分)(最短路径:斯特拉算法)
    常见的MD5碰撞
    PHP常见绕过(更新ing)
    7-10 旅游规划 (25 分)
    杂记:用python向网页提交数据
    python学习日记
    NOIP2018提高组金牌训练营——搜索专题
    noip复习之拓扑排序
    NOIP2018提高组金牌训练营——字符串专题
    NOIP2018提高组金牌训练营——数论专题
  • 原文地址:https://www.cnblogs.com/amberJava/p/12535871.html
Copyright © 2011-2022 走看看