Wait()与notify()

 

wait()->本线程进入等待队列,只能通过别的线程的notify()notifyall()唤醒  

  notify()->随机地从等待队列删除一个线程,也就是说,该线程进入执行状态  

  notifyall()->删除所有在等待队列中的线程,按线程的优先级执行  

  不推荐使用notify():因为这是随机地从等待队列中取一个线程执行,我们不能通过设定优先级进行控制,如果,随机抽取的线程不能往前执行,这就有可能产生死锁,所以,建议使用notifyall()  

如:

class MyThread extends Thread{

    Test t = new Test();

    public void run(){

    t.test();

    System.out.println("Thread say:Hello,World!");

    }

    public class Test {

    int x = 0;

    public void test(){

    if(x==0)

    try{ wait();}catch(Exception e){}

    }

    }

    public static void main(String[] args) throws Exception{

    new MyThread().start();

    }

}

结果这个线程就不会进入twait方法而直接打印出Thread say:Hello,World!.而如果改成:

public class Test {

      int x = 0;

      public synchornized void test(){

      if(x==0)

      try{

      wait();

      }catch(Exception e){}

      }

      public static void main(String[] args) throws Exception{

      new MyThread().start();

      }

}

我们就可以看到线程一直等待,注意这个线程进入等待后没有其它线程唤醒,除非强行退出JVM环境,否则它一直等待.所以请记住:[线程要想调用一个对象的wait()方法就要先获得该对象的监视锁,而一旦调用wait()后又立即释放该锁]

public class ThreadA{   

    public  static void  main(String[] args){  

      ThreadB   b=new   ThreadB();  

      b.start(); //这里只是将线程b处于可运行状态,并不会立刻执行,实际上是先执行主线(进)程,再去执行线程b,如果主线程运行时间过长的话,则会在期间执行b 

      System.out.println("b   is   start....");  

      synchronized(b){ //定义一个同步块,使用b作为资源锁。对于wait方法必须有的     

        try{  

          System.out.println("Waiting   for   b   to   complete...");  

          b.wait();//临时释放锁,并阻塞当前线程。让其他使用同一把锁的线程ThreadB有机会执行。 注意:直接写wait()是不对的。 

          System.out.println("waiting....");  

        }catch(InterruptedException e){}  

     }  

      System.out.println("Total   is   :"+b.total);

     }   

  }  

  class  ThreadB extends Thread{  

  int   total;  

  public   void   run() {  

      System.out.println("ThreadB   is   running   ....");  

      synchronized(this){ //必须有

      System.out.println("ThreadB   is   excuting   for   statement..");  

      for(int i=0;i<5;i++){  

         total+=i;  

         System.out.println("total   is   "+total);  

      }  

      notify();  //在已经获得了一个对象的锁的前提下,调用notify()会通知线程调度程序,唤醒其它等待这个锁的线程队列中的线程,notifyAll()唤醒所有这类线程。

    }   //end for synchronized

  } 

  }

运行结果:

b   is   start....

Waiting   for   b   to   complete...

ThreadB   is   running   ....

ThreadB   is   excuting   for   statement..

total   is   0

total   is   1

total   is   3

total   is   6

total   is   10

waiting....

Total   is   :10

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值