使用するには、モニターwait()
を所有している必要があります。
を使用して所有権を取得します。あなたの場合。synchronize
synchronize(this)
ただし、3つのスレッドをブロックし、4番目のスレッドから解放するには、Perceptionが言及しjava.util.concurrent.CyclicBarrier
たようにまたはを使用できます。java.util.concurrent.CountDownLatch
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!