Sunday, 28 June 2015

Understanding UnSafe Class in java

Today i am going to discuss about sun.misc.UnSafe class in java .
This class we will hardly ever use practically but we should be well aware of it as we use it indirectly a lot while using classes from packages like java.util.concurrent, java.nio, etc which involve interacting with java memory addresses.

If we ever want to write low level APIs which need to manipulate memory and threads directly, we will have to use this class.
However, this class is very dangerous and should be used with extreme caution only if needed. As aptly named as “unsafe” , we can see that it doesnt even contain a public constructor and the instance is exposed as a singleton through a get method: getUnsafe ()

Even the getter method is restricted for use to only trusted classes by having an additional check in the method that the classloader should be Null which means only the bootstrap classloader can access this method. Any other classloader will throw a security exception.

public final class Unsafe {
//....more code lines
private Unsafe() {}
private static final Unsafe theUnsafe = new Unsafe();

public static Unsafe getUnsafe() {
Class cc = Reflection.getCallerClass();
if (cc.getClassLoader() != null)
throw new SecurityException("Unsafe");
return theUnsafe;
//......more code lines

I stumbled upon the unSafe class, while looking into the source code of concurrent classes like concurrentHashMap,AtomiceInteger,etc.
In all such classes, we will find that unSafe singleton instance is used as a handler to do memory operations like CompareAndSet (CAS) internally.
For example in AtomicInteger class,
we can see that internally this class consists of a volatile integer : value .
With use of unSafe instance, the value of this volatile integer is manipulaed atomically at hardware level without using synchronization.
The valueOffset represents the memory address where value needs to be updated. At worse it might need few iterations before integer value is set as expected when multiple threads are setting the value at same time :

public class AtomicInteger extends Number implements {
private static final long serialVersionUID = 6214790243416807050L;
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
private volatile int value;

public final int getAndSet(int newValue) {
for (;;) {
int current = get();
if (compareAndSet(current, newValue))
return current;
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);

My main intention through this post was to give an idea of UnSafe class being used at the heart of java.util.concurrent classes for providing atomicity without synchronization.

However, there are a number of functionalities which UnSafe class provides you . Some of them are :
  • Creating Object instance without calling constructor (unlike new and reflection APIs)
  • Manipulating memory addresses
  • Creating super Big Arrays : C-style memory blocks natively with no restriction on size and all .
If you want to go more into deep of the UnSafe class, i would highly recommend to go through this post :

I hope this post was useful......Happy Learning :)


  1. perfect explanation about java programming .its very useful.thanks for your valuable training in chennai | chennai's no.1 java training in chennai

  2. Very informative ..i suggest this blog to my friends..Thank you for sharingjava training in chennai | chennai's no.1 java training in chennai