Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

In Java there is the Unsafe class in the java.lang.unsafe package that provides low-level access to operations.

Now it seems to me that the JVM needs to support all of the methods available in the Unsafe class in order to be compliant to the JLS, example methods can be found here.

Examples of the operations Unsafe provides and JVMs need to support in order to be compliant to the JLS would be:

  • Working with objects
  • Storing data (of type int, long, byte, etc.)
  • Retrieving data
  • Working with monitors
  • Working with memory fences
  • etc.

My question now is: Why can we not find the Unsafe class in a java.* package? Is there a specific reason why it would be better that every JVM provider creates their own sun.misc.unsafe.Unsafe variant instead of writing an implementation for a class defined in the java.* package?

like image 864
skiwi Avatar asked Aug 12 '15 18:08

skiwi


People also ask

Why is Java considered unsafe?

Java lacks null safety. When a function receives an object, this object might be null. That is, if you see 'String s' in your code, you often have no way of knowing whether 's' contains an actually String unless you check at runtime.

What is unsafe in Java?

Yet, the Java runtime provides an Unsafe API as a backdoor for the developers to access the low- level system code. Whereas the Unsafe API is designed to be used by the Java core library, a growing community of third-party libraries use it to achieve high performance.


2 Answers

Making Unsafe part of the core API, and implementing the JLS are separate issues.

Unsafe is not part of the Java API because it's unsafe, and Oracle didn't want developers to use it.

However, they are well aware 1) that many good developers have used it successfully, and that 2) many bad developers have abused it. So, there is a long term plan to expose the useful features of Unsafe through the core Java platform in a safe, sane, and supportable way.

You can view a presentation from Mark Reinhold that explains in more detail how Unsafe will be migrated and eventually go way.

However, even when Unsafe APIs are moved into some java (or javax) package, providers will still have internal classes and native code to implement those APIs. For example, Oracle might have something like oracle.internal.misc.StillUnsafe with a bunch of native methods in it. This class won't be governed by the JLS; Oracle will be free to implement it how they like and change it when they like. But, with this approach, they would also provide implementations of the core Java APIs that delegate to their internal class.

Declaring the API in a java.* package does not mean that authors of Java runtimes don't have to provide the code to back it up; many Java APIs are abstract and must be given an implementation by a provider.

like image 175
erickson Avatar answered Oct 19 '22 09:10

erickson


The Java unsafe was package was only supposed to be used by core Java Classes see here. Also Java did not want individuals using the sun.misc.unsafe class at, in fact, they wanted to remove it. Oracle wanted to discourage the use of unsafe class, while limiting the class to only support trusted code.

There is a proposal to add Unsafe to supported Java Classes, but not how it exists today. Instead Unsafe will be encapsulated within the modules that define and use them. This is a formal proposal, which arose due to the outcry Oracle recieved when they stated intentions to remove Unsafe.

like image 38
Turtle Avatar answered Oct 19 '22 10:10

Turtle