How is the atomicity of Java concurrency Framework--aqs guaranteed?

Source: Internet
Author: User
Tags cas

In the study of the AQS framework, it will be found that many places in this class use CAS operations, in the concurrent implementation of CAS operations must be atomic, but also the hardware level of atomicity, Java is isolated on the hardware, obviously powerless, at this time in order to operate the operating system level, it must be by using C + + Write the native local method to extend the implementation. The JDK provides a class to meet the requirements of CAs, Sun.misc.Unsafe, which is probably known by name to perform low-level, unsafe operations, and AQS is the use of this class to complete hardware-level atomic operations.

Unsafe is a powerful class that allocates memory, frees up memory, locates objects in a field, can modify the field values of an object, can suspend threads, restore threads, CAS operations that can be atomic at the hardware level, and so on, but usually we don't have that special need to use it. This class must also be called in the trusted code (typically specified by the JVM), such as direct unsafe unsafe = unsafe.getunsafe (); Obtaining an unsafe instance is not successful because the security of this class is important, and the designer makes the following judgments about it. It detects whether the class calling it is loaded by the startup ClassLoader Bootstrap ClassLoader (its classloader is null), thus guaranteeing that the class can only be used by the class specified by the JVM.

public static Unsafe Getunsafe () {

Class cc = Sun.reflect.Reflection.getCallerClass (2);

if (cc.getclassloader () = null)

throw new SecurityException ("Unsafe");

return theunsafe;

}

Of course, you can bypass the above restrictions by reflection, using the following Getunsafeinstance method to obtain an unsafe instance, this code demonstrates how to get the relative address offset of the Java object and use unsafe to complete the CAS operation, The final output is the memory offset of the flag field and the value after the CAS operation. are 8 and 101, respectively. In addition, if you use development tools such as Eclipse, you may compile the pass, just turn off the compilation error prompt.

public class Unsafetest {

Privateint flag = 100;

Privatestatic long offset;

privatestatic unsafe unsafe = null;

static{

try{

Unsafe= getunsafeinstance ();

offset= Unsafe.objectfieldoffset (Unsafetest.class

. Getdeclaredfield ("flag"));

}catch (Exception e) {

E.printstacktrace ();

}

}

publicstatic void Main (string[] args) throws Exception {

Intexpect = 100;

Intupdate = 101;

Unsafetestunsafetest = new Unsafetest ();

SYSTEM.OUT.PRINTLN (the address offset of the Unsafetest object's flag field is: "+offset);

Unsafetest.doswap (offset,expect, update);

SYSTEM.OUT.PRINTLN ("The flag value after CAS operation is:" +unsafetest.getflag ());

}

Privateboolean Doswap (long offset, int expect, int update) {

Returnunsafe.compareandswapint (this, offset, expect, update);

}

Publicint Getflag () {

Returnflag;

}

Privatestatic Unsafe getunsafeinstance () throws SecurityException,

Nosuchfieldexception,illegalargumentexception,

illegalaccessexception{

Fieldtheunsafeinstance = Unsafe.class.getDeclaredField ("Theunsafe");

Theunsafeinstance.setaccessible (TRUE);

Return (Unsafe) theunsafeinstance.get (Unsafe.class);

}

}

The unsafe class lets us understand how Java implements operating system operations, generally we do not need to use Java in memory to handle Java objects and memory address location, but sometimes we do need to know the Java object-related address, so we use the unsafe class, Although Java provides it with adequate security management.

The Java language designers are trying to hide related operations that involve the underlying operating system, but in this section we have to dissect the unsafe class for the purpose of the AQS framework, because the AQS inside is using unsafe to get the address offset of the object field, the related atomic operation to achieve the CAS operation.

How is the atomicity of Java concurrency Framework--aqs guaranteed?

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.