Java 并发工具核心库中常用类的使用方法
Java并发工具核心库(如java.util.concurrent包)中的一些常用类提供了强大的工具和功能,可以帮助开发人员更好地处理并发编程问题。在本文中,我们将介绍几个常用类的使用方法,并附上必要的编程代码和相关配置。
1. CountDownLatch(倒计时门闩)
CountDownLatch是一个同步工具类,它允许线程等待直到一组操作完成。它通过指定一个计数器,每个线程完成操作时,计数器就会减1,当计数器达到零时,等待线程将会继续执行。
编程示例:
import java.util.concurrent.CountDownLatch;
public class CountdownLatchExample {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(3); // 创建一个计数器,初始值为3
// 创建3个线程并启动
new Thread(new Worker(latch, "Worker1")).start();
new Thread(new Worker(latch, "Worker2")).start();
new Thread(new Worker(latch, "Worker3")).start();
latch.await(); // 等待计数器为0
System.out.println("All workers have completed their tasks");
}
}
class Worker implements Runnable {
private final CountDownLatch latch;
private final String name;
public Worker(CountDownLatch latch, String name) {
this.latch = latch;
this.name = name;
}
public void run() {
System.out.println(name + " is doing some work");
// 模拟耗时操作
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " has completed their task");
latch.countDown(); // 计数器减1
}
}
输出结果:
Worker2 is doing some work
Worker1 is doing some work
Worker3 is doing some work
Worker2 has completed their task
Worker1 has completed their task
Worker3 has completed their task
All workers have completed their tasks
2. Semaphore(信号量)
Semaphore是一个计数信号量,用于控制同时访问某个资源的线程数。它维护了一个许可证的计数器,通过acquire()获取一个许可证,通过release()释放一个许可证。
编程示例:
import java.util.concurrent.Semaphore;
public class SemaphoreExample {
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(3); // 创建一个初始值为3的信号量
// 创建10个线程并启动
for (int i = 1; i <= 10; i++) {
new Thread(new Worker(semaphore, "Worker" + i)).start();
}
}
}
class Worker implements Runnable {
private final Semaphore semaphore;
private final String name;
public Worker(Semaphore semaphore, String name) {
this.semaphore = semaphore;
this.name = name;
}
public void run() {
try {
semaphore.acquire(); // 获取许可证
System.out.println(name + " is working");
Thread.sleep(2000); // 模拟工作
System.out.println(name + " has finished their work");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release(); // 释放许可证
}
}
}
输出结果:
Worker1 is working
Worker4 is working
Worker3 is working
Worker4 has finished their work
Worker1 has finished their work
Worker3 has finished their work
Worker6 is working
Worker5 is working
Worker10 is working
Worker6 has finished their work
Worker2 is working
Worker5 has finished their work
Worker9 is working
Worker10 has finished their work
Worker7 is working
Worker9 has finished their work
Worker8 is working
Worker2 has finished their work
Worker7 has finished their work
Worker8 has finished their work
3. CyclicBarrier(循环屏障)
CyclicBarrier是一种同步辅助类,它允许一组线程互相等待,直到所有线程都到达一个共同的屏障点。当所有线程都到达屏障点后,它们将继续执行之后的操作。
编程示例:
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierExample {
public static void main(String[] args) {
CyclicBarrier barrier = new CyclicBarrier(3, () -> {
System.out.println("All threads have reached the barrier");
}); // 创建一个屏障点,等待的线程数为3
// 创建3个线程并启动
new Thread(new Worker(barrier, "Worker1")).start();
new Thread(new Worker(barrier, "Worker2")).start();
new Thread(new Worker(barrier, "Worker3")).start();
}
}
class Worker implements Runnable {
private final CyclicBarrier barrier;
private final String name;
public Worker(CyclicBarrier barrier, String name) {
this.barrier = barrier;
this.name = name;
}
public void run() {
System.out.println(name + " is doing some work");
// 模拟耗时操作
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " has reached the barrier");
try {
barrier.await(); // 等待所有线程到达屏障点
System.out.println(name + " continues execution");
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果:
Worker2 is doing some work
Worker1 is doing some work
Worker3 is doing some work
Worker2 has reached the barrier
Worker1 has reached the barrier
Worker3 has reached the barrier
All threads have reached the barrier
Worker3 continues execution
Worker1 continues execution
Worker2 continues execution
在使用这些并发工具类时,还需要注意相关的配置。例如,可以通过构造函数传入参数来初始化CountDownLatch、Semaphore或CyclicBarrier的计数器大小。此外,这些类还提供了其他方法和重载构造函数,用于满足特定需求。
总结:
Java并发工具核心库中的常用类提供了强大的功能,可以帮助开发人员更好地处理并发编程问题。本文介绍了CountDownLatch、Semaphore和CyclicBarrier这三个常用类的使用方法,并通过示例代码演示了它们的基本应用。在实际开发中,合理选择并正确使用这些类,可以提高程序的性能和可靠性。
Read in English