Article From:https://www.cnblogs.com/coffees/p/9967704.html

Learning Java requires a complete set of learning lines, need to be organized. Now it has been a while to learn java, from the original oblivious state gradually improved, but also gradually formed the habit of writing technical learning notes. Today, I summarize the atomic operation in java.

Picture 1

  1、JavaWhat’s the atomic operation in it?

  Atomic operations are operations that are not interrupted by thread scheduling mechanisms; once they start, they run to the end and switch to another thread in the middle.

  2、JavaIntroduction to Atomic Operations in uuuuuuuuuuu

  jdk1.5The package is java.util.concurrent.atomic.

  This package contains a set of atomic classes. Its basic feature is exclusivity in multi-threaded environment when multiple threads simultaneously execute the methods contained in instances of these classes.

  That is, when a thread enters a method and executes its instructions, it will not be interrupted by other threads, while other threads, like locks, wait until the method is executed before JVM chooses another thread from the waiting queue to enter. This is only a logical understanding. In fact, it’s based on hardware instructions.Implemented, but not blocked threads (synchronized will hang other waiting threads, or just blocked at the hardware level).

  The classes can be divided into four groups

  - AtomicBoolean,AtomicInteger,AtomicLong,AtomicReference

  - AtomicIntegerArray,AtomicLongArray

  - AtomicLongFieldUpdater,AtomicIntegerFieldUpdater,AtomicReferenceFieldUpdater

  - AtomicMarkableReference,AtomicStampedReference,AtomicReferenceArray

  AtomicRole of class

  - So that the operation of single data can be atomized.

  - Using Atomic classes to build complex, non-blocking code

  - Access to two or more atomic variables (or two or more operations on a single atomic variable) is generally considered to be synchronized to enable these operations to be treated as an atomic unit.

  AtomicBoolean , AtomicInteger, AtomicLong, AtomicReference These four basic types are used to process Boolean, integer, long integer and object data.

  - Constructor (two constructors)

  - Default constructors: initialized data are false, 0, 0, null, respectively

  - Parametric constructors: data whose parameters are initialized

  - set( )And get (method): atomic data can be set and acquired atomically. Similar to volatile, ensure that data is set or read in main memory

  - getAndSet( )Method

  - The atom sets the variable to new data and returns the previous old data.

  - Its essence is to get and then set. Although both operations are atomic, when they are combined, they are not atomic. At the Java source level, it’s done without relying on synchronized mechanismsIt is impossible to do this job. Native method is the only way to do this.

  - compareAndSet( ) And weakCompareAndSet (method)

  - Both methods are conditional modifier methods. These two methods accept two parameters, one is expected data and the other is new data. If the data in atomic is consistent with the expected data, the new data will be set.For atomic data, return true, indicating success; otherwise, no settings, and return false.

  - Special methods are also provided for Atomic Integer and Atomic Long. GetAndIncrement, incrementAndGet, getAndDecrement, decreMentAndGet (addAndGet), getAndAdd (getAndAdd) to achieve some addition, subtraction atomic operations. (Note that — i, ++ I is not an atomic operation, which contains three steps: first, read i; second, add 1 orReduce 1; Step 3: Write back to memory.

Leave a Reply

Your email address will not be published. Required fields are marked *