Why is sun.misc.unsafe.Unsafe not in one of the java packages by default. *?

Java has the Unsafe class in the java.lang.unsafe package, which provides access to low-level operations.

Now it seems to me that the JVM should support all the methods available in the Unsafe class, in order to be compatible with JLS, you can find examples of methods.

Examples of operations Unsafe and JVM must be supported in order to be compatible with JLS:

  • Work with objects
  • Data storage (such as int, long, byte, etc.)
  • Data retrieval
  • Work with monitors
  • Work with memory fences
  • and etc.

Now my question is: why can't we find the Unsafe class in the java package. *? Is there a specific reason why it would be better for each JVM provider to create its own version of sun.misc.unsafe.Unsafe instead of writing an implementation for a class defined in java. * Package?

+5
source share
2 answers

Creating Unsafe part of the core API, and the JLS implementation are separate issues.

Unsafe not part of the Java API because it is unsafe, and Oracle does not want developers to use it.

However, they know well 1) that many good developers have successfully used it, and that 2) many bad developers have abused it. Thus, there is a long-term plan to safely, intelligently and conveniently support the useful features of Unsafe through the underlying Java platform.

You can view the presentation from Mark Reinhold , which explains in more detail how Unsafe will be ported and will eventually go.

However, even when the Unsafe APIs are moved to some java (or javax ) javax , providers will still have inner classes and native code to implement these APIs. For example, Oracle might have something like oracle.internal.misc.StillUnsafe with a bunch of native methods. This class will not be managed by JLS; Oracle will freely implement it as they like, and change it whenever it wants. But with this approach, they would also provide an implementation of the core Java APIs that delegate them to the inner class.

Declaring an API in a java.* Package does not mean that Java runtime authors do not need to provide code to back it up; many Java APIs are abstract and must be provided by the service provider.

+9
source

Java unsafe was the package that should have been used by the Java Classes kernel, see here . Also, Java did not want people to use the sun.misc.unsafe class in fact, they wanted to remove it. Oracle wants to discourage the use of an unsafe class, while restricting the class to only trusted code support.

There is a proposal to add unsafe to supported Java classes, but not how it exists today. Instead, Unsafe will be encapsulated in modules that define and use them. This is an official proposal that arose in connection with Oracleโ€™s protest over the received one when they announced their intention to remove unsafe .

+3
source

All Articles