问题是关于线程间的通信的
 wait() notify() 是不是都没有参数啊
 那怎么知道那个线程要等待啊
 我在线程方面很不清楚
那个高手可不可以给一个简单的实例程序 带注释的
给我讲一下 线程的问题
十分感谢
(书上的例子没太看明白 )

解决方案 »

  1.   

    不管哪个线程,如果多个线程要通过wait和notify在某一件事情上进行同步,那么他们就应该通过在同样的对象上调用wait和notify。
    如果线程1执行了object1.wait,而线程2执行了object2.notify,那么它们一点关系也没有。
      

  2.   

    wait()有两种形式,第一个形式接受一个毫秒值,在指定的时间长度内暂停线程,第二个形势不接受任何参数,这代表wait()在notify()出现之前会停滞。
      

  3.   

    class WaitNotify extends Blockable {
     public WaitNotify(Container c) {
      super(c);
      new Notifier(this);
     }
     public synchronized void run() {
      while(true) {
       i++;
       update();
       try {
        wait();
       } catch (InterruptedException e) {
         System.err.println("Interrupted");
       }
      }
     }class Notifier extends Thread {
     private WaitNotify wn;
     public Notifier(WaitNotify wn) {
      this.wn=wn;
      start();
     }
     public void run() {
      while(true) {
       try {
        sleep(2000);
       } catch (InterruptedException e) {
        System.err.println("Interrupted");
       }
       synchronized(wn) {
        wm.notify();
       }
      }
     }
    }
      

  4.   

    谢谢大家 可不可以给个例子 生动的解释一下
    因为我这个地方不管是java vc 都不太清楚
      

  5.   

    class WaitNotify extends Blockable {
     public WaitNotify(Container c) {super(c);}
     public synchronized void run() {
      while(true) {
       i++;
       update();
       try {
        wait(1000);
       } catch (InterruptedException e) {
         System.err.println("Interrupted");
       }
      }
     }
    }
    上边是两种模式的例程
      

  6.   

    notify() is sending a call to a thread that has already stop;it do not tell which thread it will call,just sends the message to awake the stoped thread;similar to the notify(),notifyall() is used to sending calls to every method ,in spite of they are stop or runing.
      

  7.   

    呵呵 wait() 执行之后将线程将停止运作,等待wait的那个毫秒值得过去或者直到其他的线程通过notify()通知他可以继续,就是这样
      

  8.   

    我总结一下看是不是这个意思
     wait()
    方法使调用它的线程进入等待状态 如果有时间参数 那么等待相应的时间 类似于sleep(int i);
    notify()
    对那些进入暂停状态的线程起作用 那个进程暂停 收到notify就重新运行
    notifyall()对系统中所用得线程发消息 
    后来的看看我的总结对不对
    谢谢
      

  9.   

    你的意思是wait(时间) 在这个期间如果有notify()
    这个进程也被唤醒 对吗?ysbcg
      

  10.   

    恩 好的
    那么我总结的对吗?
    除了带参数的wait语句那个
      

  11.   

    不对,你可以翻看synchorized这个关键字,sleep后,线城要让出对象接别锁,wait则不