本帖最后由 闫宽 于 2013-6-4 11:19 编辑
- public class ep7_4 {
- public static void main(String[] args) {
- B a = new B();
- a.student.start();
- a.teacher.start();
- }
- }
- class B implements Runnable {
- Thread student, teacher;
- B() {
- 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() + "被老师叫醒了");
- }
- 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) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- student.interrupt();// 吵醒student
- }
- }
- }
复制代码 其中run方法怎么理解?try--catch中的catch在什么情况下被执行??
System.out.println(student.getName() + "开始听课");这个怎么被执行?
|