在Java中,集合类分为线程安全的和非线程安全的。线程安全的集合类在多线程环境下可以安全地进行操作,而不线程安全的集合类在多线程环境下可能会导致数据不一致或异常。
线程安全的集合
- Vector:线程安全的动态数组,类似于ArrayList,但所有方法都是同步的。
- Hashtable:线程安全的哈希表,类似于HashMap,但所有方法都是同步的。
- Collections.synchronizedList:通过Collections工具类提供的同步包装器,将非线程安全的List转换为线程安全的List。
- Collections.synchronizedMap:通过Collections工具类提供的同步包装器,将非线程安全的Map转换为线程安全的Map。
- ConcurrentHashMap:线程安全的哈希表,支持高并发操作,性能优于Hashtable。
- CopyOnWriteArrayList:线程安全的列表,写操作时会复制整个列表,适合读多写少的场景。
- CopyOnWriteArraySet:线程安全的集合,底层使用CopyOnWriteArrayList实现。
- BlockingQueue:线程安全的队列,支持阻塞操作,如ArrayBlockingQueue、LinkedBlockingQueue等。
非线程安全的集合
- ArrayList:非线程安全的动态数组。
- LinkedList:非线程安全的双向链表。
- HashMap:非线程安全的哈希表。
- HashSet:非线程安全的集合,底层使用HashMap实现。
- TreeMap:非线程安全的有序映射。
- TreeSet:非线程安全的有序集合,底层使用TreeMap实现。
思维导图(文字描述)
集合类
├── 线程安全的集合
│ ├── Vector
│ ├── Hashtable
│ ├── Collections.synchronizedList
│ ├── Collections.synchronizedMap
│ ├── ConcurrentHashMap
│ ├── CopyOnWriteArrayList
│ ├── CopyOnWriteArraySet
│ └── BlockingQueue
│ ├── ArrayBlockingQueue
│ ├── LinkedBlockingQueue
│ └── PriorityBlockingQueue
├── 非线程安全的集合
│ ├── ArrayList
│ ├── LinkedList
│ ├── HashMap
│ ├── HashSet
│ ├── TreeMap
│ └── TreeSet
└── 使用场景
├── 多线程环境
│ ├── 读多写少
│ └── 高并发操作
└── 单线程环境
├── 性能要求高
└── 简单操作
Java代码示例
线程安全的集合示例
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ConcurrentHashMap;
public class ThreadSafeCollectionsExample {
public static void main(String[] args) {
// 使用Vector
java.util.Vector<String> vector = new java.util.Vector<>();
vector.add("Vector");
// 使用Hashtable
java.util.Hashtable<String, String> hashtable = new java.util.Hashtable<>();
hashtable.put("key", "value");
// 使用Collections.synchronizedList
List<String> syncList = Collections.synchronizedList(new java.util.ArrayList<>());
syncList.add("SyncList");
// 使用Collections.synchronizedMap
Map<String, String> syncMap = Collections.synchronizedMap(new java.util.HashMap<>());
syncMap.put("key", "value");
// 使用ConcurrentHashMap
ConcurrentHashMap<String, String> concurrentMap = new ConcurrentHashMap<>();
concurrentMap.put("key", "value");
// 使用CopyOnWriteArrayList
CopyOnWriteArrayList<String> cowList = new CopyOnWriteArrayList<>();
cowList.add("CopyOnWriteArrayList");
// 打印结果
System.out.println("Vector: " + vector);
System.out.println("Hashtable: " + hashtable);
System.out.println("SyncList: " + syncList);
System.out.println("SyncMap: " + syncMap);
System.out.println("ConcurrentHashMap: " + concurrentMap);
System.out.println("CopyOnWriteArrayList: " + cowList);
}
}
非线程安全的集合示例
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.TreeMap;
import java.util.TreeSet;
public class NonThreadSafeCollectionsExample {
public static void main(String[] args) {
// 使用ArrayList
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("ArrayList");
// 使用LinkedList
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("LinkedList");
// 使用HashMap
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("key", "value");
// 使用HashSet
HashSet<String> hashSet = new HashSet<>();
hashSet.add("HashSet");
// 使用TreeMap
TreeMap<String, String> treeMap = new TreeMap<>();
treeMap.put("key", "value");
// 使用TreeSet
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("TreeSet");
// 打印结果
System.out.println("ArrayList: " + arrayList);
System.out.println("LinkedList: " + linkedList);
System.out.println("HashMap: " + hashMap);
System.out.println("HashSet: " + hashSet);
System.out.println("TreeMap: " + treeMap);
System.out.println("TreeSet: " + treeSet);
}
}
代码解释
-
线程安全的集合示例:
- 使用
Vector和Hashtable,这两个类的所有方法都是同步的。 - 使用
Collections.synchronizedList和Collections.synchronizedMap将非线程安全的ArrayList和HashMap包装成线程安全的集合。 - 使用
ConcurrentHashMap,这是一个高性能的线程安全哈希表。 - 使用
CopyOnWriteArrayList,这是一个线程安全的列表,写操作时会复制整个列表。
- 使用
-
非线程安全的集合示例:
- 使用
ArrayList、LinkedList、HashMap、HashSet、TreeMap和TreeSet,这些类在多线程环境下使用时需要额外的同步措施。
- 使用
通过合理选择和使用线程安全的集合类,可以在多线程环境中避免数据不一致和并发问题。
79

被折叠的 条评论
为什么被折叠?



