私が使用している次のプログラムがあり、メソッドjava.util.concurrent.CountDownLatch
を使用せずに正常に動作しています。await()
私は同時実行に不慣れで、の目的を知りたいですawait()
。が必要な理由は理解できますが、なぜCyclicBarrier
ですか?await()
CountDownLatch
クラスCountDownLatchSimple
:
public static void main(String args[]) {
CountDownLatch latch = new CountDownLatch(3);
Thread one = new Thread(new Runner(latch),"one");
Thread two = new Thread(new Runner(latch), "two");
Thread three = new Thread(new Runner(latch), "three");
// Starting all the threads
one.start(); two.start(); three.start();
}
クラスのRunner
実装Runnable
:
CountDownLatch latch;
public Runner(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" is Waiting.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
System.out.println(Thread.currentThread().getName()+" is Completed.");
}
出力
2 は待っています。
3 は待っています。
1つは待っています。
1つは完了です。
2つが完成しました。
3つ完成です。