jdk source reading -ConcurrentLinkedQueue (a)

Explanation

concurrentLinkedQueue non-blocking queue is unbounded, is the internal structure of the thread-safe form of linked lists, cas save internal use thread-safe. Cas employed to ensure atomicity

What is CAS

CAS operation includes three operands - a memory location (V), is expected to original value (A) and the new value (B). If the memory location of the original value matches the expected value, then the processor will automatically update the location value to the new value. Otherwise, the processor does nothing

Examples AtomicInteger

= AtomicInteger of AtomicInteger new new of AtomicInteger ();
 // internal CAS guarantee atomicity by 
 atomicInteger.getAndIncrement ();
  private static final Unsafe unsafe = Unsafe.getUnsafe();
  public final int getAndIncrement() {
     return unsafe.getAndAddInt(this, valueOffset, 1);
  }
   public  Final  int getAndAddInt (Object var1, Long var2, int var4) {
         int var5;
         do { 
            var5 = the this .getIntVolatile (var1, var2);
             / ** 
             * var1 var2 object attribute value of the object to a desired value memory var5 var4 If the failure continues cas retry value modified 
             * objects removed from memory var1 var2 value memory value 
             * / 
        } the while (! the this .compareAndSwapInt (var1, var2, var5, var5 + var4)); 

        return var5 ; 
    }

 

Note: Unsafe is c ++ implementation is regrettable that this class is that we can not use the special jdk

Guess you like

Origin www.cnblogs.com/LQBlog/p/11607351.html
Recommended