is the "Practical Java High concurrency program design," The 4th chapter of several points.
If you have a unhesitatingly pursuit of technology, you should also be particularly concerned about the implementation of Compareandset () in the Incrementandget () method. Now, let's take a closer look at it!
Public Final Boolean compareandset (intint update) { Returnunsafe.compareandswapint (This, Valueoffset, expect, update);}
Here, we see a special variable, unsafe. It is the Sun.misc.Unsafe type. By name, this class should encapsulate some unsafe operations. So what's not safe to do? If you have studied C or C + +, you should know that pointers are unsafe. This is also an important reason to remove pointers in Java. If the pointer is in the wrong position, or if there is an error in calculating the pointer offset, the result can be catastrophic, and you are likely to overwrite someone else's memory, causing the system to collapse.
And the unsafe here is to encapsulate a few pointers-like operations. The Compareandswapint () method is a Navtive method. Several of its parameters have the following meanings:
Public Final native boolean compareandswapint (Object o,long offset,int expected,int x);
The first parameter, O, is the offset within the object for the given object (in fact, it is the offset of a field to the object's head, which can be quickly positioned by this offset), expected represents the expected value, and x represents the values to set. If the value of the specified field is equal to expected, it is set to X.
It is not difficult to see that the interior of the Compareandswapint () method must be done using CAS atomic directives. In addition, the unsafe class provides several methods, mainly the following (in the case of the int operation, the other data types are similar):
//gets the int value on the offset of the given object Public native intGetInt (Object o,Longoffset);//sets the int value on the offset of the given object Public native voidPutint (Object o,LongOffsetintx);//get the offset of a field in an object Public native LongObjectfieldoffset (Field f);//sets the int value of the given object, using the volatile semantics Public native voidPutintvolatile (Object o,LongOffsetintx);//gets the int value of the given object object, using the volatile semantics Public native intGetintvolatile (Object o,Longoffset);//Same as Putintvolatile (), but it requires that the field being manipulated is a volatile type Public native voidPutorderedint (Object o,LongOffset, intx);
If you remember the Concurrentlinkedqueue implementation described in the section "3.3.4 Deep Anatomy Concurrentlinkedqueue", you should have some impressions of node in Concurrentlinkedqueue. Node Some CAS operations are also implemented using the unsafe class. You can look back to deepen your impressions of the unsafe category.
As you can see here, Java discards the pointer. But at critical times, pointers-like techniques are essential. The underlying unsafe implementation here is the best example. Unfortunately, JDK developers do not want this class to be used by people. The method of obtaining an unsafe instance is to mobilize its factory method Getunsafe (). However, its implementation is this:
Public Static Unsafe Getunsafe () { =reflection.getcallerclass (); if NULL ) throw newsecurityexception ("Unsafe"); return Theunsafe;}
Note The bold section of the code, which checks the class that calls the Getunsafe () function, and if the class's classloader is not NULL, throws the exception directly, rejecting the work. Therefore, this also makes it impossible for our own applications to use the unsafe class directly. It is an exclusive class used within the JDK.
Note: Depending on how the Java ClassLoader Works, the application's class is loaded by Apploader. The classes in the system core class, such as Rt.jar, are loaded by the Bootstrap class loader. The bootstrap loader does not have objects for Java objects, so attempting to get this classloader will return null. So, when a class loader is null, it is loaded by bootstrap, and this class is most likely the class in Rt.jar.
This book:
"Practical Java High Concurrency Programming 1" Pointers in Java: unsafe class