编写程序测试线程的sleep()方法和interrupt()方法

import java.util.*;

public class TestInterrupted{
    
    public static void main(String[] args){
        Thread t = new MyThread();
        t.start();
        try{
            Thread.sleep(10000);
        } catch(InterruptedException e){
            System.exit(-1);
        }
        t.interrupt();
    }
    
}

class MyThread extends Thread {
    
    public void run(){
        while(true){
            System.out.println("=="+new Date()+"==");
            try{
                sleep(1000);
            } catch(InterruptedException e){
                System.exit(-1);
            }
        }
    }
    
}

猜你喜欢

转载自www.cnblogs.com/yxfyg/p/12410218.html
今日推荐