当前位置:网站首页>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); } } }
参考文档
边栏推荐
- TCP congestion control details | 3 design space
- Create a new file from templates with bash script - create new file from templates with bash script
- Prototype inheritance..
- Remote office tools sharing | community essay solicitation
- 毕业总结
- Analyse ArrayList 3: suppression d'éléments
- A. Berland Poker & 1000 [simple mathematical thinking]
- 一入“远程”终不悔,几人欢喜几人愁。| 社区征文
- 统计图像中各像素值的数量
- ArrayList analysis 3: delete elements
猜你喜欢

Leetcode 669 pruning binary search tree -- recursive method and iterative method

Automata and automatic line of non-standard design
![[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)](/img/50/f89092b492d0138304209a72ff05b4.jpg)
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)

Class exercises

AcWing 271. 杨老师的照相排列【多维DP】
![[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)

Leetcode Valentine's Day Special - looking for a single dog

Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it

(9) Opencv Canny edge detection

模块九作业
随机推荐
Automata and automatic line of non-standard design
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
Win32: analyse du fichier dump pour la défaillance du tas
As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
聊聊支付流程的设计与实现逻辑
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
聊聊支付流程的设计与实现逻辑
TCP拥塞控制详解 | 3. 设计空间
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
[combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
Kotlin's collaboration: Context
How to draw non overlapping bubble chart in MATLAB
Draw some simple graphics with MFC
PHP MySQL inserts multiple pieces of data
聊聊支付流程的設計與實現邏輯
Codeforces Round #803 (Div. 2) C. 3SUM Closure
[combinatorics] generating function (summation property)
Graduation summary