JAVA Unsafe類詳解

JAVA Unsafe類詳解

官方不建議使用Unsafe

使用Unsafe要注意以下幾個問題:

1、Unsafe有可能在未來的Jdk版本移除或者不允許Java應用代碼使用,這一點可能導致使用了Unsafe的應用無法運行在高版本的Jdk。

2、Unsafe的不少方法中必須提供原始地址(內存地址)和被替換對象的地址,偏移量要自己計算,一旦出現問題就是JVM崩潰級別的異常,會導致整個JVM實例崩潰,表現爲應用程序直接crash掉。

3、Unsafe提供的直接內存訪問的方法中使用的內存不受JVM管理(無法被GC),需要手動管理,一旦出現疏忽很有可能成爲內存泄漏的源頭。

暫時總結出以上三點問題。Unsafe 在JUC (java.util.concurrent ) 包中大量使用 (主要是CAS),在netty中方便使用直接內存,還有一些高併發的交易系統爲了提高CAS的效率也有可能直接使用到Unsafe。總而言之,Unsafe類是一把雙刃劍。

功能介紹

img

內存操作

這部分主要包含堆外內存的分配、拷貝、釋放、給定地址值操作等方法。

//分配內存, 相當於C++的malloc函數
public native long allocateMemory(long bytes);
//擴充內存
public native long reallocateMemory(long address, long bytes);
//釋放內存
public native void freeMemory(long address);
//在給定的內存塊中設置值
public native void setMemory(Object o, long offset, long bytes, byte value);
//內存拷貝
public native void copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
//獲取給定地址值,忽略修飾限定符的訪問限制。與此類似操作還有: getInt,getDouble,getLong,getChar等
public native Object getObject(Object o, long offset);
//爲給定地址設置值,忽略修飾限定符的訪問限制,與此類似操作還有: putInt,putDouble,putLong,putChar等
public native void putObject(Object o, long offset, Object x);
//獲取給定地址的byte類型的值(當且僅當該內存地址爲allocateMemory分配時,此方法結果爲確定的)
public native byte getByte(long address);
//爲給定地址設置byte類型的值(當且僅當該內存地址爲allocateMemory分配時,此方法結果纔是確定的)
public native void putByte(long address, byte x);

通常,我們在Java中創建的對象都處於堆內內存(heap)中,堆內內存是由JVM所管控的Java進程內存,並且它們遵循JVM的內存管理機制,JVM會採用垃圾回收機制統一管理堆內存。與之相對的是堆外內存,存在於JVM管控之外的內存區域,Java中對堆外內存的操作,依賴於Unsafe提供的操作堆外內存的native方法。

使用堆外內存的原因

  • 對垃圾回收停頓的改善。由於堆外內存是直接受操作系統管理而不是JVM,所以當我們使用堆外內存時,即可保持較小的堆內內存規模。從而在GC時減少回收停頓對於應用的影響。
  • 提升程序I/O操作的性能。通常在I/O通信過程中,會存在堆內內存到堆外內存的數據拷貝操作,對於需要頻繁進行內存間數據拷貝且生命週期較短的暫存數據,都建議存儲到堆外內存。

CAS相關

如下源代碼釋義所示,這部分主要爲CAS相關操作的方法。

/**
	*  CAS
  * @param o         包含要修改field的對象
  * @param offset    對象中某field的偏移量
  * @param expected  期望值
  * @param update    更新值
  * @return          true | false
  */
public final native boolean compareAndSwapObject(Object o, long offset,  Object expected, Object update);

public final native boolean compareAndSwapInt(Object o, long offset, int expected,int update);
  
public final native boolean compareAndSwapLong(Object o, long offset, long expected, long update);

什麼是CAS? 即比較並替換,實現併發算法時常用到的一種技術。CAS操作包含三個操作數——內存位置、預期原值及新值。執行CAS操作的時候,將內存位置的值與預期原值比較,如果相匹配,那麼處理器會自動將該位置值更新爲新值,否則,處理器不做任何操作。我們都知道,CAS是一條CPU的原子指令(cmpxchg指令),不會造成所謂的數據不一致問題,Unsafe提供的CAS方法(如compareAndSwapXXX)底層實現即爲CPU指令cmpxchg。

線程調度

這部分,包括線程掛起、恢復、鎖機制等方法。

//取消阻塞線程
public native void unpark(Object thread);
//阻塞線程
public native void park(boolean isAbsolute, long time);
//獲得對象鎖(可重入鎖)
@Deprecated
public native void monitorEnter(Object o);
//釋放對象鎖
@Deprecated
public native void monitorExit(Object o);
//嘗試獲取對象鎖
@Deprecated
public native boolean tryMonitorEnter(Object o);

如上源碼說明中,方法park、unpark即可實現線程的掛起與恢復,將一個線程進行掛起是通過park方法實現的,調用park方法後,線程將一直阻塞直到超時或者中斷等條件出現;unpark可以終止一個掛起的線程,使其恢復正常。

典型應用

Java鎖和同步器框架的核心類AbstractQueuedSynchronizer,就是通過調用LockSupport.park()LockSupport.unpark()實現線程的阻塞和喚醒的,而LockSupport的park、unpark方法實際是調用Unsafe的park、unpark方式來實現。

Class相關

此部分主要提供Class和它的靜態字段的操作相關方法,包含靜態字段內存定位、定義類、定義匿名類、檢驗&確保初始化等。

//獲取給定靜態字段的內存地址偏移量,這個值對於給定的字段是唯一且固定不變的
public native long staticFieldOffset(Field f);
//獲取一個靜態類中給定字段的對象指針
public native Object staticFieldBase(Field f);
//判斷是否需要初始化一個類,通常在獲取一個類的靜態屬性的時候(因爲一個類如果沒初始化,它的靜態屬性也不會初始化)使用。 當且僅當ensureClassInitialized方法不生效時返回false。
public native boolean shouldBeInitialized(Class<?> c);
//檢測給定的類是否已經初始化。通常在獲取一個類的靜態屬性的時候(因爲一個類如果沒初始化,它的靜態屬性也不會初始化)使用。
public native void ensureClassInitialized(Class<?> c);
//定義一個類,此方法會跳過JVM的所有安全檢查,默認情況下,ClassLoader(類加載器)和ProtectionDomain(保護域)實例來源於調用者
public native Class<?> defineClass(String name, byte[] b, int off, int len, ClassLoader loader, ProtectionDomain protectionDomain);
//定義一個匿名類
public native Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches);

典型應用

從Java 8開始,JDK使用invokedynamic及VM Anonymous Class結合來實現Java語言層面上的Lambda表達式。

  • invokedynamic: invokedynamic是Java 7爲了實現在JVM上運行動態語言而引入的一條新的虛擬機指令,它可以實現在運行期動態解析出調用點限定符所引用的方法,然後再執行該方法,invokedynamic指令的分派邏輯是由用戶設定的引導方法決定。
  • VM Anonymous Class:可以看做是一種模板機制,針對於程序動態生成很多結構相同、僅若干常量不同的類時,可以先創建包含常量佔位符的模板類,而後通過Unsafe.defineAnonymousClass方法定義具體類時填充模板的佔位符生成具體的匿名類。生成的匿名類不顯式掛在任何ClassLoader下面,只要當該類沒有存在的實例對象、且沒有強引用來引用該類的Class對象時,該類就會被GC回收。故而VM Anonymous Class相比於Java語言層面的匿名內部類無需通過ClassClassLoader進行類加載且更易回收。

在Lambda表達式實現中,通過invokedynamic指令調用引導方法生成調用點,在此過程中,會通過ASM動態生成字節碼,而後利用Unsafe的defineAnonymousClass方法定義實現相應的函數式接口的匿名類,然後再實例化此匿名類,並返回與此匿名類中函數式方法的方法句柄關聯的調用點;而後可以通過此調用點實現調用相應Lambda表達式定義邏輯的功能。

對象操作

此部分主要包含對象成員屬性相關操作及非常規的對象實例化方式等相關方法。

//返回對象成員屬性在內存地址相對於此對象的內存地址的偏移量
public native long objectFieldOffset(Field f);
//獲得給定對象的指定地址偏移量的值,與此類似操作還有:getInt,getDouble,getLong,getChar等
public native Object getObject(Object o, long offset);
//給定對象的指定地址偏移量設值,與此類似操作還有:putInt,putDouble,putLong,putChar等
public native void putObject(Object o, long offset, Object x);
//從對象的指定偏移量處獲取變量的引用,使用volatile的加載語義
public native Object getObjectVolatile(Object o, long offset);
//存儲變量的引用到對象的指定的偏移量處,使用volatile的存儲語義
public native void putObjectVolatile(Object o, long offset, Object x);
//有序、延遲版本的putObjectVolatile方法,不保證值的改變被其他線程立即看到。只有在field被volatile修飾符修飾時有效
public native void putOrderedObject(Object o, long offset, Object x);
//繞過構造方法、初始化代碼來創建對象
public native Object allocateInstance(Class<?> cls) throws InstantiationException;

典型應用

  • 常規對象實例化方式:我們通常所用到的創建對象的方式,從本質上來講,都是通過new機制來實現對象的創建。但是,new機制有個特點就是當類只提供有參的構造函數且無顯示聲明無參構造函數時,則必須使用有參構造函數進行對象構造,而使用有參構造函數時,必須傳遞相應個數的參數才能完成對象實例化。
  • 非常規的實例化方式:而Unsafe中提供allocateInstance方法,僅通過Class對象就可以創建此類的實例對象,而且不需要調用其構造函數、初始化代碼、JVM安全檢查等。它抑制修飾符檢測,也就是即使構造器是private修飾的也能通過此方法實例化,只需提類對象即可創建相應的對象。由於這種特性,allocateInstance在java.lang.invoke、Objenesis(提供繞過類構造器的對象生成方式)、Gson(反序列化時用到)中都有相應的應用。

put/get

對象成員屬性的內存偏移量獲取,以及字段屬性值的修改,在上面的例子中我們已經測試過了。除了前面的putIntgetInt方法外,Unsafe提供了全部8種基礎數據類型以及Objectputget方法,並且所有的put方法都可以越過訪問權限,直接修改內存中的數據。閱讀openJDK源碼中的註釋發現,基礎數據類型和Object的讀寫稍有不同,基礎數據類型是直接操作的屬性值(value),而Object的操作則是基於引用值(reference value)。下面是Object的讀寫方法:

//在對象的指定偏移地址獲取一個對象引用
public native Object getObject(Object o, long offset);
//在對象指定偏移地址寫入一個對象引用
public native void putObject(Object o, long offset, Object x);

除了對象屬性的普通讀寫外,Unsafe還提供了volatile讀寫有序寫入方法。volatile讀寫方法的覆蓋範圍與普通讀寫相同,包含了全部基礎數據類型和Object類型,以int類型爲例:

//在對象的指定偏移地址處讀取一個int值,支持volatile load語義
public native int getIntVolatile(Object o, long offset);
//在對象指定偏移地址處寫入一個int,支持volatile store語義
public native void putIntVolatile(Object o, long offset, int x);

相對於普通讀寫來說,volatile讀寫具有更高的成本,因爲它需要保證可見性和有序性。在執行get操作時,會強制從主存中獲取屬性值,在使用put方法設置屬性值時,會強制將值更新到主存中,從而保證這些變更對其他線程是可見的。

有序寫入的方法有以下三個:

public native void putOrderedObject(Object o, long offset, Object x);
public native void putOrderedInt(Object o, long offset, int x);
public native void putOrderedLong(Object o, long offset, long x);

有序寫入的成本相對volatile較低,因爲它只保證寫入時的有序性,而不保證可見性,也就是一個線程寫入的值不能保證其他線程立即可見。爲了解決這裏的差異性,需要對內存屏障的知識點再進一步進行補充,首先需要了解兩個指令的概念:

  • Load:將主內存中的數據拷貝到處理器的緩存中
  • Store:將處理器緩存的數據刷新到主內存中

順序寫入與volatile寫入的差別在於,在順序寫時加入的內存屏障類型爲StoreStore類型,而在volatile寫入時加入的內存屏障是StoreLoad類型

在有序寫入方法中,使用的是StoreStore屏障,該屏障確保Store1立刻刷新數據到內存,這一操作先於Store2以及後續的存儲指令操作。而在volatile寫入中,使用的是StoreLoad屏障,該屏障確保Store1立刻刷新數據到內存,這一操作先於Load2及後續的裝載指令,並且,StoreLoad屏障會使該屏障之前的所有內存訪問指令,包括存儲指令和訪問指令全部完成之後,才執行該屏障之後的內存訪問指令。

綜上所述,在上面的三類寫入方法中,在寫入效率方面,按照putputOrderputVolatile的順序效率逐漸降低

allocateInstance

使用Unsafe的allocateInstance方法,允許我們使用非常規的方式進行對象的實例化,首先定義一個實體類,並且在構造函數中對其成員變量進行賦值操作:

@Data
public class A {
    private int b;
    public A(){
        this.b =1;
    }
}

分別基於構造函數、反射以及Unsafe方法的不同方式創建對象進行比較:

public void objTest() throws Exception{
    A a1=new A();
    System.out.println(a1.getB());
    A a2 = A.class.newInstance();
    System.out.println(a2.getB());
    A a3= (A) unsafe.allocateInstance(A.class);
    System.out.println(a3.getB());
}

打印結果分別爲1、1、0,說明通過allocateInstance方法創建對象過程中,不會調用類的構造方法。使用這種方式創建對象時,只用到了Class對象,所以說如果想要跳過對象的初始化階段或者跳過構造器的安全檢查,就可以使用這種方法。在上面的例子中,如果將A類的構造函數改爲private類型,將無法通過構造函數和反射創建對象,但allocateInstance方法仍然有效。

數組相關

這部分主要介紹與數據操作相關的arrayBaseOffset與arrayIndexScale這兩個方法,兩者配合起來使用,即可定位數組中每個元素在內存中的位置。

//返回數組中第一個元素的偏移地址
public native int arrayBaseOffset(Class<?> arrayClass);
//返回數組中一個元素佔用對象頭的大小
public native int arrayIndexScale(Class<?> arrayClass);

Demo:

public class ArrayTest {
    public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        Unsafe unsafe = (Unsafe)theUnsafe.get(null);

        String[] array=new String[]{"str1str1str","str2","str3"};
        int baseOffset = unsafe.arrayBaseOffset(String[].class);
        System.out.println(baseOffset);
        int scale = unsafe.arrayIndexScale(String[].class);
        System.out.println(scale);

        for (int i = 0; i < array.length; i++) {
            int offset=baseOffset+scale*i;
            System.out.println(offset+" : "+unsafe.getObject(array,offset));
        }
    }
}

Output:

16
4
16 : str1str1str
20 : str2
24 : str3

在String數組對象中,對象頭包含3部分,mark word標記字佔用8字節,klass point類型指針佔用4字節,數組對象特有的數組長度部分佔用4字節,總共佔用了16字節。第一個String的引用類型相對於對象的首地址的偏移量是就16,之後每個元素在這個基礎上加4,正好對應了我們上面代碼中的尋址過程,之後再使用前面說過的getObject方法,通過數組對象可以獲得對象在堆中的首地址,再配合對象中變量的偏移量,就能獲得每一個變量的引用。

內存屏障

在Java 8中引入,用於定義內存屏障(也稱內存柵欄,內存柵障,屏障指令等,是一類同步屏障指令,是CPU或編譯器在對內存隨機訪問的操作中的一個同步點,使得此點之前的所有讀寫操作都執行後纔可以開始執行此點之後的操作),避免代碼重排序。

//內存屏障,禁止load操作重排序。屏障前的load操作不能被重排序到屏障後,屏障後的load操作不能被重排序到屏障前
public native void loadFence();
//內存屏障,禁止store操作重排序。屏障前的store操作不能被重排序到屏障後,屏障後的store操作不能被重排序到屏障前
public native void storeFence();
//內存屏障,禁止load、store操作重排序
public native void fullFence();

Demo:

@Slf4j
public class BarrierTest {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        @Getter
        class ChangeThread implements Runnable {
            /** volatile **/
            boolean flag = false;

            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("subThread change flag to:" + flag);
                flag = true;
            }
        }

        ChangeThread changeThread = new ChangeThread();
        new Thread(changeThread).start();

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

        log.info("main thread begin");
        while (true) {
            boolean flag = changeThread.isFlag();
            unsafe.loadFence(); // 加入讀內存屏障
            if (flag) {
                log.info("detected flag changed");
                break;
            }
        }
        log.info("main thread end");
    }
}

Output:

23:59:01.616 [main] INFO com.qhong.disruptor.unsafe.BarrierTest - main thread begin
23:59:04.615 [Thread-0] INFO com.qhong.disruptor.unsafe.BarrierTest - subThread change flag to:false
23:59:04.615 [main] INFO com.qhong.disruptor.unsafe.BarrierTest - detected flag changed
23:59:04.615 [main] INFO com.qhong.disruptor.unsafe.BarrierTest - main thread end

而如果刪掉上面代碼中的loadFence方法,那麼主線程將無法感知到flag發生的變化,會一直在while中循環。

java內存模型(JMM)中,運行中的線程不是直接讀取主內存中的變量的,只能操作自己工作內存中的變量,然後同步到主內存中,並且線程的工作內存是不能共享的。上面demo就是子線程藉助於主內存,將修改後的結果同步給了主線程,進而修改主線程中的工作空間,跳出循環。

系統相關

這部分包含兩個獲取系統相關信息的方法。

//返回系統指針的大小。返回值爲4(32位系統)或 8(64位系統)。
public native int addressSize();  
//內存頁的大小,此值爲2的冪次方。
public native int pageSize();

Unsafe Code

//下面是sun.misc.Unsafe.java類源碼
package sun.misc;
import java.lang.reflect.Field;
/***
 * This class should provide access to low-level operations and its
 * use should be limited to trusted code.  Fields can be accessed using
 * memory addresses, with undefined behaviour occurring if invalid memory
 * addresses are given.
 * 這個類提供了一個更底層的操作並且應該在受信任的代碼中使用。可以通過內存地址
 * 存取fields,如果給出的內存地址是無效的那麼會有一個不確定的運行表現。
 * 
 * @author Tom Tromey ([email protected])
 * @author Andrew John Hughes ([email protected])
 */
public class Unsafe
{
  // Singleton class.
  private static Unsafe unsafe = new Unsafe();
  /***
   * Private default constructor to prevent creation of an arbitrary
   * number of instances.
   * 使用私有默認構造器防止創建多個實例
   */
  private Unsafe()
  {
  }
  /***
   * Retrieve the singleton instance of <code>Unsafe</code>.  The calling
   * method should guard this instance from untrusted code, as it provides
   * access to low-level operations such as direct memory access.
   * 獲取<code>Unsafe</code>的單例,這個方法調用應該防止在不可信的代碼中實例,
   * 因爲unsafe類提供了一個低級別的操作,例如直接內存存取。
   * 
   * @throws SecurityException if a security manager exists and prevents
   *                           access to the system properties.
   *                           如果安全管理器不存在或者禁止訪問系統屬性
   */
  public static Unsafe getUnsafe()
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPropertiesAccess();
    return unsafe;
  }
  
  /***
   * Returns the memory address offset of the given static field.
   * The offset is merely used as a means to access a particular field
   * in the other methods of this class.  The value is unique to the given
   * field and the same value should be returned on each subsequent call.
   * 返回指定靜態field的內存地址偏移量,在這個類的其他方法中這個值只是被用作一個訪問
   * 特定field的一個方式。這個值對於 給定的field是唯一的,並且後續對該方法的調用都應該
   * 返回相同的值。
   *
   * @param field the field whose offset should be returned.
   *              需要返回偏移量的field
   * @return the offset of the given field.
   *         指定field的偏移量
   */
  public native long objectFieldOffset(Field field);
  /***
   * Compares the value of the integer field at the specified offset
   * in the supplied object with the given expected value, and updates
   * it if they match.  The operation of this method should be atomic,
   * thus providing an uninterruptible way of updating an integer field.
   * 在obj的offset位置比較integer field和期望的值,如果相同則更新。這個方法
   * 的操作應該是原子的,因此提供了一種不可中斷的方式更新integer field。
   * 
   * @param obj the object containing the field to modify.
   *            包含要修改field的對象
   * @param offset the offset of the integer field within <code>obj</code>.
   *               <code>obj</code>中整型field的偏移量
   * @param expect the expected value of the field.
   *               希望field中存在的值
   * @param update the new value of the field if it equals <code>expect</code>.
   *           如果期望值expect與field的當前值相同,設置filed的值爲這個新值
   * @return true if the field was changed.
   *                             如果field的值被更改
   */
  public native boolean compareAndSwapInt(Object obj, long offset,
                                          int expect, int update);
  /***
   * Compares the value of the long field at the specified offset
   * in the supplied object with the given expected value, and updates
   * it if they match.  The operation of this method should be atomic,
   * thus providing an uninterruptible way of updating a long field.
   * 在obj的offset位置比較long field和期望的值,如果相同則更新。這個方法
   * 的操作應該是原子的,因此提供了一種不可中斷的方式更新long field。
   * 
   * @param obj the object containing the field to modify.
   *              包含要修改field的對象 
   * @param offset the offset of the long field within <code>obj</code>.
   *               <code>obj</code>中long型field的偏移量
   * @param expect the expected value of the field.
   *               希望field中存在的值
   * @param update the new value of the field if it equals <code>expect</code>.
   *               如果期望值expect與field的當前值相同,設置filed的值爲這個新值
   * @return true if the field was changed.
   *              如果field的值被更改
   */
  public native boolean compareAndSwapLong(Object obj, long offset,
                                           long expect, long update);
  /***
   * Compares the value of the object field at the specified offset
   * in the supplied object with the given expected value, and updates
   * it if they match.  The operation of this method should be atomic,
   * thus providing an uninterruptible way of updating an object field.
   * 在obj的offset位置比較object field和期望的值,如果相同則更新。這個方法
   * 的操作應該是原子的,因此提供了一種不可中斷的方式更新object field。
   * 
   * @param obj the object containing the field to modify.
   *    包含要修改field的對象 
   * @param offset the offset of the object field within <code>obj</code>.
   *         <code>obj</code>中object型field的偏移量
   * @param expect the expected value of the field.
   *               希望field中存在的值
   * @param update the new value of the field if it equals <code>expect</code>.
   *               如果期望值expect與field的當前值相同,設置filed的值爲這個新值
   * @return true if the field was changed.
   *              如果field的值被更改
   */
  public native boolean compareAndSwapObject(Object obj, long offset,
                                             Object expect, Object update);
  /***
   * Sets the value of the integer field at the specified offset in the
   * supplied object to the given value.  This is an ordered or lazy
   * version of <code>putIntVolatile(Object,long,int)</code>, which
   * doesn't guarantee the immediate visibility of the change to other
   * threads.  It is only really useful where the integer field is
   * <code>volatile</code>, and is thus expected to change unexpectedly.
   * 設置obj對象中offset偏移地址對應的整型field的值爲指定值。這是一個有序或者
   * 有延遲的<code>putIntVolatile</cdoe>方法,並且不保證值的改變被其他線程立
   * 即看到。只有在field被<code>volatile</code>修飾並且期望被意外修改的時候
   * 使用纔有用。
   * 
   * @param obj the object containing the field to modify.
   *    包含需要修改field的對象
   * @param offset the offset of the integer field within <code>obj</code>.
   *       <code>obj</code>中整型field的偏移量
   * @param value the new value of the field.
   *      field將被設置的新值
   * @see #putIntVolatile(Object,long,int)
   */
  public native void putOrderedInt(Object obj, long offset, int value);
  /***
   * Sets the value of the long field at the specified offset in the
   * supplied object to the given value.  This is an ordered or lazy
   * version of <code>putLongVolatile(Object,long,long)</code>, which
   * doesn't guarantee the immediate visibility of the change to other
   * threads.  It is only really useful where the long field is
   * <code>volatile</code>, and is thus expected to change unexpectedly.
   * 設置obj對象中offset偏移地址對應的long型field的值爲指定值。這是一個有序或者
   * 有延遲的<code>putLongVolatile</cdoe>方法,並且不保證值的改變被其他線程立
   * 即看到。只有在field被<code>volatile</code>修飾並且期望被意外修改的時候
   * 使用纔有用。
   * 
   * @param obj the object containing the field to modify.
   *    包含需要修改field的對象
   * @param offset the offset of the long field within <code>obj</code>.
   *       <code>obj</code>中long型field的偏移量
   * @param value the new value of the field.
   *      field將被設置的新值
   * @see #putLongVolatile(Object,long,long)
   */
  public native void putOrderedLong(Object obj, long offset, long value);
  /***
   * Sets the value of the object field at the specified offset in the
   * supplied object to the given value.  This is an ordered or lazy
   * version of <code>putObjectVolatile(Object,long,Object)</code>, which
   * doesn't guarantee the immediate visibility of the change to other
   * threads.  It is only really useful where the object field is
   * <code>volatile</code>, and is thus expected to change unexpectedly.
   * 設置obj對象中offset偏移地址對應的object型field的值爲指定值。這是一個有序或者
   * 有延遲的<code>putObjectVolatile</cdoe>方法,並且不保證值的改變被其他線程立
   * 即看到。只有在field被<code>volatile</code>修飾並且期望被意外修改的時候
   * 使用纔有用。
   *
   * @param obj the object containing the field to modify.
   *    包含需要修改field的對象
   * @param offset the offset of the object field within <code>obj</code>.
   *       <code>obj</code>中long型field的偏移量
   * @param value the new value of the field.
   *      field將被設置的新值
   */
  public native void putOrderedObject(Object obj, long offset, Object value);
  /***
   * Sets the value of the integer field at the specified offset in the
   * supplied object to the given value, with volatile store semantics.
   * 設置obj對象中offset偏移地址對應的整型field的值爲指定值。支持volatile store語義
   * 
   * @param obj the object containing the field to modify.
   *    包含需要修改field的對象
   * @param offset the offset of the integer field within <code>obj</code>.
   *       <code>obj</code>中整型field的偏移量
   * @param value the new value of the field.
   *       field將被設置的新值
   */
  public native void putIntVolatile(Object obj, long offset, int value);
  /***
   * Retrieves the value of the integer field at the specified offset in the
   * supplied object with volatile load semantics.
   * 獲取obj對象中offset偏移地址對應的整型field的值,支持volatile load語義。
   * 
   * @param obj the object containing the field to read.
   *    包含需要去讀取的field的對象
   * @param offset the offset of the integer field within <code>obj</code>.
   *       <code>obj</code>中整型field的偏移量
   */
  public native int getIntVolatile(Object obj, long offset);
  /***
   * Sets the value of the long field at the specified offset in the
   * supplied object to the given value, with volatile store semantics.
   * 設置obj對象中offset偏移地址對應的long型field的值爲指定值。支持volatile store語義
   *
   * @param obj the object containing the field to modify.
   *            包含需要修改field的對象
   * @param offset the offset of the long field within <code>obj</code>.
   *               <code>obj</code>中long型field的偏移量
   * @param value the new value of the field.
   *              field將被設置的新值
   * @see #putLong(Object,long,long)
   */
  public native void putLongVolatile(Object obj, long offset, long value);
  /***
   * Sets the value of the long field at the specified offset in the
   * supplied object to the given value.
   * 設置obj對象中offset偏移地址對應的long型field的值爲指定值。
   * 
   * @param obj the object containing the field to modify.
   *     包含需要修改field的對象
   * @param offset the offset of the long field within <code>obj</code>.
   *     <code>obj</code>中long型field的偏移量
   * @param value the new value of the field.
   *     field將被設置的新值
   * @see #putLongVolatile(Object,long,long)
   */
  public native void putLong(Object obj, long offset, long value);
  /***
   * Retrieves the value of the long field at the specified offset in the
   * supplied object with volatile load semantics.
   * 獲取obj對象中offset偏移地址對應的long型field的值,支持volatile load語義。
   * 
   * @param obj the object containing the field to read.
   *    包含需要去讀取的field的對象
   * @param offset the offset of the long field within <code>obj</code>.
   *       <code>obj</code>中long型field的偏移量
   * @see #getLong(Object,long)
   */
  public native long getLongVolatile(Object obj, long offset);
  /***
   * Retrieves the value of the long field at the specified offset in the
   * supplied object.
   * 獲取obj對象中offset偏移地址對應的long型field的值
   * 
   * @param obj the object containing the field to read.
   *    包含需要去讀取的field的對象
   * @param offset the offset of the long field within <code>obj</code>.
   *       <code>obj</code>中long型field的偏移量
   * @see #getLongVolatile(Object,long)
   */
  public native long getLong(Object obj, long offset);
  /***
   * Sets the value of the object field at the specified offset in the
   * supplied object to the given value, with volatile store semantics.
   * 設置obj對象中offset偏移地址對應的object型field的值爲指定值。支持volatile store語義
   * 
   * @param obj the object containing the field to modify.
   *    包含需要修改field的對象
   * @param offset the offset of the object field within <code>obj</code>.
   *     <code>obj</code>中object型field的偏移量
   * @param value the new value of the field.
   *       field將被設置的新值
   * @see #putObject(Object,long,Object)
   */
  public native void putObjectVolatile(Object obj, long offset, Object value);
  /***
   * Sets the value of the object field at the specified offset in the
   * supplied object to the given value.
   * 設置obj對象中offset偏移地址對應的object型field的值爲指定值。
   * 
   * @param obj the object containing the field to modify.
   *    包含需要修改field的對象
   * @param offset the offset of the object field within <code>obj</code>.
   *     <code>obj</code>中object型field的偏移量
   * @param value the new value of the field.
   *       field將被設置的新值
   * @see #putObjectVolatile(Object,long,Object)
   */
  public native void putObject(Object obj, long offset, Object value);
  /***
   * Retrieves the value of the object field at the specified offset in the
   * supplied object with volatile load semantics.
   * 獲取obj對象中offset偏移地址對應的object型field的值,支持volatile load語義。
   * 
   * @param obj the object containing the field to read.
   *    包含需要去讀取的field的對象
   * @param offset the offset of the object field within <code>obj</code>.
   *       <code>obj</code>中object型field的偏移量
   */
  public native Object getObjectVolatile(Object obj, long offset);
  /***
   * Returns the offset of the first element for a given array class.
   * To access elements of the array class, this value may be used along with
   * with that returned by 
   * <a href="#arrayIndexScale"><code>arrayIndexScale</code></a>,
   * if non-zero.
   * 獲取給定數組中第一個元素的偏移地址。
   * 爲了存取數組中的元素,這個偏移地址與<a href="#arrayIndexScale"><code>arrayIndexScale
   * </code></a>方法的非0返回值一起被使用。
   * @param arrayClass the class for which the first element's address should
   *                   be obtained.
   *                   第一個元素地址被獲取的class
   * @return the offset of the first element of the array class.
   *    數組第一個元素 的偏移地址
   * @see arrayIndexScale(Class)
   */
  public native int arrayBaseOffset(Class arrayClass);
  /***
   * Returns the scale factor used for addressing elements of the supplied
   * array class.  Where a suitable scale factor can not be returned (e.g.
   * for primitive types), zero should be returned.  The returned value
   * can be used with 
   * <a href="#arrayBaseOffset"><code>arrayBaseOffset</code></a>
   * to access elements of the class.
   * 獲取用戶給定數組尋址的換算因子.一個合適的換算因子不能返回的時候(例如:基本類型),
   * 返回0.這個返回值能夠與<a href="#arrayBaseOffset"><code>arrayBaseOffset</code>
   * </a>一起使用去存取這個數組class中的元素
   * 
   * @param arrayClass the class whose scale factor should be returned.
   * @return the scale factor, or zero if not supported for this array class.
   */
  public native int arrayIndexScale(Class arrayClass);
  
  /***
   * Releases the block on a thread created by 
   * <a href="#park"><code>park</code></a>.  This method can also be used
   * to terminate a blockage caused by a prior call to <code>park</code>.
   * This operation is unsafe, as the thread must be guaranteed to be
   * live.  This is true of Java, but not native code.
   * 釋放被<a href="#park"><code>park</code></a>創建的在一個線程上的阻塞.這個
   * 方法也可以被使用來終止一個先前調用<code>park</code>導致的阻塞.
   * 這個操作操作時不安全的,因此線程必須保證是活的.這是java代碼不是native代碼。
   * @param thread the thread to unblock.
   *           要解除阻塞的線程
   */
  public native void unpark(Thread thread);
  /***
   * Blocks the thread until a matching 
   * <a href="#unpark"><code>unpark</code></a> occurs, the thread is
   * interrupted or the optional timeout expires.  If an <code>unpark</code>
   * call has already occurred, this also counts.  A timeout value of zero
   * is defined as no timeout.  When <code>isAbsolute</code> is
   * <code>true</code>, the timeout is in milliseconds relative to the
   * epoch.  Otherwise, the value is the number of nanoseconds which must
   * occur before timeout.  This call may also return spuriously (i.e.
   * for no apparent reason).
   * 阻塞一個線程直到<a href="#unpark"><code>unpark</code></a>出現、線程
   * 被中斷或者timeout時間到期。如果一個<code>unpark</code>調用已經出現了,
   * 這裏只計數。timeout爲0表示永不過期.當<code>isAbsolute</code>爲true時,
   * timeout是相對於新紀元之後的毫秒。否則這個值就是超時前的納秒數。這個方法執行時
   * 也可能不合理地返回(沒有具體原因)
   * 
   * @param isAbsolute true if the timeout is specified in milliseconds from
   *                   the epoch.
   *                   如果爲true timeout的值是一個相對於新紀元之後的毫秒數
   * @param time either the number of nanoseconds to wait, or a time in
   *             milliseconds from the epoch to wait for.
   *             可以是一個要等待的納秒數,或者是一個相對於新紀元之後的毫秒數直到
   *             到達這個時間點
   */
  public native void park(boolean isAbsolute, long time);
}

參考:

Java中Unsafe類詳解

Java魔法類:Unsafe應用解析

Java雙刃劍之Unsafe類詳解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章