前几天我偶然发现了一个不安全的包装,它的功能让我感到惊讶。

当然,这个类是没有记录的,但是我想知道是否有一个很好的理由使用它。可能会出现哪些需要使用它的场景?如何在现实场景中使用它?

此外,如果您确实需要它,这难道不表明您的设计可能存在问题吗?

为什么Java包含这个类?


当前回答

不安全的。允许抛出检查异常而不声明它们。

这在处理反射或AOP的某些情况下非常有用。

假设您为用户定义的接口构建了通用代理。在特殊情况下,用户可以通过在接口中声明异常来指定由实现抛出的异常。这是我所知道的唯一方法,在接口的动态实现中引发一个受控异常。

import org.junit.Test;
/** need to allow forbidden references! */ import sun.misc.Unsafe;

/**
 * Demonstrate how to throw an undeclared checked exception.
 * This is a hack, because it uses the forbidden Class {@link sun.misc.Unsafe}.
 */
public class ExceptionTest {

    /**
     * A checked exception.
     */
    public static class MyException extends Exception {
        private static final long serialVersionUID = 5960664994726581924L;
    }

    /**
     * Throw the Exception.
     */
    @SuppressWarnings("restriction")
    public static void throwUndeclared() {
        getUnsafe().throwException(new MyException());
    }

    /**
     * Return an instance of {@link sun.misc.Unsafe}.
     * @return THE instance
     */
    @SuppressWarnings("restriction")
    private static Unsafe getUnsafe() {
        try {

            Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
            singleoneInstanceField.setAccessible(true);
            return (Unsafe) singleoneInstanceField.get(null);

        } catch (IllegalArgumentException e) {
            throw createExceptionForObtainingUnsafe(e);
        } catch (SecurityException e) {
            throw createExceptionForObtainingUnsafe(e);
        } catch (NoSuchFieldException e) {
            throw createExceptionForObtainingUnsafe(e);
        } catch (IllegalAccessException e) {
            throw createExceptionForObtainingUnsafe(e);
        }
    }

    private static RuntimeException createExceptionForObtainingUnsafe(final Throwable cause) {
        return new RuntimeException("error while obtaining sun.misc.Unsafe", cause);
    }


    /**
     * scenario: test that an CheckedException {@link MyException} can be thrown
     * from an method that not declare it.
     */
    @Test(expected = MyException.class)
    public void testUnsingUnsaveToThrowCheckedException() {
        throwUndeclared();
    }
}

其他回答

通过在一些代码搜索引擎中运行搜索,我得到了以下示例:

Java对象符号——使用它来进行更有效的数组处理,引用javadoc

类来获取对{@link Unsafe}对象的访问。{@link安全} 为了允许对数组进行有效的CAS操作,*是必需的。注意 {@link java.util.concurrent中的版本。原子},例如{@link java.util.concurrent.atomic。AtomicLongArray},需要额外的内存排序 这些保证在这些算法中通常是不需要的 在大多数处理器上都很昂贵。

SoyLatte - java 6 for osx javadoc excerpt

/** sun.misc的基类基于静态的不安全的FieldAccessors 字段。据观察,只有9种类型的 从反射代码的角度看字段:八个原语 类型和对象。使用不安全类而不是生成类 字节码节省内存和加载时间 动态生成的FieldAccessors。* /

SpikeSource

/* 通过线路发送的FinalFields ..如何解编和重新创建对象上 接收方?我们不想调用构造函数,因为它将为 最后一个字段。我们必须重新创建与发送端完全相同的最终字段。 太阳,杂项,不安全为我们做了这些。 * /

还有很多其他的例子,只要按上面的链接…

对象的可用性似乎低于Java代码通常允许的级别。如果您正在编写一个高级应用程序,那么JVM将内存处理和其他操作从代码级别抽象出来,因此更容易编程。通过使用不安全库,您可以有效地完成通常由您完成的低级操作。

正如woliveirajr所述,“random()”使用Unsafe来播种,就像许多其他操作将使用Unsafe中包含的allocateMemory()函数一样。

作为程序员,你可能永远不需要这个库,但严格控制底层元素确实很方便(这就是为什么在主要产品中仍然有汇编代码和(在较小程度上)C代码的原因)

I was recently working on reimplementing the JVM and found that a surprising number of classes are implemented in terms of Unsafe. The class is mostly designed for the Java library implementers and contains features that are fundamentally unsafe but necessary for building fast primitives. For example, there are methods for getting and writing raw field offsets, using hardware-level synchronization, allocating and freeing memory, etc. It is not intended to be used by normal Java programmers; it's undocumented, implementation-specific, and inherently unsafe (hence the name!). Moreover, I think that the SecurityManager will disallow access to it in almost all cases.

简而言之,它的存在主要是为了允许库实现者访问底层机器,而不必在某些类(如AtomicInteger native)中声明每个方法。在常规Java编程中不需要使用或担心它,因为重点是使其余的库足够快,从而不需要这种访问。

类不安全 用于执行低级、不安全操作的方法的集合。虽然类和所有方法都是公共的,但该类的使用是有限的,因为只有可信代码才能获得它的实例。

它的一种用法是在java.util.concurrent.atomic类中:

AtomicIntegerArray AtomicLongArray

例子

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 read more about CAS here http://en.wikipedia.org/wiki/Compare-and-swap 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. 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! Compile time optimizations for JVM. HIgh performance VM using "magic", requiring low-level operations. eg: http://en.wikipedia.org/wiki/Jikes_RVM Allocating memory, sun.misc.Unsafe.allocateMemory eg:- DirectByteBuffer constructor internally calls it when ByteBuffer.allocateDirect is invoked Tracing the call stack and replaying with values instantiated by sun.misc.Unsafe, useful for instrumentation 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://bytescrolls.blogspot.com/2011/04/interesting-uses-of-sunmiscunsafe.html