Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the Unsafe utility class

Shulou Source: shulou.com Published: 2022-06-03 05:49:59 10月28日 Update

This article mainly explains "what is the Unsafe tool class". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the Unsafe tool class".

Unsafe utility class

Reflection is the first feature of Java. Once you open the door of reflection, you can have a richer form of class design. In addition to the reflection processing supported by JVM itself, a Unsafe class (unsafe operation) is also provided in Java. The main feature of this class is that it can use reflection to obtain objects and directly use the underlying C++ to replace JVM implementation, that is, you can bypass JVM's related object management mechanism. Once Unsafe is used, then the project will no longer be able to use JVM memory management mechanism as well as garbage collection processing.

But if you want to use the Unsafe class, you first need to confirm the constructor and constant problems defined in this class:

Construction method: private Unsafe () {}

Private constant: private static final Unsafe theUnsafe = new Unsafe ()

However, it should be noted that there is no static method provided in this Unsafe class, that is, it cannot be manipulated by a style similar to that provided in the traditional singleton design pattern, and if you want to get the objects of this class, you must use the reflection mechanism to do so.

Import sun.misc.Unsafe;import java.lang.reflect.Field;public class JavaAPIDemo {public static void main (String [] args) throws Exception {Field field = Unsafe.class.getDeclaredField ("theUnsafe"); field.setAccessible (true); / / Unencapsulation processing Unsafe unsafeObject = (Unsafe) field.get (null); / / static property does not need to pass instantiated object}}

In traditional development, a program class must instantiate the object before it can call the common methods in the class, especially taking the singleton design pattern as an example.

Example: use the Unsafe class to bypass the management of instantiated objects

Import sun.misc.Unsafe;import java.lang.reflect.Field;public class JavaAPIDemo {public static void main (String [] args) throws Exception {Field field = Unsafe.class.getDeclaredField ("theUnsafe"); field.setAccessible (true); Unsafe unsafeObject = (Unsafe) field.get (null) / / using the Unsafe class to bypass the JVM management mechanism, you can get an Singleton class instantiated object Singleton instance = (Singleton) unsafeObject.allocateInstance (Singleton.class) without instantiating the object; instance.print () / / www.mldn.cn}} class Singleton {private Singleton () {System.out.println ("* Singleton Class Construction *")} public void print () {System.out.println ("www.mldn.cn") }} Thank you for your reading, the above is the content of "what is the Unsafe tool class". After the study of this article, I believe you have a deeper understanding of what is the Unsafe tool class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Objects tools instances reflections methods mechanisms management processing learning design traditions content constants situations patterns characteristics design patterns problems general security Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology Microsoft Shulou Information Huawei