参照例15.7,编写一个Java应用程序,要求有student1、student2和teacher 3个线程,其中,student1准备睡10分钟后再开始上课,student2准备睡1小时后再开始上课。teacher在输出3句“上课”后吵醒休眠的线程student1; student1被吵醒后,负责吵醒休眠的线程student2。
public class teacher {
public static void main(String[] args){
Student1 stu1=new Student1();
Student2 stu2=new Student2();
Thread thread2=new Thread(stu2);
Thread thread1=new Thread(stu1);
stu1.setThread(thread2);
thread1.start();
thread2.start();
System.out.println("上课");
}
}
public class Student1 implements Runnable{
Thread student2;
@Override
public void run() {
try {
Thread.sleep(1000*3);
student2.interrupt();
System.out.println("1结束");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void setThread(Thread student2){
this.student2=student2;
}
}
public class Student2 implements Runnable{
@Override
public void run() {
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
System.out.println("2结束");
}
}
}