当前位置:网站首页>Unsafe类的使用
Unsafe类的使用
2022-07-03 18:02:00 【一年春又来】
Unsafe类的使用
Unsafe可用来直接访问系统内存资源并自主管理,在提升Java运行效率、增强Java语言底层操作能力方面起了很大的作用——可以认为,Unsafe类是Java中留下的后门,提供了一些低层次操作,如直接内存访问、线程调度等。
Unsafe不属于Java标准。 官方并不建议使用Unsafe,并且从JDK 9开始,官方开始去Unsafe!
相关Issue:https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6852936
因此,Unsafe类对于项目实战,意义并不大。然而目前业界有很多好用的类库大量使用了Unsafe类,例如java.util.concurrent.atomic 包里的一堆类、Netty、Hadoop、Kafka等。所以了解一下还是有好处的。
不同的JDK版本中,Unsafe类也有区别,例如:
- 在JDK 8中归属于sun.misc包下;
- 在JDK 11中归属于sun.misc包或jdk.internal.misc下,其中jdk.internal.misc下的Unsafe类功能更强。(应该是从JDK 9开始的,笔者未亲测)
快速上手
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class DirectMemoryTest1 {
private static final int _1MB = 1024 * 1024;
public static void main(String[] args) throws IllegalAccessException {
//通过反射获取Unsafe类并通过其分配直接内存
Field unsafeField = Unsafe.class.getDeclaredFields()[0];
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) unsafeField.get(null);
int i = 0;
while (true) {
unsafe.allocateMemory(_1MB);
System.out.println(++i);
}
}
}
Unsafe类的使用
详见:
- https://www.jb51.net/article/140726.htm
- https://blog.csdn.net/ahilll/article/details/81628215
- https://www.jianshu.com/p/dd2be4d3b88e
JDK 11如何使用Unsafe类
TIPS
- 再次强调,实际项目中,除非情非得已,尽量不要用Unsafe类,官方不建议使用
- 从JDK 10开始,Unsafe类的部分功能已经被VarHandle取代,建议用VarHandle
创建一个JDK 11的项目
在项目源码路径下创建
module-info.java,内容如下:module unsafe { requires jdk.unsupported; }创建后,代码结构如下:
|____src | |____main | | |____java | | | |____module-info.java | | | |____com | | | | |____example | | | | | |____demo | | | | | | |____UnsafePlayer.java | | | | | | |____DirectMemoryTest1.java测试代码:
import sun.misc.Unsafe; import java.lang.reflect.Field; public class DirectMemoryTest1 { private static final int _1MB = 1024 * 1024; public static void main(String[] args) throws IllegalAccessException { //通过反射获取Unsafe类并通过其分配直接内存 Field unsafeField = Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible(true); Unsafe unsafe = (Unsafe) unsafeField.get(null); int i = 0; while (true) { unsafe.allocateMemory(_1MB); System.out.println(++i); } } }
参考文档
边栏推荐
- PHP MySQL reads data
- PHP MySQL where clause
- Three gradient descent methods and code implementation
- Deops入门
- The third day of writing C language by Yabo people
- Basic grammar of interview (Part 2)
- Module 9 operation
- [combinatorics] generating function (linear property | product property)
- PHP MySQL preprocessing statement
- [enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
猜你喜欢

Introduction to SolidWorks gear design software tool geartrax
![[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th](/img/f1/c96c4a6d34e1ae971f492f4aed5a93.jpg)
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
![[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)](/img/e6/9880e708aed42dc82c94aea002020c.jpg)
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)

How do microservices aggregate API documents? This wave of operation is too good

聊聊支付流程的设计与实现逻辑

SQL injection database operation foundation

How to draw non overlapping bubble chart in MATLAB

On Data Mining

Redis core technology and practice - learning notes (IX): slicing cluster

聊聊支付流程的设计与实现逻辑
随机推荐
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
Keepalived setting does not preempt resources
ArrayList分析3 : 删除元素
Mathematical formula (test)
Three gradient descent methods and code implementation
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
(8) HS corner detection
Kotlin's collaboration: Context
Embedded-c language-7
基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
[untitled]
Valentine's day, send you a little red flower~
Distributed task distribution framework gearman
Applet with multiple tabs and Swipers + paging of each tab
TCP congestion control details | 3 design space
PHP processing - watermark images (text, etc.)
ArrayList分析3 : 删除元素
Basic grammar of interview (Part 2)
Getting started with deops
Ssl/bio of OpenSSL_ get_ fd