Java Multithreading Series---Juc Atom Class (ii) Atomiclong Atomic class

Source: Internet
Author: User
Tags cas volatile

Overview

Atomicinteger, Atomiclong and Atomicboolean are similar in principle and usage to the 3 basic types of atomic classes. This chapter introduces the basic types of atomic classes with Atomiclong.

Atomiclong Introduction and Function List

Atomiclong is a function of atomic manipulation of long-shaped shapes.
In 32-bit operating systems, the 64-bit long and double variables are not atomic because they are manipulated by the JVM as a two separated 32-bit. The use of Atomiclong allows long operations to remain atomic.

List of Atomiclong functions

The constructor Atomiclong ()//Creates a Atomiclong object with a value of InitialValue Atomiclong (long InitialValue)//atomically sets the current value to NewValue. Final void set (long NewValue)//Gets the current value final long get ()//atomically subtract the current value by 1 and returns the value minus 1. Equivalent to "--num" final long decrementandget ()//atomically subtract the current value by 1 and return the value before minus 1. Equivalent to "num--" final long getanddecrement ()//atomically adds 1 to the current value and returns the value after 1. Equivalent to "++num" final long incrementandget ()//atomically adds 1 to the current value and returns the value before the plus 1. Equivalent to "num++" final long getandincrement ()//atomically adds the delta to the current value and returns the added value. The final long addandget (long delta)//atomically adds the delta to the current value and returns the value before it is added. Final long Getandadd (long delta)//If the current value = = expect, the value is atomically set to update. The success returns TRUE, otherwise false is returned, and the original value is not modified. Final Boolean compareandset (long expect, long update)//atomically sets the current value to NewValue and returns the old value. Final long Getandset (long NewValue)//returns the int value corresponding to the current value int intvalue ()//Gets the long value corresponding to the current value long longvalue ()//returns the current value in float form Flo At Floatvalue ()//returns the current value in double form double doublevalue ()//last set to the given value. The delay sets the value of the variable, which is equivalent to the set () method, but because the field is volatile, the modification of the field will have a slight performance delay (albeit negligible) than the normal field (non-volatile field), so if you do not want to read the new value of the setting immediately, allow the "background" Modify the value, thenThis method is useful. If it is still difficult to understand, it is similar to starting a background thread, such as performing a task to modify the new value, the original thread does not wait for the result of the modification to return immediately (this explanation is actually incorrect, but it is understood). Final void Lazyset (long newvalue)//If the current value = = expected value, the setting is atomically set to the given update value. The JSR specification reads and conditionally writes a variable without creating any happen-before ordering, and therefore does not provide any guarantees relating to previous or subsequent read or write operations other than the Weakcompareandset target. The main idea is that when calling Weakcompareandset, there is no guarantee that no happen-before will occur (that is, there may be a command reordering that causes this operation to fail). However, from the Java source, in fact, this method does not implement the requirements of the JSR specification, the final effect and compareandset are equivalent, all call the Unsafe.compareandswapint () to complete the operation. Final Boolean weakcompareandset (long expect, long update)

Atomiclong Source Analysis (based on jdk1.7.0_40)

Atomiclong code is very simple, the following is only Incrementandget () as an example, the principle of Atomiclong is described.
Incrementandget () source code is as follows:

Public final long Incrementandget () {for    (;;) {        //Get Atomiclong current corresponding Long value        long = Get ();        Add current to 1        long next = current + 1;        Update the value of current with the CAS function        if (compareandset (current, next))            return to Next;}    }

Description :
Incrementandget () First gets the long value corresponding to Atomiclong based on Get (). The value is a variable of type volatile, and the source of Get () is as follows:

Value is a long value corresponding to Atomiclong, private volatile long value;//returns atomiclong corresponding Long value public final long get () {    return Value;}

Incrementandget () then adds 1 to current and then assigns the new value to value through the CAS function.
The source code of Compareandset () is as follows:

Public Final Boolean compareandset (long expect, long update) {    return Unsafe.compareandswaplong (this, Valueoffset, expect, update);}

The function of Compareandset () is to update the atomiclong corresponding Long value. It compares whether the original value of Atomiclong is equal to expect, and if so, sets the value of Atomiclong to update.

Atomiclong Example
 1//Longtest.java source 2 import Java.util.concurrent.atomic.AtomicLong;         3 4 public class Longtest {5 6 public static void main (string[] args) {7 8//New Atomiclong Object 9  Atomiclong Matolong = new Atomiclong (); Matolong.set (0X0123456789ABCDEFL); System.out.printf ("%20s : 0x%016x\n "," Get () ", Matolong.get ()), System.out.printf ("%20s:0x%016x\n "," Intvalue () ", Matolong.intvalue ()  ); System.out.printf ("%20s:0x%016x\n", "Longvalue ()", Matolong.longvalue ()), System.out.printf ("%20s :%s\n "," Doublevalue () ", Matolong.doublevalue ()), System.out.printf ("%20s:%s\n "," Floatvalue () ", MATOLONG.FL         Oatvalue ()); System.out.printf ("%20s:0x%016x\n", "getanddecrement ()", matolong.getanddecrement ()); 19 System.out.printf ("%20s:0x%016x\n", "Decrementandget ()", Matolong.decrementandget ()), System.out.printf ("%20       S:0x%016x\n "," getandincrement () ", matolong.getandincrement ()); 21  System.out.printf ("%20s:0x%016x\n", "Incrementandget ()", Matolong.incrementandget ()), and System.out.printf (" %20s:0x%016x\n "," Addandget (0x10) ", Matolong.addandget (0x10)), System.out.printf ("%20s:0x%016x\n "," Getandad         D (0x10) ", Matolong.getandadd (0x10)); System.out.printf (" \n%20s:0x%016x\n "," Get () ", Matolong.get ()); 27 28         System.out.printf ("%20s:%s\n", "Compareandset ()", Matolong.compareandset (0x12345679l, 0xfedcba9876543210l)); 29 System.out.printf ("%20s:0x%016x\n", "Get ()", Matolong.get ()); 30}31}

Operation result :

               Get (): 0x0123456789abcdef          intvalue (): 0x0000000089abcdef         longvalue (): 0x0123456789abcdef       Doublevalue ( ): 8.1985529216486896E16        floatvalue (): 8.1985531E16   getanddecrement (): 0x0123456789abcdef   Decrementandget (): 0x0123456789abcded   getandincrement (): 0x0123456789abcded   incrementandget (): 0x0123456789abcdef     Addandget (0x10): 0X0123456789ABCDFF     Getandadd (0x10): 0X0123456789ABCDFF               get (): 0x0123456789abce0f     Compareandset (): false               Get (): 0x0123456789abce0f

Reprint: http://www.cnblogs.com/skywang12345/p/3514593.html

Java Multithreading Series---Juc Atom Class (ii) Atomiclong Atomic class

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.