Article From:https://www.cnblogs.com/jinjian91/p/9061481.html

copyOnWriteArray  Record

package java.util.concurrent;//You're not mistaken, it's this bag
 private transient volatile Object[] array;//Initialization arrayPublic CopyOnWriteArrayList () * / / constructor, initializationSetArray (New Object[0]);}Final voiD setArray (Object[] a) {Array = a;}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 ();}}Final Object[] getArray () {Return array;}
public E get(int index) {//Get a single array objectReturn get (getArray () (), index);}
private E get(Object[] a, int index) { return (E) a[index]; }

  addWhen the yoke is first copied and then assigned, there is no lock on the get, which may cause get to be less than the nearest value during multithreading.

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *