有2个线程:student和teacher,student准备睡10分钟后再开始上课,teacher在输出3句“上课”后,吵醒student。
请问应该如何编啊这个程序?麻烦高手帮帮忙

解决方案 »

  1.   

    public class ThreadTest
    {
    public static void main(String args[])
    {
    Object obj=new Object();
    StudentThead stu=new StudentThead(obj);
    stu.start();
    TeacherThread  teacher=new TeacherThread(obj);
    teacher.start();
    }
    }
    class StudentThead extends Thread
    {
    Object obj=null;
    StudentThead(Object obj)
    {
    this.obj=obj;
    }
    public void run()
    {

    synchronized(obj){ System.out.println("开始睡觉!");
    try{
    obj.wait(10*60*1000);
    }
    catch(Exception es)
    {
    }
    System.out.println("学生上课");
    }
    }
    }
    class TeacherThread extends Thread
    {
    Object obj=null;
    TeacherThread(Object obj)
    {
    this.obj=obj;
    }
    public void run()
    {
    synchronized(obj){
    for(int i=0;i<3;i++)
    {
    System.out.println("上课了");
    }
    obj.notify();
    }
    }
    }
      

  2.   


    public   class   ThreadTest 

    public   static   void   main(String   args[]) 

    Object   obj=new   Object(); 
    StudentThead   stu=new   StudentThead(obj); 
    stu.start(); 
    TeacherThread     teacher=new   TeacherThread(obj); 
    teacher.start(); 


    class   StudentThead   extends   Thread 

    Object   obj=null; 
    StudentThead(Object   obj) 

    this.obj=obj; 

    public   void   run() 
    { synchronized(obj){ System.out.println("开始睡觉!"); 
    try{ 
    obj.wait(10*60*1000); 

    catch(Exception   es) 


    System.out.println("学生上课"); 



    class   TeacherThread   extends   Thread 

    Object   obj=null; 
    TeacherThread(Object   obj) 

    this.obj=obj; 

    public   void   run() 

    synchronized(obj){ 
    for(int   i=0;i <3;i++) 

    System.out.println("上课了"); 

    obj.notify(); 


    }从新发一次
      

  3.   

    class Student extends Thread{
    public void run(){
    try{
    System.out.println("先睡10分钟");
    sleep(600000);
    }catch(InterruptedException ie){
    System.out.println("醒了");
    }
    }
    }class Teacher extends Thread{
    Student s=new Student();
    public void run(){
    for(int i=0;i<3;i++){
    System.out.println("上课");
    }
    s.interrupt();
    }
    }public class TestTwo{
    public static void main(String args[]){
    Student student=new Student();
    student.start();
    Teacher teacher=new Teacher();
    teacher.start();
    }
    }
    如果我这样写为什么不行呢?