java同步包装器与线程安全

原文来自:http://trophy.iteye.com/blog/1145078

同步线程包装器是为“原集合对象”的实际操作找一个代理对象,代理在“原集合对象”的一切功能之上又增加了同步功能(只是对这个“代理对象”上的操作同步,“原集合对象”上的操作非同步)。

java同步线程包装器:

public static Collection synchronizedCollection(Collection c);
public static Set synchronizedSet(Set s);
public static List synchronizedList(List list);
public static Map synchronizedMap(Map m);
public static SortedSet synchronizedSortedSet(SortedSet s);
public static SortedMap synchronizedSortedMap(SortedMap m);

java的同步线程包装器是有条件的同步,只有对集合的原子粒度的操作才同步。对于有并发情况的迭代操作,因为迭代操作是通过对对像集的调用间接操作原对像,所以在迭代时要对迭代的对像实现再同步:

 

 Collection c = Collections.synchronizedCollection(myCollection);
     ...
  synchronized(c) {
      Iterator i = c.iterator(); // Must be in the synchronized block
      while (i.hasNext())
         foo(i.next());
  }

 

 

 

 

线程安全的集合包含2个问题 
1.多线程并发修改一 个 集合 怎么办? 
2.如果迭代的过程中 集合 被修改了怎么办?
    a.一个线程在迭代,另一个线程在修改
    b.在同一个线程内用同一个迭代器对象进行迭代、修改、迭代、修改. . .

共有有3种解决方案 
1.用老的Vector/Hashtable类,上面2个问题都不用担心。
Vector/Hashtable所提供的所有方法都是synchronized的。vector的线程安全是指它的get,add等方法,是通过synchronized修饰的,可以线程安全的修改和读取单个数据。 但vector 的 iterator 方法返回的迭代器是非线性安全的:如果在迭代器创建后的任意时间从结构上修改了vector,则迭代器将抛出concurrentmodificationexception;迭代器的线程安全性与vector的线程安全性无关。 但这种方法效率低下,不建议使用。

2.使用ArrayList/HashMap和同步包装器

可用 同步包装器使容器变成线程安全的

Java代码

List synchArrayList = Collections.synchronizedList(new ArrayList());
Map synchHashMap = Collections.synchronizedMap(new HashMap())

如果要迭代,出于iterator的非线程安全性,需要这样

Java代码

synchronized (synchHashMap){
Iterator iter = synchHashMap.keySet().iterator();
while (iter.hasNext()) . . .;
}

collections.synchronizedlist(new arraylist<student>())返回的包装类,跟vector情况类似,get,add等方法都是线程安全的,但iterator,返回的其实是被包装对象的iterator,因为被包装的arraylist的iterator是快速失败的,所以这种情况也是会抛concurrentmodificationexception异常的。

3.用java5.0新加入的ConcurrentLinkedQueue、ConcurrentHashMap、CopyOnWriteArrayList 和 CopyOnWriteArraySet
对这些集合进行并发修改是安全的。concurrentlinkedqueue的iterator不会抛concurrentmodificationexception,并保证能遍历构造这个iterator时,队列里的元素,但不保证能反映后续的修改。

 

 

举例入下:

设置非常简单的一个场景,有一个student集合,对该集合采用迭代器进行遍历之后,又对其添加了一个元素。由于添加元素是改变集合结构的操作,所以集合如果在迭代器构造之后发生改变,就会抛出concurrentmodificationexception异常。
    student类代码,很简单:
[code=java]
class student{
public student(string name, int age){
this.name = name;
this.age = age;
}
string name;
int age;

public string  tostring(){
return "i am "+name+" , "+age+" years old.";
}
}

    1. 创建一个修改collection的线程,实现runnable接口。不采取任何同步措施。
[code=java]
class modifycollectiontask implements runnable{
public modifycollectiontask(collection<student> slist){
this.slist = slist;
}
public void run(){
// 遍历学生列表,
for(student s : slist){
system.out.println(thread.currentthread().getname());
system.out.println(s);
}
// 向学生列表添加元素
slist.add(new student("katie", 30));
}
collection<student> slist;
}


在main函数里启动100个线程,
[code=java]
public class synccollection {
public static void main(string[] args) {

collection<student> slist = new arraylist<student>();
slist.add(new student("aaa",10));
slist.add(new student("bbb",12));
slist.add(new student("ccc",14));
slist.add(new student("ddd",16));
slist.add(new student("eee",18));

for(int i=0;i<100;i++){
new thread(new modifycollectiontask(slist)).start();
}
}

很明显,没有同步控制,那么很快就抛出了concurrentmodificationexception异常

    2. 由于vector类是线程安全的动态数组,所以,将集合实现改为vector,在线程run方法中没做任何修改
[code=java]
// 使用vector
list<student> svector = new vector<student>();
svector.add(new student("aaa",10));
svector.add(new student("bbb",12));
svector.add(new student("ccc",14));
svector.add(new student("ddd",16));
svector.add(new student("eee",18));

for(int i=0;i<100;i++){
new thread(new modifycollectiontask(svector)).start();
}

结果还是发生了concurrentmodificationexception异常。这让我有点怀疑vector的同步机制。

    3. 使用collections工具类中的同步包装方法,将线程不安全arraylist进行包装,而线程实现方法没有改动
[code=java]
// 使用collections工具类中的同步包装器
list<student> slist2 = collections.synchronizedlist(new arraylist<student>());
slist2.add(new student("aaa",10));
slist2.add(new student("bbb",12));
slist2.add(new student("ccc",14));
slist2.add(new student("ddd",16));
slist2.add(new student("eee",18));

for(int i=0;i<100;i++){
new thread(new modifycollectiontask(slist2)).start();
}

    结果还是发生了异常,不明白。。。

    4. 下面使用synchronized关键字进行同步控制,对线程实现代码进行了修改
[code=java]
class syncmodifylisttask implements runnable{
public syncmodifylisttask(collection<student> slist){
this.slist = slist;
}
public void run(){
synchronized(slist){
// 遍历学生列表
for(student s : slist){
system.out.println(thread.currentthread().getname());
system.out.println(s);
}
// 向学生列表添加元素
slist.add(new student("katie", 30));
}
}
collection<student> slist;
}

由于有了synchronized关键字对代码片段进行了保护,所以没有出现异常

    5. 使用java.util.concurrent包中的高效的同步集合, concurrentlinkedqueue,线程实现代码还是用modifycollectiontask
[code=java]
collection<student> concurrentcollection = new concurrentlinkedqueue<student>();
concurrentcollection.add(new student("aaa",10));
concurrentcollection.add(new student("bbb",12));
concurrentcollection.add(new student("ccc",14));
concurrentcollection.add(new student("ddd",16));
concurrentcollection.add(new student("eee",18));

for(int i=0;i<100;i++){
new thread(new modifycollectiontask(concurrentcollection)).start();
}

结果也没有出现异常,证明高效的同步集合还是很给力的!

 

参考页面:http://www.haowen.it/question/23060

感谢作者!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值