当前位置:网站首页>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); } } }
参考文档
边栏推荐
- [linux]centos 7 reports an error when installing MySQL "no package MySQL server available" no package ZABBIX server MySQL available
- Keepalived setting does not preempt resources
- This diversion
- STM32实现74HC595控制
- PHP processing - watermark images (text, etc.)
- Basic grammar of interview (Part 2)
- Redis core technology and practice - learning notes (IX): slicing cluster
- 聊聊支付流程的設計與實現邏輯
- Analyse ArrayList 3: suppression d'éléments
- Implementation of Tetris in C language
猜你喜欢

BFS - topology sort

win32:堆破壞的dump文件分析

Talk about the design and implementation logic of payment process

Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding

PHP MySQL inserts data

PHP MySQL preprocessing statement

Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up

Deops入门

PHP MySQL inserts multiple pieces of data

Introduction to SolidWorks gear design software tool geartrax
随机推荐
How to draw non overlapping bubble chart in MATLAB
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
Kotlin的协程:上下文
Keepalived setting does not preempt resources
毕业总结
聊聊支付流程的设计与实现逻辑
Win32: analyse du fichier dump pour la défaillance du tas
Five problems of database operation in commodity supermarket system
Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
Keepalived 设置不抢占资源
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
Distributed task distribution framework gearman
Postfix tips and troubleshooting commands
Micro service component sentinel console call
ES6类的继承
Records of long objects and long judgments in the stream of list
[combinatorics] generating function (shift property)
PHP MySQL inserts multiple pieces of data
PHP MySQL preprocessing statement
Redis core technology and practice - learning notes (IX): slicing cluster