[Translation] Java Concurrent Atomic Package Detailed

Let my people encountered, the calendar of things, because even if I have a little bit better, I'll be satisfied.

Translated from: Package java.util.concurrent.atomic Address: docs.oracle.com/javase/8/do... translator reason JDK8 instead of 12 is JDK8 more detailed explanation of the semantic memory section.

Package java.util.concurrent.atomic for a single variable support lock-free thread-safe tools operation.

SUMMARY:

The class name description
AtomicBoolean Atoms can update boolean value.
AtomicInteger Int atoms can update the value.
AtomicIntegerArray An int array, wherein the atomic elements may be updated.
AtomicIntegerFieldUpdater Based on reflection, may be updated atoms designated volatile int fields of designated classes.
AtomicLong You can update atoms long value.
AtomicLongArray A long array in which the elements may atomic update.
AtomicLongFieldUpdater Based on reflection, may be updated atoms designated volatile long fields of designated classes.
AtomicMarkableReference Maintenance object with a reference flag can be updated atomically.
AtomicReference Atoms may update object reference
AtomicReferenceArray An object reference array, wherein the atomic elements may be updated.
AtomicReferenceFieldUpdater<T,V> Based on reflection, may be updated atoms designated volatile reference fields of designated classes.
AtomicStampedReference Maintenance of an object with a reference mark integer version can be updated atomically.
DoubleAccumulator One or more variables that together maintain a running double value updated using a supplied function.
DoubleAdder One or more variables that together maintain an initially zero double sum.
LongAccumulator One or more variables that together maintain a running long value updated using a supplied function.
Long Adder One or more variables that together maintain an initially zero long sum.

DoubleAccumulator, DoubleAdder, LongAccumulator, LongAdder Striped64 is a subclass of both, an array is maintained internally, when concurrent updates, each thread operating elements in an array, thereby reducing the lock granularity.

In essence, the extended field type, the value of volatile concept, the array element at Package, provided in the form of atomic update operation:

 boolean compareAndSet(expectedValue, updateValue);
复制代码

The method (a different parameter types in different classes) atomically sets the variable updateValue, successful returns true if it is currently holding expectedValue, update. The package further comprising a class and a method of acquiring unconditionally setting (set) value.

These specifications enable the method can be employed to achieve efficient machine-level instructions atoms contemporary processors available. However, on some platforms, support may require some form of internal locking. Thus, these methods are not guaranteed to be strictly non-blocking (thread may be temporarily blocked before the operation).

AtomicBoolean, AtomicInteger, AtomicLong AtomicReference class instance and provide access and update of a single corresponding type of the variable. Each class also provides a practical method applicable to this type. For example, class AtomicLong atoms and AtomicInteger provide incremental approach. One application is to generate a serial number, such as:

 class Sequencer {
   private final AtomicLong sequenceNumber = new AtomicLong(0);
   public long next() {
     return sequenceNumber.getAndIncrement();
   }
 }
复制代码

Atomic access and update memory effect, follow the same rules and volatiles, such as The Java Language Specification (17.4 Memory Model ) said:

  • get the same effect with volatile read
  • set and volatile write the same effect
  • lazySet effect as the write (allocate) a volatile variable [not write reorder before any write operations], but it may be a subsequent rearrangement operation [i.e., written in the previous synchronization operation or volatile possible for other the thread is not visible ].
  • compareAndSet and all other read and update operations (e.g. getAndIncrement) as variables read and write volatile memory has the same effect.

lazySet using Unsafe.putOrderedObject method, this method is useful for low delay of the code, it is possible to realize a non-blocking write, these will not be written in the Java JIT reorder instruction (instruction reordering), so that it quickly using storage - storage (store-store) barrier, rather than the slower memory - load (store-load) barrier, which is always used in the volatile write operation. this performance comes at a price, though cheap, that is, after writing the results will not be seen by other threads, and even its own thread, typically a few nanoseconds after the other threads to see, this time is relatively short, so the cost can be tolerated. Imagine the following scene: a volatile variable is set to null, so that the object is out of GC, volatile write is consumed relatively large (store-load barrier), but only putOrderedInt plus store-store barrier, the loss will be smaller.

Add lazySet method of reasons: bugs.java.com/bugdatabase...

After weakCompareAndSet JDK 9 Deprecated, we skipped herein.

Apart from Class represents a single value, Package also contains Updater class, it may be used to perform operations on a volatile field compareAndSet class. AtomicReferenceFieldUpdater, AtomicIntegerFieldUpdater AtomicLongFieldUpdater and are based on the reflection and provides access to the relevant field types. These are mainly used for atomic data structures, several volatile field of the same node (e.g., a tree node linked) atom separately updated. These classes provide greater flexibility in terms of how and when to use atomic updates, but at the expense of more awkward reflection-based settings, more convenient to use and weaker guarantees.

AtomicIntegerArray, AtomicLongArray AtomicReferenceArray classes and further extended to support atomic operation of these types of arrays. These classes also provide an array element volatile access semantics, which is not supported by ordinary array.

AtomicMarkableReference class single Boolean value associated with a reference. For example, the bit may be used in the data structure representing the referenced objects have been logically deleted. AtomicStampedReference class integer value associated with a reference. For example, this can be used to represent a series of numbers corresponding to the updated version.

Atoms principal construct designed for implementing non-blocking data structures and associated infrastructure classes. CompareAndSet general alternative method is not locked. Only when the object is critical updates are limited to a single variable, it applies.

Alternatively atoms generic class is not java.lang.Integer and related classes. They do not define equals, hashCode, and the like compareTo (expected due atoms variables will change, so they are not suitable as a hash table keys).

Follow-up will be a text analysis: AtomicLong, Striped64, LongAdder purpose of this article is mainly from the general understanding of atomic and semantic memory

JDK 12 docs.oracle.com/en/java/jav…

Personal micro-channel public number:

Personal CSDN blog:

blog.csdn.net/jiankunking

Personal Nuggets blog:

juejin.im/user/5d3192…

Personal github:

github.com/jiankunking

personal blog:

www.jiankunking.com

Guess you like

Origin juejin.im/post/5d497916f265da03de3aea79