threadexp7.java
来自「java关于多线程的课堂练习」· Java 代码 · 共 69 行
JAVA
69 行
/**
* @(#)ThreadExp7.java
*
*
* @author
* @version 1.00 2007/11/29
*/
import java.util.*;
class A implements Runnable {
Thread student,teacher;
long begin,end;
A() {
teacher=new Thread(this);
student=new Thread(this);
teacher.setName("王教授");
student.setName("李明");
}
public void run() {
if (Thread.currentThread()==student) {
try {
System.out.println(student.getName()+"正在睡觉,不听课");
Thread.sleep(1000*60*60);
}
catch (InterruptedException e) {
System.out.println(student.getName()+"被老师叫醒了");
//end=System.currentTimeMillis();
//System.out.println(end-begin);
}
//end=System.currentTimeMillis();
//System.out.println(end-begin);
System.out.println(student.getName()+"开始听课");
}
else if (Thread.currentThread()==teacher) {
for (int i=1; i<=3; i++) {
System.out.println("上课啦!");
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
System.out.println("there is an interruption in teacher process!");
}
}
student.interrupt();
//begin=System.currentTimeMillis();
}
}
}
public class ThreadExp7 {
/**
* Creates a new instance of <code>ThreadExp7</code>.
*/
public ThreadExp7() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
A a=new A();
a.student.start();
a.teacher.start();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?