编写程序测试线程的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); } } } }
浙公网安备 33010602011771号