Java Multithreading: "Juc Atomic class" 02 Atomiclong Atomic class

Source: Internet
Author: User
Tags thread volatile

Atomiclong Introduction and Functions List

Atomiclong is the role of the atomic operation of the long plastic.

In a 32-bit operating system, the 64-bit long and double variables are not atomic because they are manipulated by the JVM as two separate 32 bits. Using Atomiclong, however, allows long operations to remain atomic in shape.

Atomiclong Function List

The constructor Atomiclong ()//Creates a Atomiclong object Atomiclong (Long InitialValue) with a value of InitialValue to set the current value to NewValue atomically. Final void set (long NewValue)//Gets the current value of final long get ()//to subtract the current value by 1 in atomic form and return the value after minus 1. Equivalent to the "--num" final long decrementandget ()//to subtract the current value by 1 in atomic terms and return the value before minus 1. Equivalent to "num--" final long getanddecrement () to add the current value by 1 in atomic terms and return the value plus 1. Equivalent to the "++num" final long incrementandget () to add the current value by 1 in atomic terms and return the value before plus 1.
Equivalent to the "num++" final long getandincrement ()//atomically adds the delta to the current value and returns the added value.
Final long Addandget (long delta)//The delta is added to the current value atomically, and the value before the addition is returned. Final long Getandadd (long delta)//If the current value = = expect, the value is set to update atomically.
Returns true successfully, otherwise it returns false and does not modify the original value.
Final Boolean compareandset (long expect, long update)//sets the current value to NewValue in atomic mode and returns the old value. Final long Getandset (long NewValue)//returns an int value of the current value int intvalue ()//Gets the current value's corresponding Long value long longvalue ()//is returned as float The preceding value float floatvalue ()//returns the current value double doublevalue ()//last set to the given value in double. Delay sets the value of the variable, which is equivalent to the set () method, but because the field is volatile type, the modification of this field will have a slightly slower performance delay than the normal field (not volatile field) (DoTube can be ignored), so this method is useful if you do not want to read the new values of the settings immediately, allowing you to modify the values in the background.
If it's still hard to understand, it's similar to starting a background thread such as executing a task that modifies a new value, and the original thread does not wait for the change to return immediately (an explanation that is incorrect, but understandable). Final void Lazyset (long newvalue)//If the current value = = expected value, the setting is atomic to the given update value. The JSR specification says: Read and conditionally write variables without creating any happen-before ordering, and therefore does not provide any guarantees related to previous or subsequent read or write operations on any variables except the Weakcompareandset target. The effect is that calling Weakcompareandset does not guarantee that there is no happen-before occurrence (that is, there may be a command reordering that is causing the 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 is equivalent, all called unsafe.compareandswapint () to complete the operation. Final Boolean weakcompareandset (long expect, long update)

Atomiclong Source Analysis (based on jdk1.7.0_40)

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.