⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadexp7.java

📁 java关于多线程的课堂练习
💻 JAVA
字号:
/**
 * @(#)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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -