CopyOnWriteArraySet 与CopyOnWriteArraySet 的使用 高并发 读写分离



  


CopyOnWriteArrayList是ArrayList 的一个线程安全的变体,其中所有可变操作(add、set等等)都是通过对底层数组进行一次新的复制来实现的。

     这一般需要很大的开销,但是当遍历操作的数量大大超过可变操作的数量时,这种方法可能比其他替代方法 有效。在不能或不想进行同步遍历,但又需要从并发线程中排除冲突时,它也很有用。“快照”风格的迭代器方法在创建迭代器时使用了对数组状态的引用。此数组在迭代器的生存期内不会更改,因此不可能发生冲突,并且迭代器保证不会抛出ConcurrentModificationException。创建迭代器以后,迭代器就不会反映列表的添加、移除或者更改。在迭代器上进行的元素更改操作(remove、set和add)不受支持。这些方法将抛出UnsupportedOperationException。允许使用所有元素,包括null。

    内存一致性效果:当存在其他并发 collection 时,将对象放入CopyOnWriteArrayList之前的线程中的操作 happen-before 随后通过另一线程从CopyOnWriteArrayList中访问或移除该元素的操作。 

   这种情况一般在多线程操作时,一个线程对list进行修改。一个线程对list进行fore时会出现java.util.ConcurrentModificationException错误。

   下面来看一个列子:两个线程一个线程fore一个线程修改list的值。

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.lucky.concurrent.list;
 
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class CopyOnWriteArrayListDemo {
    /**
     * 读线程
     * @author wangjie
     *
     */
    private static class ReadTask implements Runnable {
        List<String> list;
 
        public ReadTask(List<String> list) {
            this.list = list;
        }
 
        public void run() {
            for (String str : list) {
                System.out.println(str);
            }
        }
    }
    /**
     * 写线程
     * @author wangjie
     *
     */
    private static class WriteTask implements Runnable {
        List<String> list;
        int index;
 
        public WriteTask(List<String> list, int index) {
            this.list = list;
            this.index = index;
        }
 
        public void run() {
            list.remove(index);
            list.add(index, "write_" + index);
        }
    }
 
    public void run() {
        final int NUM = 10;
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < NUM; i++) {
            list.add("main_" + i);
        }
        ExecutorService executorService = Executors.newFixedThreadPool(NUM);
        for (int i = 0; i < NUM; i++) {
            executorService.execute(new ReadTask(list));
            executorService.execute(new WriteTask(list, i));
        }
        executorService.shutdown();
    }
 
    public static void main(String[] args) {
        new CopyOnWriteArrayListDemo().run();
    }
}


运行结果:


从结果中可以看出来。在多线程情况下报错。其原因就是多线程操作结果:那这个种方案不行我们就换个方案。用jdk自带的类CopyOnWriteArrayList来做容器。这个类和ArrayList最大的区别就是add(E) 的时候。容器会自动copy一份出来然后再尾部add(E)。看源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
    * Appends the specified element to the end of this list.
    *
    * @param e element to be appended to this list
    * @return <tt>true</tt> (as specified by {@link Collection#add})
    */
   public boolean add(E e) {
   final ReentrantLock lock = this.lock;
   lock.lock();
   try {
       Object[] elements = getArray();
       int len = elements.length;
       Object[] newElements = Arrays.copyOf(elements, len + 1);
       newElements[len] = e;
       setArray(newElements);
       return true;
   } finally {
       lock.unlock();
   }
   }


用到了Arrays.copyOf 方法。这样导致每次操作的都不是同一个引用。也就不会出现java.util.ConcurrentModificationException错误。
换了种方案看代码:

?
1
2
//      List<String> list = new ArrayList<String>();
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();

也就把容器list换成了 CopyOnWriteArrayList,其他的没变。线程里面的list不用改。因为 CopyOnWriteArrayList实现的也是list<E> 接口。看结果:

其结果没报错。
CopyOnWriteArrayList add(E
) 和remove(int index)都是对新的数组进行修改和新增。所以在多线程操作时不会出现java.util.ConcurrentModificationException错误。
所以最后得出结论:CopyOnWriteArrayList适合使用在读操作远远大于写操作的场景里,比如缓存。发生修改时候做copy,新老版本分离,保证读的高性能,适用于以读为主的情况。




CopyOnWriteArraySet



  1. import java.util.concurrent.ConcurrentSkipListSet;  

  2. import java.util.concurrent.ExecutorService;  

  3. import java.util.concurrent.Executors;  

  4.   

  5. import org.junit.Test;  

  6.   

  7. /** 

  8.  * @author amber2012 

  9.  *  

  10.  * 1、ConcurrentSkipListSet<E>: 

  11.  *      public class ConcurrentSkipListSet<E> extends AbstractSet<E>  

  12.  *                      implements NavigableSet<E>, Cloneable, Serializable 

  13.  *  

  14.  *  关于ConcurrentSkipListSet<E>在jdk的API的文档说明: 

  15.  *      1)ConcurrentSkipListSet<E>是jdk6新增的类,位于java.util.concurrent并发库下; 

  16.  *  

  17.  *      2)ConcurrentSkipListSet<E>和TreeSet一样,都是支持自然排序,并且可以在构造的时候定义Comparator<E> 

  18.  *      的比较器,该类的方法基本和TreeSet中方法一样(方法签名一样); 

  19.  *  

  20.  *      3)和其他的Set集合一样,ConcurrentSkipListSet<E>都是基于Map集合的,ConcurrentSkipListMap便是它的底层实现; 

  21.  *  

  22.  *      4)在多线程的环境下,ConcurrentSkipListSet<E>中的contains、add、remove操作是安全的,多个线程可以安全地并发 

  23.  *      执行插入、移除和访问操作。但是对于批量操作 addAll、removeAll、retainAll 和 containsAll并不能保证以原子方式执行, 

  24.  *      理由很简单,因为addAll、removeAll、retainAll底层调用的还是contains、add、remove的方法,在批量操作时,只能保证 

  25.  *      每一次的contains、add、remove的操作是原子性的(即在进行contains、add、remove三个操作时,不会被其他线程打断),而 

  26.  *      不能保证每一次批量的操作都不会被其他线程打断。 

  27.  *       

  28.  *      5)此类不允许使用 null 元素,因为无法可靠地将 null 参数及返回值与不存在的元素区分开来。  

  29.  *  

  30.  * 2、public class CopyOnWriteArraySet<E>extends AbstractSet<E>implements Serializable 

  31.  *      对于CopyOnWriteArraySet<E>类: 

  32.  *      1)它最适合于具有以下特征的应用程序:set 大小通常保持很小,只读操作远多于可变操作,需要在遍历期间防止线程间的冲突。  

  33.  *      2)它是线程安全的, 底层的实现是CopyOnWriteArrayList;   

  34.  *      3)因为通常需要复制整个基础数组,所以可变操作(add、set 和 remove 等等)的开销很大。  

  35.  *      4)迭代器不支持可变 remove 操作。  

  36.  *      5)使用迭代器进行遍历的速度很快,并且不会与其他线程发生冲突。在构造迭代器时,迭代器依赖于不变的数组快照。  

  37.  *  

  38.  */  

  39. public class ConcurrentSkipListSetTest {  

  40.       

  41.     private static ConcurrentSkipListSet<Student> listSet = new ConcurrentSkipListSet<Student>();  

  42.       

  43.     static {  

  44.         for(int i = 1; i <= 30; i++){  

  45.             listSet.add(new Student(new Long(i)));  

  46.         }  

  47.     }  

  48.       

  49.     public static void main(String[] args) {  

  50.           

  51.         ExecutorService threadPool = Executors.newFixedThreadPool(3);  

  52.         threadPool.execute(new Runnable() {  

  53.             public void run() {  

  54.                 while(listSet.size() != 0){  

  55.                     sop(Thread.currentThread().getName()+ ” : ” + listSet.pollFirst()); //获取并移除第一个(最低)元素  

  56.                 }  

  57.             }  

  58.         });  

  59.           

  60.         threadPool.execute(new Runnable() {  

  61.             public void run() {  

  62.                 while(listSet.size() != 0){  

  63.                     sop(Thread.currentThread().getName()+ ” : ” + listSet.pollLast()); //获取并移除最后(最高)元素  

  64.                 }  

  65.             }  

  66.         });  

  67.           

  68.         threadPool.execute(new Runnable() {  

  69.             public void run() {  

  70.                 while(listSet.size() != 0){  

  71.                     sop(Thread.currentThread().getName()+ ” : ” + listSet.pollFirst()); //获取并移除第一个(最低)元素  

  72.                 }  

  73.             }  

  74.         });  

  75.     }  

  76.       

  77.     @Test  

  78.     public void test(){  

  79.     }  

  80.       

  81.     private static void sop(Object obj){  

  82.         System.out.println(obj);  

  83.     }  

  84. }  




猜你喜欢

转载自blog.csdn.net/qq_34531925/article/details/80859537