當前位置: 首頁>>代碼示例>>Java>>正文


Java Utils.getUnsafe方法代碼示例

本文整理匯總了Java中com.oracle.java.testlibrary.Utils.getUnsafe方法的典型用法代碼示例。如果您正苦於以下問題:Java Utils.getUnsafe方法的具體用法?Java Utils.getUnsafe怎麽用?Java Utils.getUnsafe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.oracle.java.testlibrary.Utils的用法示例。


在下文中一共展示了Utils.getUnsafe方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: inflateMonitor

import com.oracle.java.testlibrary.Utils; //導入方法依賴的package包/類
/**
 * Inflates monitor associated with object {@code monitor}.
 * Inflation is forced by entering the same monitor from
 * two different threads.
 *
 * @param monitor monitor to be inflated.
 * @return inflated monitor.
 * @throws Exception if something went wrong.
 */
public static Object inflateMonitor(Object monitor) throws Exception {
    Unsafe unsafe = Utils.getUnsafe();
    CyclicBarrier barrier = new CyclicBarrier(2);

    Runnable inflatingRunnable = () -> {
        unsafe.monitorEnter(monitor);
        try {
            barrier.await();
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            throw new RuntimeException(
                    "Synchronization issue occurred.", e);
        } finally {
            unsafe.monitorExit(monitor);
        }
    };

    Thread t = new Thread(inflatingRunnable);
    t.start();
    // Wait until thread t enters the monitor.
    barrier.await();
    // At this point monitor will be owned by thread t,
    // so our attempt to enter the same monitor will force
    // monitor inflation.
    Asserts.assertFalse(unsafe.tryMonitorEnter(monitor),
                        "Not supposed to enter the monitor first");
    barrier.await();
    t.join();
    return monitor;
}
 
開發者ID:infobip,項目名稱:infobip-open-jdk-8,代碼行數:40,代碼來源:AbortProvoker.java

示例2: main

import com.oracle.java.testlibrary.Utils; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
  sun.misc.Unsafe unsafe = Utils.getUnsafe();
  final int array_size = 128;
  final int element_size = 4;
  final int magic = 0x12345678;

  Random rnd = new Random();

  long array = unsafe.allocateMemory(array_size * element_size); // 128 ints
  long addr = array + array_size * element_size / 2; // something in the middle to work with
  unsafe.putInt(addr, magic);
  for (int j = 0; j < 100000; j++) {
     if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception();
     if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception();
     if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception();
     if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception();
     {
       long idx1 = rnd.nextLong();
       long addr1 = addr - (idx1 << 2);
       if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception();
     }
     {
       long idx2 = rnd.nextLong();
       long addr2 = addr - (idx2 >> 2);
       if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception();
     }
     {
       long idx3 = rnd.nextLong();
       long addr3 = addr - (idx3 >>> 2);
       if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception();
     }
     {
       long idx4 = 0x12345678;
       long addr4 = addr - idx4;
       if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception();
     }
     {
       long idx5 = 0x1234567890abcdefL;
       long addr5 = addr - idx5;
       if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception();
     }
     {
       int idx6 = rnd.nextInt();
       long addr6 = addr - (idx6 >> 2);
       if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception();
     }
     {
       int idx7 = rnd.nextInt();
       long addr7 = addr - (idx7 >>> 2);
       if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception();
     }
     {
       int idx8 = rnd.nextInt();
       long addr8 = addr - (idx8 * 16);
       if (Tests.int_index_mul_scale_16(unsafe, addr8, idx8) != magic) throw new Exception();
     }
     {
       long idx9 = rnd.nextLong();
       long addr9 = addr - (idx9 * 16);
       if (Tests.long_index_mul_scale_16(unsafe, addr9, idx9) != magic) throw new Exception();
     }
  }
}
 
開發者ID:ojdkbuild,項目名稱:lookaside_java-1.8.0-openjdk,代碼行數:64,代碼來源:UnsafeRaw.java


注:本文中的com.oracle.java.testlibrary.Utils.getUnsafe方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。