public class Test { public static void main(String[] args) { ClassRoom room210=new ClassRoom(); room210.student.start(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } room210.teacher.start() ;//开启老师线程 } } class ClassRoom implements Runnable{ Thread student, teacher; ClassRoom(){ //补充代码:创建学生线程和老师线程 } public void run() { if( /*补充代码:判断当前线程是否是学生线程*/){ System.out.println("学生休息10分钟"); try { //补充代码:休眠60分钟 } catch (InterruptedException e) { System.out.println("学生被吵醒"); } System.out.println("学生开始上课"); } else if( /*补充代码:判断当前线程是否是老师线程*/ ){ for(int i=0; i<3; i++) { System.out.println("上课"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } //补充代码:唤醒学生 } } } 帮我补充以上代码
时间: 2024-03-20 11:38:33 浏览: 62
C#中static void Main(string[] args) 参数示例详解
补充代码如下:
```
public class Test {
public static void main(String[] args) {
ClassRoom room210 = new ClassRoom();
room210.student.start(); // 开启学生线程
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
room210.teacher.start(); // 开启老师线程
}
}
class ClassRoom implements Runnable {
Thread student, teacher;
ClassRoom() {
// 创建学生线程和老师线程
student = new Thread(this);
teacher = new Thread(this);
}
public void run() {
if (Thread.currentThread() == student) { // 判断当前线程是否是学生线程
System.out.println("学生休息10分钟");
try {
Thread.sleep(600000); // 休眠60分钟
} catch (InterruptedException e) {
System.out.println("学生被吵醒");
}
System.out.println("学生开始上课");
} else if (Thread.currentThread() == teacher) { // 判断当前线程是否是老师线程
for (int i = 0; i < 3; i++) {
System.out.println("上课");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized (student) { // 唤醒学生
student.notify();
}
}
}
}
```
阅读全文