シリーズ: Java Old
java
57 行
· 更新日 2026-02-10
CountDownLatchDemo.java
Java_Old/advanced/concurrency_course/module03/src/concurrency/module03/CountDownLatchDemo.java
package concurrency.module03;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Demonstrates {@link java.util.concurrent.CountDownLatch}: a one-shot barrier
* that starts at a count N; countDown() decrements it; await() blocks until the
* count reaches 0.
*
* <p><b>Two common patterns:</b>
* <ul>
* <li><b>Start gate:</b> Latch(1). Workers call await(); main (or a coordinator)
* calls countDown() when "ready" so all workers start together.</li>
* <li><b>Finish gate:</b> Latch(N). Each of N workers does its work then countDown();
* main await()s until all are done.</li>
* </ul>
* This demo uses both: workers wait on a start gate, then main waits on a done gate.
*/
public class CountDownLatchDemo {
/**
* Creates 3 worker threads that await a start gate (count=1), then each does
* a short sleep and countDown on a done gate (count=3). Main releases the
* start gate, then await()s the done gate (with timeout).
*
* @param args unused
* @throws InterruptedException if interrupted during await or join
*/
public static void main(String[] args) throws InterruptedException {
int workers = 3;
CountDownLatch startGate = new CountDownLatch(1);
CountDownLatch doneGate = new CountDownLatch(workers);
for (int i = 0; i < workers; i++) {
int id = i;
new Thread(() -> {
try {
startGate.await(); // Wait until main releases the start gate
System.out.println("Worker " + id + " started");
Thread.sleep(100);
} catch (InterruptedException e)
{ Thread.currentThread().interrupt();
}
finally {
doneGate.countDown(); // Signal this worker is done
}
}).start();
}
System.out.println("Main: releasing start gate");
startGate.countDown();
doneGate.await(5, TimeUnit.SECONDS);
System.out.println("Main: all workers done");
}
}
関連記事
Java Old
java
更新日 2026-02-03
Main.java
Main.java — java source code from the Java Old learning materials (Java_Old/Main.java).
記事を読む →
Java Old
java
更新日 2026-02-03
Student.java
Student.java — java source code from the Java Old learning materials (Java_Old/Student.java).
記事を読む →
Java Old
java
更新日 2026-02-03
01_Generics.java
01_Generics.java — java source code from the Java Old learning materials (Java_Old/advanced/01_Generics.java).
記事を読む →
Java Old
java
更新日 2026-02-03
02_Collections.java
02_Collections.java — java source code from the Java Old learning materials (Java_Old/advanced/02_Collections.java).
記事を読む →
Java Old
java
更新日 2026-02-03
03_Streams.java
03_Streams.java — java source code from the Java Old learning materials (Java_Old/advanced/03_Streams.java).
記事を読む →
Java Old
java
更新日 2026-02-03
CollectionsExample.java
CollectionsExample.java — java source code from the Java Old learning materials (Java_Old/advanced/CollectionsExample.java).
記事を読む →