为什么 sun.misc.Unsafe 存在,在现实世界中如何使用? [英] Why does sun.misc.Unsafe exist, and how can it be used in the real world?

查看:52
本文介绍了为什么 sun.misc.Unsafe 存在,在现实世界中如何使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天我遇到了 sun.misc.Unsafe 包,对它的功能感到惊讶.

I came across the sun.misc.Unsafe package the other day and was amazed at what it could do.

当然,这个类是无证的,但我想知道是否有充分的理由使用它.哪些场景可能会出现在您需要使用它的地方?如何在现实世界中使用它?

Of course, the class is undocumented, but I was wondering if there was ever a good reason to use it. What scenarios might arise where you would need to use it? How might it be used in a real-world scenario?

此外,如果您确实需要它,这不表示您的设计可能有问题吗?

Furthermore, if you do need it, does that not indicate that something is probably wrong with your design?

为什么 Java 甚至包含这个类?

Why does Java even include this class?

推荐答案

examples

  1. 虚拟机内在化".即无锁哈希表中使用的CAS(Compare-And-Swap)例如:sun.misc.Unsafe.compareAndSwapInt它可以将真正的 JNI 调用转换为包含 CAS 特殊指令的本机代码

  1. VM "intrinsification." ie CAS (Compare-And-Swap) used in Lock-Free Hash Tables eg:sun.misc.Unsafe.compareAndSwapInt it can make real JNI calls into native code that contains special instructions for CAS

在此处阅读有关 CAS 的更多信息 http://en.wikipedia.org/wiki/Compare-and-swap

read more about CAS here http://en.wikipedia.org/wiki/Compare-and-swap

宿主 VM 的 sun.misc.Unsafe 功能可用于分配未初始化的对象,然后将构造函数调用解释为任何其他方法调用.

The sun.misc.Unsafe functionality of the host VM can be used to allocate uninitialized objects and then interpret the constructor invocation as any other method call.

可以从本机地址跟踪数据.可以检索一个使用 java.lang.Unsafe 类获取对象的内存地址,并通过不安全的 get/put 方法直接对其字段进行操作!

One can track the data from the native address.It is possible to retrieve an object’s memory address using the java.lang.Unsafe class, and operate on its fields directly via unsafe get/put methods!

JVM 的编译时间优化.使用魔法"的高性能虚拟机,需要低级操作.例如:http://en.wikipedia.org/wiki/Jikes_RVM

Compile time optimizations for JVM. HIgh performance VM using "magic", requiring low-level operations. eg: http://en.wikipedia.org/wiki/Jikes_RVM

分配内存,sun.misc.Unsafe.allocateMemory eg:- DirectByteBuffer 构造函数在调用 ByteBuffer.allocateDirect 时内部调用它

Allocating memory, sun.misc.Unsafe.allocateMemory eg:- DirectByteBuffer constructor internally calls it when ByteBuffer.allocateDirect is invoked

跟踪调用堆栈并重放由 sun.misc.Unsafe 实例化的值,对检测很有用

Tracing the call stack and replaying with values instantiated by sun.misc.Unsafe, useful for instrumentation

sun.misc.Unsafe.arrayBaseOffset 和 arrayIndexScale 可用于开发 arraylets,这是一种有效地将大型数组分解为更小的对象以限制对大型对象进行扫描、更新或移动操作的实时成本的技术

sun.misc.Unsafe.arrayBaseOffset and arrayIndexScale can be used to develop arraylets,a technique for efficiently breaking up large arrays into smaller objects to limit the real-time cost of scan, update or move operations on large objects

http://robaustin.wikidot.com/java中如何写入直接内存位置

更多关于这里的参考 - http://bytescrolls.blogspot.com/2011/04/interesting-uses-of-sunmiscunsafe.html

more on references here - http://bytescrolls.blogspot.com/2011/04/interesting-uses-of-sunmiscunsafe.html

这篇关于为什么 sun.misc.Unsafe 存在,在现实世界中如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆