• 线程安全Collections.synchronizedList


    ollections.synchronizedList引发的线程安全问题

    有些容器是线程安全的(Vector,ConcurrentLinkedQueue等),有些则不是(list等),利用类

    似 private static List<Task> taskQueue = Collections.synchronizedList(new 

    LinkedList<Task>());的方法可以得到本身不是线程安全的容易的线程安全的状态,但是要注意

    的是线程安全仅仅指的是如果直接使用它提供的函数,比如:queue.add(obj); 或者 

    queue.poll(obj);,这样我们自己不需要做任何同步。

    但如果是非原子操作,比如:

       1. if(!queue.isEmpty()) {  

       2.    queue.poll(obj);  

       3. }  

    所谓原子操作是指不会被线程调度机制打断的操作;这种操作一旦开始,就一直运行到结束,中间不会有任何 context switch (切换到另一个线程)

    我们很难保证,在调用了isEmpty()之后,poll()之前,这个queue没有被其他线程修改。

    所以对于这种情况,我们还是需要自己同步:

       1. synchronized(queue) {  

       2.     if(!queue.isEmpty()) {  

       3.        queue.poll(obj);  

       4.     }  

       5. }  

    对于Collections.synchronizedList(new LinkedList<Task>()),

    在返回的列表上进行迭代时,用户必须手工在返回的列表上进行同步:

    List list = Collections.synchronizedList(new ArrayList()); ... synchronized(list) { 

    Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) 

    foo(i.next()); }

  • 相关阅读:
    基于Python的人脸动漫转换
    let 与 var的区别
    【LeetCode】汇总
    【HDU】4632 Palindrome subsequence(回文子串的个数)
    【算法】均匀的生成圆内的随机点
    【LeetCode】725. Split Linked List in Parts
    【LeetCode】445. Add Two Numbers II
    【LeetCode】437. Path Sum III
    【LeetCode】222. Count Complete Tree Nodes
    【LeetCode】124. Binary Tree Maximum Path Sum
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/4384545.html
Copyright © 2020-2023  润新知