Java Multithreading: "Juc Atomic class" 03 Atomiclongarray Atomic Class

Source: Internet
Author: User
Tags tostring

Atomiclongarray Introduction and Functions List

In the "Java Multithreaded Series-" Juc Atomic class "02 Atomiclong Atomic Class", Atomiclong is the role of the operation of the long plastic to atomic operations. The role of Atomiclongarray is to perform atomic operations on the "long Plastic array".

Atomiclongarray Function List

Creates a new atomiclongarray of the given length.
atomiclongarray (int length)
//Creates a new atomiclongarray with the same length as the given array and copies all its elements from the given array.
Atomiclongarray (long[] array)
    
//The element that adds the given value to the index I in atomic mode.
long Addandget (int i, long delta)
//If the current value = = expected value, set the value to the given update value atomically.
boolean compareandset (int i, long expect, long update)
//The element of index i is reduced by 1 in atomic form.
long decrementandget (int i)
//Get the current value of position I. The
long get (int i)
//Is the atom that adds the given value to the element of index I.
long Getandadd (int i, long delta)
//The element of index i is reduced by 1 in atomic form.
long getanddecrement (int i)
//The element of index i is added by 1 in atomic form.
long getandincrement (int i)
//sets the element of position I to the given value in atomic mode and returns the old value.
long Getandset (int i, long newvalue)
//The element of index I is added in atomic mode by 1.
long incrementandget (int i)
//finally sets the element of position I to the given value.
void Lazyset (int i, long newvalue)
//Returns the length of the array.
int Length ()
//sets the element of position I to the given value.
void Set (int i, long newvalue)
//Returns the string representation of the current value of the array.
String toString ()
///if current value = = expected value, set the value to the given update value atomically.
boolean    weakcompareandset (int i, long expect, long update)

Atomiclongarray Source Analysis (based on jdk1.7.0_40)

Atomiclongarray full Source code

* * ORACLE proprietary/confidential.
 Use are subject to license terms. * * * * * * * * * * * * * * * * * * * * * * * * * * * written Ce from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * Http://creativecommon
s.org/publicdomain/zero/1.0/* * Package java.util.concurrent.atomic;
Import Sun.misc.Unsafe;
    
Import java.util.*;
 /** * A {@code long} array in which elements the May is updated atomically. * The {@link java.util.concurrent.atomic} Package Specification * For description of the properties of atomic variabl
 Es. * @since 1.5 * @author Doug Lea/public class Atomiclongarray implements java.io.Serializable {private static fin
    
    Al long serialversionuid = -2308431214976778248l;
    private static final Unsafe Unsafe = Unsafe.getunsafe ();
    private static final int base = Unsafe.arraybaseoffset (Long[].class);
    private static final int shift; PrivaTe final long[] array;
        static {int scale = Unsafe.arrayindexscale (Long[].class);
        if ((Scale & (scale-1))!= 0) throw new Error ("Data type scale not a power of two");
    Shift = 31-integer.numberofleadingzeros (scale); Private long Checkedbyteoffset (int i) {if (I < 0 | | I >= array.length) throw new Ind
    
        Exoutofboundsexception ("index" + i);
    return Byteoffset (i);
    private static long byteoffset (int i) {return ((long) I << shift) + base;
     }/** * Creates a new atomiclongarray of the given length, with all * elements initially zero. * * @param length The length of the array */public atomiclongarray (int length) {array = new long[l
    Ength]; }/** * Creates a new atomiclongarray with the same length as, and * all elements copied from, the Give
     N Array. * @param array the array to Copy elements from * @throws nullpointerexception if array is null/public Atomiclongarray (long[] array) {
    Visibility guaranteed by final field guarantees This.array = Array.clone ();
     }/** * Returns the length of the array.
    * * @return The length of the array */public final int length () {return array.length;
     }/** * Gets the current value at position {@code i}. * * @param i the index * @return the current value */public final long get (int i) {return get
    Raw (Checkedbyteoffset (i));
    Private long Getraw (long offset) {return unsafe.getlongvolatile (array, offset);
     }/** * Sets the element at position {@code I} to the given value. 
        * * @param i the index * @param newvalue The new value */public final void set (int i, long newvalue) { Unsafe.putlongvolatile (Array, Checkedbyteoffset (i), newvalUE);
     }/** * Eventually sets the element at position {@code I} to the given value. * * @param i the index * @param newvalue The new value * @since 1.6/public final void Lazyset (i
    NT I, long NewValue) {Unsafe.putorderedlong (array, Checkedbyteoffset (i), newvalue);  }/** * Atomically sets the element at position {@code I} to the given value * and returns the old
     Value. * * @param i the index * @param newvalue The new value * @return The previous value/public FINA
        L Long Getandset (int i, long newvalue) {Long offset = checkedbyteoffset (i);
            while (true) {long = Getraw (offset);
        if (Compareandsetraw (offset, current, newvalue)) return to current; }/** * Atomically sets the element at position {@code I} to the given * updated value if the cur
  Rent value {@code = =} The expected value.   * * @param i the index * @param expect the expected value * @param update the new value * @return t Rue if successful.
     False return indicates that * the actual value is not equal to the expected value. */Public final Boolean compareandset (int i, long expect, long update) {return Compareandsetraw checkedbyteof
    Fset (i), expect, update); Private Boolean Compareandsetraw (long offset, long expect, long update) {return UNSAFE.COMPAREANDSWAPL
    Ong (array, offset, expect, update); }/** * Atomically sets the element at position {@code I} to the given * updated value if the current V
     alue {@code = =} The expected value. * * <p>may <a href= "package-summary.html#spurious" >fail spuriously</a> * and does not provid
     E ordering guarantees, so are only rarely a * appropriate alternative to {@code Compareandset}. * * @param i the index * @param expect the EXPECTED value * @param update the new value * @return true if successful. * Public Final Boolean weakcompareandset (int i, long expect, long update) {return Compareandset (I, expect, u
    Pdate);
     /** * atomically increments by one of the element at index {@code i}.
        * * @param i the index * @return the previous value */public final long getandincrement (int i) {
    Return Getandadd (i, 1);
     /** * atomically decrements by one of the element at index {@code i}.
        * * @param i the index * @return the previous value */public final long getanddecrement (int i) {
    Return Getandadd (I,-1);
     }/** * Atomically adds the given value to the element at index {@code i}. * * @param i the index * @param delta the value to add * @return the previous value */public FINA
  L Long Getandadd (int i, long delta) {Long offset = checkedbyteoffset (i);      while (true) {long = Getraw (offset);
        if (Compareandsetraw (offset, current, current + Delta)) return to current;
     }/** * atomically increments by one of the element at index {@code i}.
        * * @param i the index * @return the updated value */public final long incrementandget (int i) {
    Return Addandget (i, 1);
     /** * atomically decrements by one of the element at index {@code i}.
        * * @param i the index * @return the updated value */public final long decrementandget (int i) {
    Return Addandget (I,-1);
     }/** * Atomically adds the given value to the element at index {@code i}. * * @param i the index * @param delta the value to add * @return The updated value */public long
        Addandget (int i, long delta) {Long offset = checkedbyteoffset (i); while (true) {long =Getraw (offset);
            Long next = current + Delta;
        if (Compareandsetraw (offset, current, next)) return to next;
     }/** * Returns The String representation of the current values of array.  * @return The string representation of the current values of array */public String toString () {int IMax
        = Array.length-1;
    
        if (IMax = = 1) return "[]";
        StringBuilder B = new StringBuilder ();
        B.append (' [');
            for (int i = 0;; i++) {B.append (Getraw (Byteoffset (i)));
            if (i = = IMax) return b.append ('] '). toString ();
        B.append (', '). Append ('); }
    }
    
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.