当前位置:网站首页>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); } } }
参考文档
边栏推荐
- 基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
- Win32: analyse du fichier dump pour la défaillance du tas
- Supervisor monitors gearman tasks
- Assembly for unloading Loadfrom() loaded assembly - unloading the assembly loaded with assembly LoadFrom()
- PHP MySQL reads data
- Remote office tools sharing | community essay solicitation
- Image 24 bits de profondeur à 8 bits de profondeur
- Basic grammar of interview (Part 2)
- Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
- 小程序 多tab 多swiper + 每个tab分页
猜你喜欢

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

win32:堆破壞的dump文件分析

Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028

Module 9 operation

MySQL has been stopped in the configuration interface during installation

Deops入门

Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028

Baiwen.com 7 days Internet of things smart home learning experience punch in the next day

win32:堆破坏的dump文件分析

Class exercises
随机推荐
ArrayList分析3 : 删除元素
Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
聊聊支付流程的设计与实现逻辑
How to expand the capacity of golang slice slice
Assembly for unloading Loadfrom() loaded assembly - unloading the assembly loaded with assembly LoadFrom()
1146_ SiCp learning notes_ exponentiation
Ssl/bio of OpenSSL_ get_ fd
Managing multiple selections with MVVM - managing multiple selections with MVVM
微服务组件Sentinel控制台调用
PHP MySQL create database
PHP processing - watermark images (text, etc.)
Create a new file from templates with bash script - create new file from templates with bash script
网格图中递增路径的数目[dfs逆向路径+记忆dfs]
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
ArrayList分析3 : 删除元素
一入“远程”终不悔,几人欢喜几人愁。| 社区征文
AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
Getting started with deops
STM32实现74HC595控制
win32:堆破坏的dump文件分析