Java.util.concurrent 包
java.util.concurrent 是 Java 提供的一组类和接口,用于简化并发编程和多线程操作。它引入了许多高级工具和并发结构,使得编写线程安全的代码变得更容易。这个包是在 Java 5 中引入的,并且在后续版本中不断扩展。以下是对 java.util.concurrent 包的详细介绍:
1. 基本组成部分
java.util.concurrent 包主要由以下几个部分组成:
- Executor 框架: 用于管理和控制线程的执行。
- 并发集合类: 提供线程安全的集合类,如 ConcurrentHashMap、CopyOnWriteArrayList 等。
- 同步工具: 如锁(Lock 接口和 ReentrantLock 类)、信号量(Semaphore)、倒计时门闩(CountDownLatch)等。
- 并发工具类: 提供了实用的并发工具,如 Atomic 类和 ForkJoinPool 等。
2. Executor 框架
Executor 框架是 java.util.concurrent 包的核心之一。它通过抽象化线程的创建和管理,简化了并发编程。
- Executor 接口: 提供了执行任务的抽象方法 execute(Runnable command)。
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class ExecutorExample {
public static void main(String[] args) {
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
System.out.println("Task executed in executor!");
});
}
}
- ExecutorService 接口: 扩展了 Executor 接口,增加了管理任务生命周期的方法,如 submit()、shutdown()、invokeAll() 等。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorServiceExample {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
executorService.submit(() -> {
System.out.println("Task executed by " + Thread.currentThread().getName());
});
}
executorService.shutdown();
}
}
- ThreadPoolExecutor 类: ExecutorService 的一个实现,支持线程池管理和任务调度。
- ScheduledExecutorService 接口: 支持任务的定时和周期执行。
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorServiceExample {
public static void main(String[] args) {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> {
System.out.println("Periodic Task executed by " + Thread.currentThread().getName());
}, 0, 3, TimeUnit.SECONDS);
// Optional: Shut down the scheduler after 10 seconds to stop the example
scheduler.schedule(() -> {
scheduler.shutdown();
}, 10, TimeUnit.SECONDS);
}
}
3. 并发集合类
java.util.concurrent 包提供了多种线程安全的集合类,这些集合类通过巧妙的锁机制和算法,允许多线程同时访问。
- ConcurrentHashMap: 一个线程安全的哈希表,支持高效的并发访问。
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.put("A", 1);
map.put("B", 2);
map.computeIfPresent("A", (key, value) -> value + 1);
map.computeIfAbsent("C", key -> 3);
map.forEach((key, value) -> System.out.println(key + ": " + value));
}
}
- CopyOnWriteArrayList: 在修改时创建副本的线程安全列表,适用于读取多于写入的场景。
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListExample {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
list.add("Hello");
list.add("World");
for (String item : list) {
System.out.println(item);
list.add("New Item"); // 这是安全的,不会引发ConcurrentModificationException
}
System.out.println("Final list: " + list);
}
}
- BlockingQueue: 阻塞队列接口,提供了线程安全的队列实现,如 ArrayBlockingQueue、LinkedBlockingQueue、PriorityBlockingQueue 等。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueExample {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> queue = new ArrayBlockingQueue<>(2);
Thread producer = new Thread(() -> {
try {
queue.put("Item 1");
System.out.println("Produced Item 1");
queue.put("Item 2");
System.out.println("Produced Item 2");
queue.put("Item 3");
System.out.println("Produced Item 3");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
Thread consumer = new Thread(() -> {
try {
Thread.sleep(1000); // Simulate some processing time
System.out.println("Consumed " + queue.take());
System.out.println("Consumed " + queue.take());
System.out.println("Consumed " + queue.take());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
producer.start();
consumer.start();
producer.join();
consumer.join();
}
}
- ConcurrentSkipListMap 和 ConcurrentSkipListSet: 基于跳表的有序集合和映射。
4. 同步工具
同步工具是 java.util.concurrent 包中的另一重要组成部分,提供了比 synchronized 关键字更灵活和强大的同步机制。
- Lock 接口: 提供显式的锁机制,如 ReentrantLock、ReadWriteLock 等。
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample {
private final Lock lock = new ReentrantLock();
public void sharedResource() {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + " is executing.");
Thread.sleep(1000); // Simulate some work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
ReentrantLockExample example = new ReentrantLockExample();
Thread t1 = new Thread(example::sharedResource);
Thread t2 = new Thread(example::sharedResource);
t1.start();
t2.start();
}
}
- Semaphore: 信号量,用于控制同时访问某一资源的线程数量。
import java.util.concurrent.Semaphore;
public class SemaphoreExample {
private final Semaphore semaphore = new Semaphore(1);
public void accessResource() {
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + " is accessing the resource.");
Thread.sleep(1000); // Simulate some work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
semaphore.release();
System.out.println(Thread.currentThread().getName() + " released the resource.");
}
}
public static void main(String[] args) {
SemaphoreExample example = new SemaphoreExample();
Thread t1 = new Thread(example::accessResource);
Thread t2 = new Thread(example::accessResource);
t1.start();
t2.start();
}
}
- CountDownLatch: 倒计时门闩,允许一个或多个线程等待一组操作完成。
import java.util.concurrent.CountDownLatch;
public class CountDownLatchExample {
private static final int TASK_COUNT = 3;
private final CountDownLatch latch = new CountDownLatch(TASK_COUNT);
public void performTask(int taskId) {
System.out.println("Task " + taskId + " is starting...");
try {
Thread.sleep(1000); // Simulate some work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
latch.countDown();
System.out.println("Task " + taskId + " is done.");
}
}
public static void main(String[] args) throws InterruptedException {
CountDownLatchExample example = new CountDownLatchExample();
for (int i = 1; i <= TASK_COUNT; i++) {
int taskId = i;
new Thread(() -> example.performTask(taskId)).start();
}
System.out.println("Waiting for all tasks to finish...");
example.latch.await(); // Wait for all tasks to complete
System.out.println("All tasks are finished.");
}
}
- CyclicBarrier: 循环栅栏,使一组线程可以互相等待,直到到达共同的栅栏点。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierExample {
private final CyclicBarrier barrier;
public CyclicBarrierExample(int parties) {
this.barrier = new CyclicBarrier(parties, () -> {
System.out.println("All parties have reached the barrier. Proceeding...");
});
}
public void performTask(int taskId) {
System.out.println("Task " + taskId + " is waiting at the barrier.");
try {
barrier.await();
System.out.println("Task " + taskId + " is proceeding.");
} catch (InterruptedException | BrokenBarrierException e) {
Thread.currentThread().interrupt();
}
}
public static void main(String[] args) {
int parties = 3;
CyclicBarrierExample example = new CyclicBarrierExample(parties);
for (int i = 1; i <= parties; i++) {
int taskId = i;
new Thread(() -> example.performTask(taskId)).start();
}
}
}
- Exchanger: 允许两个线程在同步点交换数据。
import java.util.concurrent.Exchanger;
public class ExchangerExample {
private final Exchanger<String> exchanger = new Exchanger<>();
public void exchangeData(String data) {
try {
System.out.println(Thread.currentThread().getName() + " is exchanging " + data);
String receivedData = exchanger.exchange(data);
System.out.println(Thread.currentThread().getName() + " received " + receivedData);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public static void main(String[] args) {
ExchangerExample example = new ExchangerExample();
Thread t1 = new Thread(() -> example.exchangeData("Data from T1"));
Thread t2 = new Thread(() -> example.exchangeData("Data from T2"));
t1.start();
t2.start();
}
}
5. 并发工具类
除了集合和同步工具,java.util.concurrent 包还提供了许多有用的并发工具类。
- Atomic 包: 提供了原子变量操作类,如 AtomicInteger、AtomicLong、AtomicReference 等,确保变量的线程安全更新。
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerExample {
private final AtomicInteger counter = new AtomicInteger(0);
public void increment() {
int value = counter.incrementAndGet();
System.out.println(Thread.currentThread().getName() + " incremented to " + value);
}
public static void main(String[] args) {
AtomicIntegerExample example = new AtomicIntegerExample();
Thread t1 = new Thread(example::increment);
Thread t2 = new Thread(example::increment);
t1.start();
t2.start();
}
}
- ForkJoinPool: 是 Java 7 引入的,用于分而治之的并行任务执行。
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.ForkJoinPool;
public class ForkJoinExample {
static class SumTask extends RecursiveTask<Integer> {
private final int[] arr;
private final int start;
private final int end;
private static final int THRESHOLD = 10;
public SumTask(int[] arr, int start, int end) {
this.arr = arr;
this.start = start;
this.end = end;
}
@Override
protected Integer compute() {
if (end - start <= THRESHOLD) {
int sum = 0;
for (int i = start; i < end; i++) {
sum += arr[i];
}
return sum;
} else {
int mid = (start + end) / 2;
SumTask leftTask = new SumTask(arr, start, mid);
SumTask rightTask = new SumTask(arr, mid, end);
leftTask.fork();
return rightTask.compute() + leftTask.join();
}
}
}
public static void main(String[] args) {
ForkJoinPool forkJoinPool = new ForkJoinPool();
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
SumTask task = new SumTask(arr, 0, arr.length);
int sum = forkJoinPool.invoke(task);
System.out.println("Sum: " + sum);
}
}
- Phaser: 一个更灵活的 CyclicBarrier,允许动态调整参与的线程数量。
- CompletableFuture: Java 8 引入的类,用于异步编程,支持函数式编程的风格。
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CompletableFutureExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("Task is running in " + Thread.currentThread().getName());
return "Result";
});
CompletableFuture<String> transformedFuture = future.thenApply(result -> result + " transformed");
transformedFuture.thenAccept(finalResult -> {
System.out.println("Final result: " + finalResult);
});
transformedFuture.get(); // This blocks the main thread until the result is ready
}
}
6. 应用场景
java.util.concurrent 包非常适合以下场景:
- 高并发环境: 当需要处理大量并发请求时,使用 ExecutorService 和 ConcurrentHashMap 等工具可以有效提高程序的并发性和性能。
- 复杂任务调度: ScheduledExecutorService 可以用于执行定时任务,ForkJoinPool 适用于递归任务的并行处理。
- 线程协调: 使用 CountDownLatch、CyclicBarrier 等工具,可以轻松实现线程之间的协调和同步。
7. 性能与扩展性
java.util.concurrent 包中的类大多是为高性能设计的,特别是在多处理器环境下,它们能充分利用系统资源。通过合理使用这些工具,可以显著提高应用程序的扩展性和稳定性。
总结
java.util.concurrent 包是 Java 并发编程中的重要组成部分。它提供了一系列高级工具,简化了多线程编程中的许多复杂性,使得开发人员能够更加高效地编写、调试和维护并发程序。通过深入理解并掌握这些工具,开发者可以编写出高效且健壮的并发应用程序。