Atomic class of jdk (preliminary)

    Before introducing the jdk atomic class, let's introduce a very important knowledge point: the Unsafe class

1: What is Unsafe

     Java itself cannot directly access the underlying operating system. What should I do if I want to access it? The Unsafe class is precisely this capability, which can be directly atomic and provide this capability at the hardware level.

    The Unsafe class enables Java to have the ability to manipulate memory space like pointers in C language, but it also brings the problem of pointers. Excessive use of the Unsafe class will increase the probability of errors, so it is not recommended by Java officials, and there are almost no official documents. In actual coding, we cannot use it, otherwise an exception will be reported.

    Although Java is not officially recommended to be used, we should understand what capabilities it has, which will be of great help to our later code study.

2: Unsafe function (only list the main ones we need to know)

2.1: Object instantiation.

      The common way is that we use new or reflection to instantiate objects. Unsafe uses the allocateInstance() method to directly generate object instances without calling constructors and other initialization methods.

2.2: Manipulate classes, objects, and variables.

   This part includes staticFieldOffset (static domain offset), defineClass (defined class), defineAnonymousClass (defined anonymous class), ensureClassInitialized (ensure class initialization), objectFieldOffset (object domain offset) and other methods.

        Through these methods, we can obtain the pointer of the object, and by offsetting the pointer, we can not only directly modify the data pointed to by the pointer (even if they are private), but even find the object that the JVM has identified as garbage and can be recycled.

2.3: Manipulate arrays.

    This part includes methods such as arrayBaseOffset (getting the offset address of the first element of the array), arrayIndexScale (getting the incremental address of the elements in the array). ArrayBaseOffset is used in conjunction with arrayIndexScale to locate the location of each element in the array in memory.

2.4: CAS operation

    The CAS ( compareAndSwap comparison replacement ) operation of the Unsafe class is probably the most used. It provides a new solution for Java's locking mechanism. For example, classes such as AtomicXXX are implemented by this method.

2.5: Thread suspend and resume.

A thread is suspended       by the park method . After calling park, the thread will be blocked until a timeout or interruption occurs. unpark can terminate a suspended thread and bring it back to normal. The suspending operation of threads in the entire concurrency framework is encapsulated in the LockSupport class (detailed when introducing locks later).


After figuring out what Unsafe is and what capabilities it has, we can start our "Atomic Class Journey"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324729244&siteId=291194637