同步类容器

        同步类容器,都是线程安全的,但是在某些场景下需要枷锁来保护复合操作,符合类操作:迭代(反复访问元素,遍历完成容器中的所有元素),跳转(根据指定的顺序找到当前元素的下一个元素)、以及条件运算。这些复合操作在多线程并发的修改容器时,可能会表现出意外的行为,最经典的是:ConcurrentModificantionException。原因是当容器迭代的过程中,被并发的修改了内容,这是由于早期迭代器设计的时候并没有考虑并发修改的问题;

       同步类容器:如古老的Vector、Hashtable,这类容器的同步功能其实都是有JDK的Conllections.synchronized...等工厂方法去创建实现的。其实底层的机制无非就是使用传统的synchronized关键字对每个公用的方法都进行同步,是的每次只能有一个线程访问容器的状态,这很明显不能满足我们今天互联网时代的并发要求,在保证线程安全的时候,也必须要有足够的性能;

下面的代码会出现:ConcurrentModificantionException

package com.taikang.demo01;


import java.util.Iterator;
import java.util.Vector;


public class Tickets {


public static void main(String[] args) {
final Vector<String> tickets = new Vector<>();

for (int i = 0; i < 100; i++) {
tickets.add("彦彦" + i);
}

for (Iterator iterator = tickets.iterator(); iterator.hasNext();) {
String string = (String)iterator.next();
tickets.remove(20);
}
}
}


如果在单线程实现安全的话,我们可以通过一个公共类的方法:
List<String> list = Collections.synchronizedList(new LinkedList<String>());
Map<String, String> map = Collections.synchronizedMap(new HashMap<String,String>());
通过这样情况,就得到了一个安全的容器;


下面,我们来看看Collections.synchronizedList(new LinkedList<String>());源码:

    /**
     * Returns a synchronized (同步)(thread-safe) list backed by the specified(指定

返回由指定列表支持的同步(线程安全)list
     * list.  In order to guarantee(保证) serial access(串行访问或者连续的访问), it is critical(危险) that
     * <strong>all</strong> access to the backing list is accomplished(完成的)
     * through the returned list.<p>
     *
     * It is imperative that the user manually synchronize on the returned
     * list when iterating over it:

     在迭代时,用户必须在返回的列表上手动同步:
     * 
     *  List list = Collections.synchronizedList(new ArrayList());
     *      ...
     *  synchronized (list) {
     *      Iterator i = list.iterator(); // Must be in synchronized block
     *      while (i.hasNext())
     *          foo(i.next());
     *  }
     * </pre>
     * Failure to follow this advice may result in non-deterministic behavior.
     *
     * <p>The returned list will be serializable if the specified list is
     * serializable.
     *
     * @param  list the list to be "wrapped" in a synchronized list.
     * @return a synchronized view of the specified list.
     */
    public static <T> List<T> synchronizedList(List<T> list) {
        return (list instanceof RandomAccess ?
                new SynchronizedRandomAccessList<>(list) :
                new SynchronizedList<>(list));
    }


    static <T> List<T> synchronizedList(List<T> list, Object mutex) {
        return (list instanceof RandomAccess ?
                new SynchronizedRandomAccessList<>(list, mutex) :
                new SynchronizedList<>(list, mutex));
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值