Interrupted()与sleep()测试
Interrupted()与sleep()测试
public class TestInterruted implements Runnable {
Thread zhang,zhao,teacher;
public TestInterruted() {
teacher = new Thread(this);
zhang = new Thread(this);
zhao = new Thread(this);
zhang.setName("zhang");
zhao.setName("zhao");
}
public void run() {
if (Thread.currentThread() == zhang) {
try {
System.out.println(zhang.getName()+"休息10秒钟后说");
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(zhang.getName()+"被吵醒了");
}
System.out.println("早上好!");
} else if (Thread.currentThread() == zhao){
try {
System.out.println(zhao.getName()+"详细10秒钟后说");
Thread.sleep(10000);
} catch (Exception e) {
System.out.println(zhao.getName()+"被吵醒了");
}
System.out.println("Good morning!");
}
else{
for(int i = 0;i <= 5;i++){
System.out.println("\n上课了!");
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
zhang.interrupt();
zhao.interrupt();
}
}
}
public class InterruptMain {
public static void main(String[] args) {
TestInterruted test = new TestInterruted();
test.zhang.start();
test.zhao.start();
test.teacher.start();
}
}
(常州java培训)