57、唤醒正在睡眠的线程

唤醒正在睡眠的线程

可以使用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();
        
    }
}
原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/10154311.html