wait()的问题;

时间:2013-02-23 07:59:03

标签: java multithreading synchronization

我目前有这种运行方法,我希望3个单独的线程访问并等待另一个线程通知。

public void run(){
   try {
      System.out.println(this.name + " waits on the table...");
      /// waits for agent to signal that new ingredients have been passed out
      wait();
   } catch(InterruptedException ex) {
      Thread.currentThread().interrupt();
   }
   System.out.println(this.name + " stops waiting and checks the table...");
   checkTable();
}

目前,这会抛出java.lang.IllegalMonitorStateException,可以通过对方法使用同步来解决。问题是,当我使用同步时,第一个线程将等待,而另外两个线程无法执行任何操作,因为第一个线程对方法有锁定。所以我的问题是如何才能使三个独立的线程可以访问此方法并同时等待?

2 个答案:

答案 0 :(得分:3)

要使用wait(),您必须own the monitorYou acquire the ownership by using synchronize。 在您的情况下synchronize(this)

但要阻止3个帖子并从第4个帖子中释放,您可以使用java.util.concurrent.CyclicBarrier提到的java.util.concurrent.CountDownLatchPerception

以下是CyclicBarrier版本:

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class RendezVous extends Thread {

   private final CyclicBarrier _barrier;

   RendezVous( String name, CyclicBarrier barrier ) {
      super( name );

      _barrier = barrier;
      setDaemon( true );
      start();
   }

   @Override public void run() {
      System.out.println( getName() + " waits for counterparts..." );
      try { _barrier.await(); }
      catch( InterruptedException | BrokenBarrierException x ) {
         x.printStackTrace(); }
      System.out.println( getName() + " has reached its rendez-vous!");
   }

   public static void main( String[] args ) throws InterruptedException {
      CyclicBarrier barrier = new CyclicBarrier( 4 );
      new RendezVous( "Rdvz 1", barrier ); Thread.sleep(  1000L );
      new RendezVous( "Rdvz 2", barrier ); Thread.sleep(  1000L );
      new RendezVous( "Rdvz 3", barrier ); Thread.sleep(  1000L );
      new RendezVous( "Rdvz 4", barrier ); Thread.sleep( 10000L );
   }
}

输出:

Rdvz 1 waits for counterparts...
Rdvz 2 waits for counterparts...
Rdvz 3 waits for counterparts...
Rdvz 4 waits for counterparts...
Rdvz 4 has reached its rendez-vous!
Rdvz 1 has reached its rendez-vous!
Rdvz 2 has reached its rendez-vous!
Rdvz 3 has reached its rendez-vous!

带有CountDownLatch的版本:

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo extends Thread {

   private final CountDownLatch _cdl;

   CountDownLatchDemo( String name, CountDownLatch cdl ) {
      super( name );

      _cdl = cdl;
      setDaemon( true );
      start();
   }

   @Override public void run() {
      System.out.println( getName() + " waits for counterparts..." );
      try {
         _cdl.countDown();
         _cdl.await();
      }
      catch( InterruptedException x ) {
         x.printStackTrace(); }
      System.out.println( getName() + " has reached its rendez-vous!");
   }

   public static void main( String[] args ) throws InterruptedException {
      CountDownLatch cdl = new CountDownLatch( 4 );
      new CountDownLatchDemo( "Rdvz 1", cdl ); Thread.sleep(  1000L );
      new CountDownLatchDemo( "Rdvz 2", cdl ); Thread.sleep(  1000L );
      new CountDownLatchDemo( "Rdvz 3", cdl ); Thread.sleep(  1000L );
      new CountDownLatchDemo( "Rdvz 4", cdl ); Thread.sleep( 10000L );
   }
}

输出:

Rdvz 1 waits for counterparts...
Rdvz 2 waits for counterparts...
Rdvz 3 waits for counterparts...
Rdvz 4 waits for counterparts...
Rdvz 4 has reached its rendez-vous!
Rdvz 1 has reached its rendez-vous!
Rdvz 2 has reached its rendez-vous!
Rdvz 3 has reached its rendez-vous!

答案 1 :(得分:0)

当第一个线程进入等待状态时,它会释放锁定。所以其他线程可以获得锁