唤醒正在睡眠的线程

可以使用Thread类中的interrupt方法唤醒正在睡眠的线程,调用interrupt方法会抛出一个InterruptedException的异常。

package com.sutaoyu.Thread;

public class test_9 {
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            public void run() {
                try {
                    //睡眠时间长一些
                    Thread.sleep(100000000000L);
                }catch(InterruptedException e) {
                    e.printStackTrace();
                }
                
                for(int i = 0;i<10;i++) {
                    System.out.println("sleep");
                }
            }
        };
        
        t1.start();
        
        //唤醒睡眠中的线程
        t1.interrupt();
        
    }
}

 

相关文章:

  • 2022-01-27
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-07-24
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-27
  • 2021-05-26
  • 2021-06-13
  • 2021-12-03
相关资源
相似解决方案