<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                下面是Unsafe的主要功能: * 普通讀寫:讀寫一個Object的field;直接從內存地址讀寫 * volatile讀寫:volatile讀寫Object的field,可以保證可見性和有序性 * 有序寫:有序寫Object的field,保證有序性不保證可見性 * 直接內存操作:申請內存;重新申請內存;釋放內存;內存復制 * CAS相關:提供int,long,和Object的CAS操作 * 偏移量相關:獲取對象屬性和靜態屬性的偏移量;獲取數組的arrayBaseOffset和arrayIndexScale * 線程調度:掛起線程,喚醒線程,monitorEnter,monitorExit * 類加載:定義類,創建對象,定義匿名內部類,確保一個類被加載,判斷是否加載一個類 * 內存屏障:讀屏障,寫屏障,讀寫屏障 * [1 獲取Unsafe實例](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#1_Unsafe_14) * [2 普通讀寫](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#2__25) * [3 volatile讀寫](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#3_volatile_96) * [4 有序寫](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#4__103) * [5 直接內存操作](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#5__109) * [6 compareAndSwap相關](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#6_compareAndSwap_118) * [7 偏移量相關](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#7__242) * [8 線程調度](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#8__251) * [9 類加載](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#9__262) * [10 內存屏障](http://www.hmoore.net/book/zhaoxuyang/blog/fc815341991a4bf102a30860315fff449c706ec3/preview/%E7%90%86%E8%A7%A3Unsafe.md#10__276) ## 1 獲取Unsafe實例 通過反射獲取Unsafe實例: ~~~ public static Unsafe getUnsafe() throws IllegalAccessException { Field unsafeField = Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible(true); Unsafe unsafe = (Unsafe) unsafeField.get(null); return unsafe; } ~~~ ## 2 普通讀寫 ~~~ // 讀寫一個Object屬性的相關方法 public native int getInt(Object o, long l); public native void putInt(Object o, long l, int i); // 讀寫內存地址屬性的相關方法 public native int getInt(long l); public native void putInt(long l, int i); ~~~ 下面是測試用例: ~~~ package sun.misc; import java.lang.reflect.Field; /** * * @author zhaoxuyang */ public class UnsafeTest { public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException { Model m = new Model(1, "zhangsan", 22); sun.misc.Unsafe unsafe = getUnsafe(); System.out.println(unsafe.toString()); Class<?> c = Model.class; long idOffset = unsafe.objectFieldOffset(c.getDeclaredField("id")); long nameOffset = unsafe.objectFieldOffset(c.getDeclaredField("name")); long ageOffset = unsafe.objectFieldOffset(c.getDeclaredField("age")); System.out.println("idOffset: " + idOffset); System.out.println("nameOffset: " + nameOffset); System.out.println("ageOffset: " + ageOffset); // 讀寫一個Object屬性的相關方法 int getIntId = unsafe.getInt(m, idOffset); int getIntName = unsafe.getInt(m, nameOffset); int getIntAge = unsafe.getInt(m, ageOffset); System.out.println(getIntId); System.out.println(getIntName); System.out.println(getIntAge); unsafe.putInt(m, ageOffset, getIntAge + 1); System.out.println(m.age); } public static Unsafe getUnsafe() throws IllegalAccessException { Field unsafeField = Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible(true); Unsafe unsafe = (Unsafe) unsafeField.get(null); return unsafe; } private static class Model { long id; String name; int age; Model(long id, String name, int age) { this.id = id; this.name = name; this.age = age; } } } ~~~ ## 3 volatile讀寫 ~~~ // 普通的讀寫無法保證可見性和有序性,而volatile讀寫就可以保證可見性和有序性。 public native int getIntVolatile(Object o, long l); public native void putIntVolatile(Object o, long l, int i); ~~~ ## 4 有序寫 ~~~ // 有序寫入只保證寫入的有序性,不保證可見性,即一個線程的寫入不保證其他線程立馬可見,但是效率比volatile高。 public native void putOrderedInt(Object o, long l, int i); ~~~ ## 5 直接內存操作 ~~~ public native long allocateMemory(long l); // 分配內存 public native long reallocateMemory(long l, long l1); // 重新分配內存 public native void setMemory(Object o, long l, long l1, byte b); // 內存初始化 public native void copyMemory(Object o, long l, Object o1, long l1, long l2); // 內存復制 public native void freeMemory(long l); // 清除內存 ~~~ ## 6 compareAndSwap相關 Unsafe中提供了int,long和Object的CAS操作: ~~~ public final native boolean compareAndSwapObject(Object o, long l, Object o1, Object o2); public final native boolean compareAndSwapInt(Object o, long l, int i, int i1); public final native boolean compareAndSwapLong(Object o, long l, long l1, long l2); ~~~ CAS示例如下: ~~~ package sun.misc; import java.lang.reflect.Field; /** * * @author zhaoxuyang */ public class CasDemo{ private volatile int value; private static final long valueOffset; private final static Unsafe unsafe; static { try { unsafe = getUnsafe(); valueOffset = unsafe.objectFieldOffset(CasDemo.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } } private static Unsafe getUnsafe() throws IllegalAccessException { Field unsafeField = Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible(true); Unsafe unsafe = (Unsafe) unsafeField.get(null); return unsafe; } public final boolean cas(int expect, int update){ return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } public final int getAndIncr(){ return unsafe.getAndAddInt(this, valueOffset, 1); } public final int getAndDecr(){ return unsafe.getAndAddInt(this, valueOffset, -1); } public final int getValue(){ return value; } } ~~~ 下面的例子將實現兩個線程交替打印奇偶數: ~~~ package sun.misc; /** * * @author zhaoxuyang */ public class CasDemoTest { public static void main(String[] args) throws InterruptedException { CasDemo casDemo = new CasDemo(); System.out.println(casDemo.getValue()); Thread t1 = new Thread(() -> { while(true){ int value = casDemo.getValue(); if(value > 10){ break; } if(value % 2 == 1){ System.out.println("線程1 " + value); casDemo.cas(value, value + 1); } } }); Thread t2 = new Thread(() -> { while(true){ int value = casDemo.getValue(); if(value > 10){ break; } if(value % 2 == 0){ System.out.println("線程2 " + value); casDemo.cas(value, value + 1); } } }); t1.start(); t2.start(); Thread.sleep(200); System.out.println(casDemo.getValue()); } } ~~~ 輸出如下: ~~~ run: 0 線程2 0 線程1 1 線程2 2 線程1 3 線程2 4 線程1 5 線程2 6 線程1 7 線程2 8 線程1 9 線程2 10 11 ~~~ ## 7 偏移量相關 ~~~ public native long staticFieldOffset(Field field); // 獲取靜態屬性Field在對象中的偏移量 public native long objectFieldOffset(Field field); // 非靜態屬性Field在對象實例中的偏移量 public native Object staticFieldBase(Field field); // 返回Field所在的對象 public native int arrayBaseOffset(Class<?> type); // 返回數組中第一個元素實際地址相對數組地址的偏移量 public native int arrayIndexScale(Class<?> type); //計算數組中第一個元素所占用的內存空間 ~~~ ## 8 線程調度 ~~~ public native void park(boolean bln, long l); // 掛起線程 public native void unpark(Object o); // 喚醒線程 // 用于加鎖,synchronized便是通過以下指令實現 @Deprecated public native void monitorEnter(Object o); @Deprecated public native void monitorExit(Object o); @Deprecated public native boolean tryMonitorEnter(Object o); ~~~ ## 9 類加載 ~~~ // 定義一個類,用于動態地創建類 public native Class<?> defineClass(String string, byte[] bytes, int i, int i1, ClassLoader cl, ProtectionDomain pd); // 用于動態地創建一個匿名內部類 public native Class<?> defineAnonymousClass(Class<?> type, byte[] bytes, Object[] os); // 用于創建一個類的實例,但是不會調用這個實例的構造方法,如果這個類還未被初始化,則初始化這個類。 public native Object allocateInstance(Class<?> type) throws InstantiationException; // 用于判斷是否需要初始化一個類 public native boolean shouldBeInitialized(Class<?> type); // 用于保證已經初始化過一個類 public native void ensureClassInitialized(Class<?> type); ~~~ ## 10 內存屏障 ~~~ public native void loadFence(); // 保證在這個屏障之前的所有【讀】操作都已經完成 public native void storeFence(); // 【寫】 public native void fullFence(); // 【讀寫】 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看