当前位置:实例文章 » 其他实例» [文章]CountDownLatch和CyclicBarrier学习

CountDownLatch和CyclicBarrier学习

发布人:shili8 发布时间:2025-03-13 06:42 阅读次数:0

**CountDownLatch 和 CyclicBarrier 学习**

在 Java 中,`CountDownLatch` 和 `CyclicBarrier` 是两个非常有用的类,它们可以帮助我们实现线程之间的同步和协调。虽然它们看起来很相似,但它们的使用场景却是不同的。

### CountDownLatch`CountDownLatch` 是一个计数器,当它的值达到0 时,所有等待其释放的线程都会被唤醒。它可以用来实现多个线程之间的同步和协调。

#### 使用示例

javaimport java.util.concurrent.CountDownLatch;

public class CountDownLatchExample {
 public static void main(String[] args) throws InterruptedException {
 // 创建一个CountDownLatch对象,初始值为3 CountDownLatch latch = new CountDownLatch(3);

 // 创建三个线程,每个线程都要等待latch的值达到0才继续执行 Thread thread1 = new Thread(() -> {
 System.out.println("Thread1: Waiting for latch...");
 try {
 latch.await();
 } catch (InterruptedException e) {
 Thread.currentThread().interrupt();
 }
 System.out.println("Thread1: Latch released, continuing...");
 });

 Thread thread2 = new Thread(() -> {
 System.out.println("Thread2: Waiting for latch...");
 try {
 latch.await();
 } catch (InterruptedException e) {
 Thread.currentThread().interrupt();
 }
 System.out.println("Thread2: Latch released, continuing...");
 });

 Thread thread3 = new Thread(() -> {
 System.out.println("Thread3: Waiting for latch...");
 try {
 latch.await();
 } catch (InterruptedException e) {
 Thread.currentThread().interrupt();
 }
 System.out.println("Thread3: Latch released, continuing...");
 });

 // 启动三个线程 thread1.start();
 thread2.start();
 thread3.start();

 // 等待所有线程执行完毕 latch.countDown(); // 将latch的值减少为0 System.out.println("Main: Latch released, all threads finished.");
 }
}


在这个示例中,我们创建了一个 `CountDownLatch` 对象,初始值为3。然后我们启动三个线程,每个线程都要等待 `latch` 的值达到0 才继续执行。在 `main` 线程中,我们先将 `latch` 的值减少为0,然后等待所有线程执行完毕。

### CyclicBarrier`CyclicBarrier` 是一个循环屏障,当它的值达到0 时,所有等待其释放的线程都会被唤醒。与 `CountDownLatch` 不同的是,`CyclicBarrier` 可以重复使用,而不是只有一次使用。

#### 使用示例
javaimport java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierExample {
 public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
 // 创建一个CyclicBarrier对象,初始值为3 CyclicBarrier barrier = new CyclicBarrier(3);

 // 创建三个线程,每个线程都要等待barrier的值达到0才继续执行 Thread thread1 = new Thread(() -> {
 System.out.println("Thread1: Waiting for barrier...");
 try {
 barrier.await();
 } catch (BrokenBarrierException e) {
 Thread.currentThread().interrupt();
 }
 System.out.println("Thread1: Barrier released, continuing...");
 });

 Thread thread2 = new Thread(() -> {
 System.out.println("Thread2: Waiting for barrier...");
 try {
 barrier.await();
 } catch (BrokenBarrierException e) {
 Thread.currentThread().interrupt();
 }
 System.out.println("Thread2: Barrier released, continuing...");
 });

 Thread thread3 = new Thread(() -> {
 System.out.println("Thread3: Waiting for barrier...");
 try {
 barrier.await();
 } catch (BrokenBarrierException e) {
 Thread.currentThread().interrupt();
 }
 System.out.println("Thread3: Barrier released, continuing...");
 });

 // 启动三个线程 thread1.start();
 thread2.start();
 thread3.start();

 // 等待所有线程执行完毕 System.out.println("Main: All threads finished.");
 }
}


在这个示例中,我们创建了一个 `CyclicBarrier` 对象,初始值为3。然后我们启动三个线程,每个线程都要等待 `barrier` 的值达到0 才继续执行。在 `main` 线程中,我们不需要将 `barrier` 的值减少为0,因为 `CyclicBarrier` 可以重复使用。

### 总结在本文中,我们学习了 `CountDownLatch` 和 `CyclicBarrier` 这两个非常有用的类,它们可以帮助我们实现线程之间的同步和协调。虽然它们看起来很相似,但它们的使用场景却是不同的。`CountDownLatch` 可以用来实现多个线程之间的同步和协调,而 `CyclicBarrier` 可以重复使用,适合于需要多次等待的场景。

相关标签:学习
其他信息

其他资源

Top