Java Multithreading: "Juc Atomic class" 05 of the Atomiclongfieldupdater Atom

Source: Internet
Author: User
Tags abstract volatile

Atomiclongfieldupdater Introduction and Functions List

Atomiclongfieldupdater can make atomic updates to members of the specified class's ' volatile long ' type. It is realized based on the principle of reflection.

Atomiclongfieldupdater Function List

A protected, no-action constructed method for use by subclasses.
protected Atomiclongfieldupdater ()//Adds the given value to the current value of the field in an atomic manner to the given object managed by this update.
Long Addandget (T obj, long delta)//If the current value = = expected value, the field of the given object managed by this update is atomically set to the given update value.
Abstract Boolean compareandset (T obj, long expect, long Update)//The current value of the given object field managed by this update is reduced by 1 in atomic mode.
Long Decrementandget (T obj)//Gets the current value maintained in the field of the given object managed by this update.
Abstract long get (T obj)//Adds the given value to the current value of the field in an atomic manner to the given object managed by this update.
Long Getandadd (T obj, long delta)//The current value of the given object field managed by this update is reduced by 1 in atomic mode.
Long getanddecrement (T obj)//atomically adds 1 to the current value of the given object field managed by this update.
Long getandincrement (T obj)//The field of the given object managed by this update is set atomically to the given value and the old value is returned.
Long Getandset (T obj, long newvalue)//The current value of the given object field managed by this update is added to 1 in atomic mode.
Long Incrementandget (T obj)//Finally sets the field of the given object managed by this update to the given update value.
abstract void Lazyset (T obj, long newvalue)//Creates and returns an update with the given field for the object. Static <U> atomiclongfieldupdater<u> newupdater (class<u> tclass, String fieldName)//
Sets the field of the given object managed by this update to the given update value.
abstract void Set (T obj, long newvalue)//If the current value = = expected value, the field of the given object managed by this update is atomically set to the given update value. AbstraCT boolean weakcompareandset (T obj, long expect, long update) 

Atomiclongfieldupdater sample

Longtest.java Source code
import Java.util.concurrent.atomic.AtomicLongFieldUpdater;
    
public class Longfieldtest {public
        
    static void Main (string[] args) {
    
        //Get the person's class object
        class CLS = Person.c Lass; 
        Creates a new Atomiclongfieldupdater object that passes the arguments "class object" and "long type corresponding name in class"
        atomiclongfieldupdater Matolong = Atomiclongfieldupdater.newupdater (CLS, "id");
        person person = new person (12345678L);
    
        Compare the id attribute of person to 1000 if the ID value is 12345678L.
        Matolong.compareandset (person, 12345678L, 1000);
        System.out.println ("id=" +person.getid ());
    }
    
Class Person {
    volatile long ID;
    Public person (long ID) {
        this.id = ID;
    }
    public void SetId (long id) {
        this.id = ID;
    }
    Public long GetId () {return
        ID;
    }
}

Related Article

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.