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

查看:140
本文介绍了为什么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?

推荐答案

示例


  1. VM内在化。即无锁散列表中使用的CAS(比较和交换)
    例如: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

http://en.wikipedia上阅读有关CAS的更多信息。 org / wiki /比较和交换

主机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的时间优化。高性能VM使用魔术,需要低级操作。例如: 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例如: - 当调用ByteBuffer.allocateDirect时,DirectByteBuffer构造函数在内部调用它

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/how-to-write-to- direct-memory-locations-in-java

更多关于此处的参考资料 - http://by tescrolls.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天全站免登陆