Java中的并发集合类
Java提供了一系列并发集合类,用于在多线程环境下安全地操作集合数据。这些集合类位于java.util.concurrent
包中,主要包括以下几类:
- ConcurrentHashMap:线程安全的哈希表。
- CopyOnWriteArrayList:线程安全的列表。
- CopyOnWriteArraySet:线程安全的集合。
- BlockingQueue:阻塞队列。
- ConcurrentLinkedQueue:线程安全的队列。
- ConcurrentSkipListMap:线程安全的有序映射。
- ConcurrentSkipListSet:线程安全的有序集合。
并发集合类的使用场景
1. ConcurrentHashMap
ConcurrentHashMap
是线程安全的哈希表,适用于高并发场景下的读写操作。
使用场景:缓存实现、计数器、分布式锁等。
示例代码:
import java.util.concurrent.ConcurrentHashMap;
public class Main {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
// 写操作
map.put("key1", 1);
map.put("key2", 2);
// 读操作
System.out.println(map.get("key1")); // 输出:1
// 并发读写
Runnable writer = () -> {
for (int i = 0; i < 1000; i++) {
map.put("key" + i, i);
}
};
Runnable reader = () -> {
for (int i = 0; i < 1000; i++) {
System.out.println(map.get("key" + i));
}
};
Thread writerThread = new Thread(writer);
Thread readerThread = new Thread(reader);
writerThread.start();
readerThread.start();
try {
writerThread.join();
readerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Map size: " + map.size()); // 输出:1000
}
}
2. CopyOnWriteArrayList
CopyOnWriteArrayList
是线程安全的列表,适用于读多写少的场景。
使用场景:事件监听器、配置管理、缓存等。
示例代码:
import java.util.concurrent.CopyOnWriteArrayList;
public class Main {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
// 写操作
list.add("element1");
list.add("element2");
// 读操作
for (String element : list) {
System.out.println(element);
}
// 并发读写
Runnable writer = () -> {
for (int i = 0; i < 1000; i++) {
list.add("element" + i);
}
};
Runnable reader = () -> {
for (String element : list) {
System.out.println(element);
}
};
Thread writerThread = new Thread(writer);
Thread readerThread = new Thread(reader);
writerThread.start();
readerThread.start();
try {
writerThread.join();
readerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("List size: " + list.size()); // 输出:1002
}
}
3. BlockingQueue
BlockingQueue
是阻塞队列,适用于生产者-消费者模式。
使用场景:任务调度、消息队列、线程池等。
示例代码:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Main {
public static void main(String[] args) {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(10);
// 生产者线程
Runnable producer = () -> {
try {
for (int i = 0; i < 10; i++) {
queue.put("Element " + i);
System.out.println("Produced Element " + i);
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
// 消费者线程
Runnable consumer = () -> {
try {
while (true) {
String element = queue.take();
System.out.println("Consumed " + element);
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread producerThread = new Thread(producer);
Thread consumerThread = new Thread(consumer);
producerThread.start();
consumerThread.start();
try {
producerThread.join();
consumerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
通过理解Java中的并发集合类及其使用场景,可以更好地编写高效、安全的并发程序。