当前位置:网站首页>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); } } }
参考文档
边栏推荐
- The third day of writing C language by Yabo people
- Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
- Distributed task distribution framework gearman
- ArrayList分析3 : 删除元素
- [combinatorics] recursive equation (case where the non-homogeneous part is exponential | example where the non-homogeneous part is exponential)
- 解决Zabbix用snmp监控网络流量不准的问题
- Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
- Redis on local access server
- 一入“远程”终不悔,几人欢喜几人愁。| 社区征文
- The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
猜你喜欢
Leetcode 108 converts an ordered array into a binary search tree -- recursive method
TCP拥塞控制详解 | 3. 设计空间
Interviewer: why is the value nil not equal to nil?
Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
win32:堆破壞的dump文件分析
On Data Mining
面试官:值为 nil 为什么不等于 nil ?
Leetcode Valentine's Day Special - looking for a single dog
Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
PHP MySQL inserts data
随机推荐
分布式的任务分发框架-Gearman
How to draw non overlapping bubble chart in MATLAB
Applet with multiple tabs and Swipers + paging of each tab
1146_ SiCp learning notes_ exponentiation
Class exercises
PHP returns 500 errors but no error log - PHP return 500 error but no error log
Website with JS doesn't work in IE9 until the Developer Tools is activated
Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
【统信UOS】扫描仪设备管理驱动安装
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
[combinatorics] generating function (linear property | product property)
Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
Analyse ArrayList 3: suppression d'éléments
AcWing 271. 杨老师的照相排列【多维DP】
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
Inheritance of ES6 class
The third day of writing C language by Yabo people
Baiwen.com 7 days Internet of things smart home learning experience punch in the next day
图像24位深度转8位深度
Introduction to SolidWorks gear design software tool geartrax