首页 > 学院 > 开发设计 > 正文

线程安全的类

2019-11-06 07:34:51
字体:
来源:转载
供稿:网友

线程安全的类

A:StringBuffer

B:Vector

C:Hashtable

D:如何把一个线程不安全的集合类变成一个线程安全的集合类

用Collections工具类的方法即可。

static 

<T> Collection<T>

 

synchronizedCollection(Collection<T> c)           返回指定 collection 支持的同步(线程安全的)collection。

static 

<T> List<T>

 

synchronizedList(List<T> list)           返回指定列表支持的同步(线程安全的)列表。

static 

<K,V> Map<K,V>

 

synchronizedMap(Map<K,V> m)           返回由指定映射支持的同步(线程安全的)映射。

static 

<T> Set<T>

 

synchronizedSet(Set<T> s)           返回指定 set 支持的同步(线程安全的)set。

 

 

Eg:

// Vector是线程安全的时候才去考虑使用的,但是我还说过即使要安全,我也不用你// 那么到底用谁呢?// public static <T> List<T> synchronizedList(List<T> list)List<String> list1 = new ArrayList<String>();// 线程不安全List<String> list2 = Collections.synchronizedList(new ArrayList<String>()); // 线程安全


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表