![JAR search and dependency download from the Maven repository](/logo.png)
org.multiverse.performancetool.UnsafeUtils Maven / Gradle / Ivy
package org.multiverse.performancetool;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
/**
* Simple class to obtain access to the {@link Unsafe} object. {@link Unsafe}
* is required to allow efficient CAS operations on arrays. Note that the
* versions in {@link java.util.concurrent.atomic}, such as {@link
* java.util.concurrent.atomic.AtomicLongArray}, require extra memory ordering
* guarantees which are generally not needed in these algorithms and are also
* expensive on most processors.
*/
class UtilUnsafe {
private UtilUnsafe() {
} // dummy private constructor
/**
* Fetch the Unsafe. Use With Caution.
*/
public static Unsafe getUnsafe() {
// Not on bootclasspath
if (UtilUnsafe.class.getClassLoader() == null)
return Unsafe.getUnsafe();
try {
final Field fld = Unsafe.class.getDeclaredField("theUnsafe");
fld.setAccessible(true);
return (Unsafe) fld.get(UtilUnsafe.class);
} catch (Exception e) {
throw new RuntimeException("Could not obtain access to sun.misc.Unsafe", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy