自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

【揭秘】JUC并發(fā)工具包底層機(jī)制探究,Unsafe原來這么強(qiáng)大!

開發(fā) 前端
在程序中過度、不正確使用Unsafe類會使得程序出錯的概率變大,使得Java這種安全的語言變得不再“安全”,因此對Unsafe的使用一定要慎重。java.util.concurrent.atomic包下的原子操作類,基本都是使用Unsafe實(shí)現(xiàn)的。

Unsafe介紹

Unsafe是位于sun.misc包下的一個類,主要提供一些用于執(zhí)行低級別、不安全操作的方法,如直接訪問系統(tǒng)內(nèi)存資源、自主管理內(nèi)存資源等,這些方法在提升Java運(yùn)行效率、增強(qiáng)Java語言底層資源操作能力方面起到了很大的作用。但由于Unsafe類使得Java語言擁有了類似C語言指針一樣操作內(nèi)存空間的能力,這無疑也增加了程序發(fā)生相關(guān)指針問題的風(fēng)險。在程序中過度、不正確使用Unsafe類會使得程序出錯的概率變大,使得Java這種安全的語言變得不再“安全”,因此對Unsafe的使用一定要慎重。java.util.concurrent.atomic包下的原子操作類,基本都是使用Unsafe實(shí)現(xiàn)的。

Unsafe提供的API大致可分為內(nèi)存操作、CAS、Class、對象操作、線程、系統(tǒng)信息獲取、內(nèi)存屏障、數(shù)組操作等幾類。

內(nèi)存相關(guān)

圖片圖片

CAS相關(guān)

圖片圖片

java.util.concurrent.atomic包中的原子類基本都用的Unsafe

private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
static {
  try {
    valueOffset = unsafe.objectFieldOffset(AtomicInteger.class.getDeclaredField("value"));
  } catch (Exception ex) { throw new Error(ex); }
}
public final int getAndSet(int newValue) {
  return unsafe.getAndSetInt(this, valueOffset, newValue);
}

線程相關(guān)

圖片圖片

LockSupport類中有應(yīng)用unpark,park

public static void park(Object blocker) {
  Thread t = Thread.currentThread();
  setBlocker(t, blocker);
  UNSAFE.park(false, 0L);
  setBlocker(t, null);
}
public static void unpark(Thread thread) {
  if (thread != null)
    UNSAFE.unpark(thread);
}

Class相關(guān)

圖片圖片

對象操作相關(guān)

圖片圖片

圖片圖片

系統(tǒng)相關(guān)

圖片圖片

內(nèi)存屏障

圖片圖片

loadFence:保證在這個屏障之前的所有讀操作都已經(jīng)完成。
storeFence:保證在這個屏障之前的所有寫操作都已經(jīng)完成。fullFence:保證在這個屏障之前的所有讀寫操作都已經(jīng)完成。

在java8中 有這個StampedLock類,該類中應(yīng)用了內(nèi)存屏障功能。

private static final sun.misc.Unsafe U;
static {
  try {
    U = sun.misc.Unsafe.getUnsafe();
  } catch (Exception e) {
    throw new Error(e);
  }
}
public boolean validate(long stamp) {
  U.loadFence();
  return (stamp & SBITS) == (state & SBITS);
}

Unsafe.java

public final class Unsafe {


  private static native void registerNatives();
  static {
    registerNatives();
    sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
  }


  private Unsafe() {}


  private static final Unsafe theUnsafe = new Unsafe();
  // ...
}

獲取Unsafe實(shí)例

Unsafe類是final且是單例的,并且theUnsafe字段是private;通過如下方法獲取實(shí)例。

方法1

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

方法2

private static Unsafe unsafe = null ;


static {
  try {
    Constructor<Unsafe> cons = Unsafe.class.getDeclaredConstructor() ;
    cons.setAccessible(true) ;
    unsafe = cons.newInstance() ;
  } catch (Exception e) {
    e.printStackTrace();
  }
}

Unsafe簡單應(yīng)用

int i = 0 ;


public static void main(String[] args) throws Exception {
  UnsafeDemo d = new UnsafeDemo() ;
  // 獲取Unsafe實(shí)例
  Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe") ;
  theUnsafe.setAccessible(true) ;
  Unsafe unsafe = (Unsafe) theUnsafe.get(null) ;
  // 獲取類的實(shí)例變量
  Field f = UnsafeDemo.class.getDeclaredField("i") ;
  // 獲取字段相對Java對象的"起始地址"的偏移量
  long fieldOffset = unsafe.objectFieldOffset(f) ;
  System.out.println(fieldOffset) ;
  // 設(shè)置值
  boolean success = unsafe.compareAndSwapInt(d, fieldOffset, 0, 10) ;
  System.out.println(success) ;
  System.out.println(d.i) ;
}

Unsafe對象操作

private static Unsafe unsafe = null ;


static {
try {
    Constructor<Unsafe> cons = Unsafe.class.getDeclaredConstructor() ;
    cons.setAccessible(true) ;
    unsafe = cons.newInstance() ;
  } catch (Exception e) {
    e.printStackTrace();
  }
}
public static void allocate() {
  try {
    Person p = (Person)unsafe.allocateInstance(Person.class) ;
    p.setId("s001");
    System.out.println(p.getValue()) ;
    System.out.println(p.getId()) ;
  } catch (Exception e) {
    e.printStackTrace();
  }
}

執(zhí)行結(jié)果:

圖片圖片

對象操作2:

private Person p = new Person("1", "張三") ;


public static void main(String[] args) throws Exception {
  UnSafeObjectDemo d = new UnSafeObjectDemo() ;
  Field field = Unsafe.class.getDeclaredField("theUnsafe") ;
  field.setAccessible(true) ;
  Unsafe unsafe = (Unsafe) field.get(null) ;
  Field f = d.getClass().getDeclaredField("p") ;
  long offset = unsafe.objectFieldOffset(f) ;
  System.out.println(offset) ;
  boolean res = unsafe.compareAndSwapObject(d, offset, d.p, new Person("2", "李四")) ;
  System.out.println(res) ;
  System.out.println(d.p.getName()) ;
}

圖片圖片

Unsafe創(chuàng)建對象

當(dāng)不知道即將使用的對象有何構(gòu)造函數(shù),或是不想使用現(xiàn)有對象的構(gòu)造函數(shù)創(chuàng)建對象時,可以通過如下方式:

Constructor<Teacher> cons = (Constructor<Teacher>) ReflectionFactory
    .getReflectionFactory()
    .newConstructorForSerialization(Teacher.class, Object.class.getConstructor());
cons.setAccessible(true) ;
Teacher t = cons.newInstance() ;
System.out.println(t) ;

Unsafe簡單實(shí)現(xiàn)原子操作類

public class AtomicCount {


  private static Unsafe unsafe ;


  private int value ;
  private static long valueOffset ;


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


      Field f = AtomicCount.class.getDeclaredField("value") ;
      valueOffset = unsafe.objectFieldOffset(f) ;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  public AtomicCount(int value) {
    this.value = value ;
  }


  public final int get() {
    return value;
  }


  public final int getAndIncrement() {
    return unsafe.getAndAddInt(this, valueOffset, 1);
  }


}

完畢?。。?/p>

責(zé)任編輯:武曉燕 來源: Spring全家桶實(shí)戰(zhàn)案例源碼
相關(guān)推薦

2009-11-17 14:39:59

PHP工具包

2009-02-03 14:00:20

PHP運(yùn)行PHP調(diào)用PHP原理

2016-08-12 09:33:38

2020-12-15 07:54:40

工具Hutoolgithub

2020-12-15 15:08:17

工具Java線程

2024-12-17 08:28:30

2021-04-19 07:35:01

Linuxhistory命令

2021-04-26 10:24:52

Linux 開發(fā)操作系統(tǒng)

2025-01-28 00:00:00

OpenFeign接口依賴

2022-04-09 10:02:59

HTTPLInux開源

2018-05-03 09:03:16

微軟工具包Windows

2021-11-17 10:16:39

Kodex隱私安全工程工具包

2022-06-01 12:04:02

項(xiàng)目Webpack

2021-04-19 05:42:51

Mmap文件系統(tǒng)

2011-04-18 09:46:58

Windows工具包活動目錄MPS報告

2020-02-10 16:07:42

工具包

2009-09-16 08:34:17

Windows Emb

2009-04-03 11:12:43

PalmwebOS開發(fā)

2023-02-15 08:26:29

iOS工具

2023-11-01 14:49:07

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號