synchronizedCollection
public static Collection synchronizedCollection(Collection c)返回由指定 collection 支持的同步(线程安全的)collection。为了保证按顺序访问,必须通过返回的 collection 完成对底层 collection 的所有访问。
package ThreadCollections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
public class CollectionsDemo {
public static void main(String[] args) {
//创建Collection集合
Collection col=new ArrayList();
col.add("hello");
col.add("Thread");
//创建集合安全的Collection
Collection sycol=Collections.synchronizedCollection(col);
//线程同步进行迭代
synchronized(sycol){
//创建迭代器
Iterator it=sycol.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
}
synchronizedList
public static List synchronizedList(List list)返回由指定列表支持的同步(线程安全的)列表。为了保证按顺序访问,必须通过返回的列表完成对底层列表的所有访问。
package ThreadCollections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
//创建List集合
List list=new ArrayList();
list.add("hello");
list.add("list");
list.add("Thread");
//创建集合安全的List
List sylist=Collections.synchronizedList(list);
//线程同步进行迭代
synchronized(sylist){
//创建迭代器
Iterator it=sylist.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
}
synchronizedMap
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)返回由指定映射支持的同步(线程安全的)映射。为了保证按顺序访问,必须通过返回的映射完成对底层映射的所有访问。
package ThreadCollections;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
//创建Map集合
Map map=new HashMap();
//添加键值对map集合
map.put(1, "hello");
map.put(2, "map");
map.put(3, "thread");
//创建集合安全的Map
Map symap=Collections.synchronizedMap(map);
Set set=symap.entrySet();
//线程同步进行迭代
synchronized(symap){
//创建迭代器
Iterator it=set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
}
synchronizedSet
public static Set synchronizedSet(Set s)返回由指定 set 支持的同步(线程安全的)set。为了保证按顺序访问,必须通过返回的 set 完成对底层 set 的所有访问。
package ThreadCollections;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetDemo {
public static void main(String[] args) {
//创建Set集合
Set set=new HashSet();
set.add("hello");
set.add("set");
set.add("Thread");
//创建集合安全的Set
Set syset=Collections.synchronizedSet(set);
//线程同步进行迭代
synchronized(syset){
//创建迭代器
Iterator it=syset.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
}
synchronizedSortedMap
public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m)返回由指定有序映射支持的同步(线程安全的)有序映射。为了保证按顺序访问,必须通过返回的有序映射(或其视图)完成对底层有序映射的所有访问。
package ThreadCollections;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
public class SortMapDemo {
public static void main(String[] args) {
//创建TreeMap集合
TreeMap<Integer,Integer> map=new TreeMap<Integer,Integer>();
//添加键值对Treemap集合
map.put(2,200);
map.put(1,100);
map.put(3,50);
//创建集合安全的Map
Map sysortmap=Collections.synchronizedSortedMap(map);
Set set=sysortmap.entrySet();
//线程同步进行迭代
synchronized(sysortmap){
//创建迭代器
Iterator it=set.iterator();
while(it.hasNext()) {
//对于键进行排序
System.out.println(it.next());
}
}
}
}
synchronizedSortedSet
public static SortedSet synchronizedSortedSet(SortedSet s)返回由指定有序 set 支持的同步(线程安全的)有序 set。为了保证按顺序访问,必须通过返回的有序 set(或其视图)完成对底层有序 set 的所有访问。
package ThreadCollections;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortSetDemo{
public static void main(String[] args) {
//创建TreeSet集合
TreeSet<Integer> set=new TreeSet<Integer>();
set.add(4);
set.add(2);
set.add(3);
//创建集合安全的Set
Set syset=Collections.synchronizedSortedSet(set);
//线程同步进行迭代
synchronized(syset){
//创建迭代器
Iterator it=syset.iterator();
while(it.hasNext()) {
//对值进行排序
System.out.println(it.next());
}
}
}
}
以下类与Collection,List,Set,Map实现方法类似
类 ConcurrentHashMap<K,V>
支持检索的完全并发和更新的所期望可调整并发的哈希表。此类遵守与 Hashtable 相同的功能规范,并且包括对应于 Hashtable 的每个方法的方法版本。不过,尽管所有操作都是线程安全的,但检索操作不 必锁定,并且不 支持以某种防止所有访问的方式锁定整个表。此类可以通过程序完全与 Hashtable 进行互操作,这取决于其线程安全,而与其同步细节无关。
类 ConcurrentLinkedQueue
一个基于链接节点的、无界的、线程安全的队列。此队列按照 FIFO(先进先出)原则对元素进行排序。队列的头部 是队列中时间最长的元素。队列的尾部 是队列中时间最短的元素。新的元素插入到队列的尾部,队列检索操作从队列头部获得元素。当许多线程共享访问一个公共 collection 时,ConcurrentLinkedQueue 是一个恰当的选择。此队列不允许 null 元素。
类 CopyOnWriteArrayList
ArrayList 的一个线程安全的变体,其中所有可变操作(添加、设置,等等)都是通过对基础数组进行一次新的复制来实现的。
类 CopyOnWriteArraySet
- 它最适合于 set 大小通常保持很小、只读操作远多于可变操作以及需要在遍历期间防止线程间冲突的应用程序。
- 它是线程安全的。
- 因为通常需要复制整个基础数组,所以可变操作(添加、设置、移除,等等)的开销巨大。
- 迭代器不支持可变移除操作。
- 使用迭代器进行遍历的速度很快,并且不会与其他线程发生冲突。在构造迭代器时,迭代器依赖于不变的数组快照。