问题
编写一个Java应用程序,要求有三个进程:student1,student2,teacher,其中线程student1准备“睡”1分钟后再开始上课,线程student2准备“睡”5分钟后再开始上课。Teacher在输出4句“上课”后,“唤醒”了休眠的线程student1;线程student1被“唤醒”后,负责再“唤醒”休眠的线程student2。
代码
package training10;
public class ThreadTest {
public static void main(String[] args) {
// TODO 自动生成的方法存根
TeacherThread teacher=new TeacherThread("teacher");
Stu1Thread stu1=new Stu1Thread("stu1");
Stu2Thread stu2=new Stu2Thread("stu2");
//stu1.start();
//stu2.start();
try {
stu1.sleep(2*1000);
stu2.sleep(4*1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
teacher.start();
stu1.start();
stu2.start();
}
}
class TeacherThread extends Thread{
String threadName;
public TeacherThread(String threadName)
{ System.out.println("本线程的名字:" + threadName );
this.threadName=threadName;
}
public void run()
{
for(int i=0;i<4;i++)
System.out.println("teacher:上课");
}
}
class Stu1Thread extends Thread{
String threadName;
public Stu1Thread(String threadName)
{ System.out.println("本线程的名字:" + threadName );
this.threadName=threadName;
}
@Override
public void run()
{
// TODO 自动生成的方法存根
System.out.println("本线程" + threadName+"被唤醒" );
}
}
class Stu2Thread extends Thread{
String threadName;
public Stu2Thread(String threadName)
{ System.out.println("本线程的名字:" + threadName );
this.threadName=threadName;
}
@Override
public void run() {
// TODO 自动生成的方法存根
System.out.println("本线程" + threadName+"被唤醒" );
}
}