一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 一篇看懂Java中的Unsafe類

一篇看懂Java中的Unsafe類

2021-05-05 10:42Ye_yang Java教程

在閱讀AtomicInteger的源碼時,看到了這個類:sum.msic.Unsafe,之前從沒見過。所以花了點時間研究了下,下面這篇文章主要給大家介紹了關于Java中Unsafe類的相關資料,需要的朋友可以參考借鑒,下面來一起學習學習吧

前言

本文主要給大家介紹了關于java中unsafe類的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧

1.unsafe類介紹

unsafe類是在sun.misc包下,不屬于java標準。但是很多java的基礎類庫,包括一些被廣泛使用的高性能開發庫都是基于unsafe類開發的,比如netty、hadoop、kafka等。

使用unsafe可用來直接訪問系統內存資源并進行自主管理,unsafe類在提升java運行效率,增強java語言底層操作能力方面起了很大的作用。

unsafe可認為是java中留下的后門,提供了一些低層次操作,如直接內存訪問、線程調度等。

 官方并不建議使用unsafe。

下面是使用unsafe的一些例子。

1.1實例化私有類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.lang.reflect.field;
import sun.misc.unsafe;
public class unsafeplayer {
 public static void main(string[] args) throws exception {
 //通過反射實例化unsafe
 field f = unsafe.class.getdeclaredfield("theunsafe");
 f.setaccessible(true);
 unsafe unsafe = (unsafe) f.get(null);
 //實例化player
 player player = (player) unsafe.allocateinstance(player.class);
 player.setname("li lei");
 system.out.println(player.getname());
 }
}
 
class player{
 private string name;
 private player(){}
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
}

1.2cas操作,通過內存偏移地址修改變量值

java并發包中的synchronousqueue中的transferstack中使用cas更新棧頂。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/ unsafe mechanics
private static final sun.misc.unsafe unsafe;
private static final long headoffset;
static {
 try {
 unsafe = sun.misc.unsafe.getunsafe();
 class<?> k = transferstack.class;
 headoffset = unsafe.objectfieldoffset
  (k.getdeclaredfield("head"));
 } catch (exception e) {
 throw new error(e);
 }
}
//棧頂
volatile snode head;
//更新棧頂
boolean cashead(snode h, snode nh) {
 return h == head &&
 unsafe.compareandswapobject(this, headoffset, h, nh);
}

1.3直接內存訪問

unsafe的直接內存訪問:用unsafe開辟的內存空間不占用heap空間,當然也不具有自動內存回收功能。做到像c一樣自由利用系統內存資源。

2.unsafe類源碼分析

unsafe的大部分api都是native的方法,主要包括以下幾類:

1)class相關。主要提供class和它的靜態字段的操作方法。

2)object相關。主要提供object和它的字段的操作方法。

3)arrray相關。主要提供數組及其中元素的操作方法。

4)并發相關。主要提供低級別同步原語,如cas、線程調度、volatile、內存屏障等。

5)memory相關。提供了直接內存訪問方法(繞過java堆直接操作本地內存),可做到像c一樣自由利用系統內存資源。

6)系統相關。主要返回某些低級別的內存信息,如地址大小、內存頁大小。

2.1class相關

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//靜態屬性的偏移量,用于在對應的class對象中讀寫靜態屬性
public native long staticfieldoffset(field f);
 
public native object staticfieldbase(field f);
//判斷是否需要初始化一個類
public native boolean shouldbeinitialized(class<?> c);
//確保類被初始化
public native void ensureclassinitialized(class<?> c);
//定義一個類,可用于動態創建類
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);

2.2object相關

java中的基本類型(boolean、byte、char、short、int、long、float、double)及對象引用類型都有以下方法。

?
1
2
3
4
5
6
//獲得對象的字段偏移量
public native long objectfieldoffset(field f);
//獲得給定對象地址偏移量的int值
public native int getint(object o, long offset);
//設置給定對象地址偏移量的int值
public native void putint(object o, long offset, int x);
?
1
2
3
//創建對象,但并不會調用其構造方法。如果類未被初始化,將初始化類。
public native object allocateinstance(class<?> cls)
 throws instantiationexception;

2.3數組相關

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * report the offset of the first element in the storage allocation of a
 * given array class. if {@link #arrayindexscale} returns a non-zero value
 * for the same class, you may use that scale factor, together with this
 * base offset, to form new offsets to access elements of arrays of the
 * given class.
 *
 * @see #getint(object, long)
 * @see #putint(object, long, int)
 */
//返回數組中第一個元素的偏移地址
public native int arraybaseoffset(class<?> arrayclass);
//boolean、byte、short、char、int、long、float、double,及對象類型均有以下方法
/** the value of {@code arraybaseoffset(boolean[].class)} */
public static final int array_boolean_base_offset
 = theunsafe.arraybaseoffset(boolean[].class);
 
/**
 * report the scale factor for addressing elements in the storage
 * allocation of a given array class. however, arrays of "narrow" types
 * will generally not work properly with accessors like {@link
 * #getbyte(object, int)}, so the scale factor for such classes is reported
 * as zero.
 *
 * @see #arraybaseoffset
 * @see #getint(object, long)
 * @see #putint(object, long, int)
 */
//返回數組中每一個元素占用的大小
public native int arrayindexscale(class<?> arrayclass);
 
//boolean、byte、short、char、int、long、float、double,及對象類型均有以下方法
/** the value of {@code arrayindexscale(boolean[].class)} */
public static final int array_boolean_index_scale
 = theunsafe.arrayindexscale(boolean[].class);

通過arraybaseoffset和arrayindexscale可定位數組中每個元素在內存中的位置。

2.4并發相關

 2.4.1cas相關

cas:compareandswap,內存偏移地址offset,預期值expected,新值x。如果變量在當前時刻的值和預期值expected相等,嘗試將變量的值更新為x。如果更新成功,返回true;否則,返回false。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//更新變量值為x,如果當前值為expected
//o:對象 offset:偏移量 expected:期望值 x:新值
public final native boolean compareandswapobject(object o, long offset,
       object expected,
       object x);
 
public final native boolean compareandswapint(object o, long offset,
      int expected,
      int x);
 
public final native boolean compareandswaplong(object o, long offset,
      long expected,
      long x);

從java 8開始,unsafe中提供了以下方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//增加
public final int getandaddint(object o, long offset, int delta) {
 int v;
 do {
 v = getintvolatile(o, offset);
 } while (!compareandswapint(o, offset, v, v + delta));
 return v;
}
 
public final long getandaddlong(object o, long offset, long delta) {
 long v;
 do {
 v = getlongvolatile(o, offset);
 } while (!compareandswaplong(o, offset, v, v + delta));
 return v;
}
//設置
public final int getandsetint(object o, long offset, int newvalue) {
 int v;
 do {
 v = getintvolatile(o, offset);
 } while (!compareandswapint(o, offset, v, newvalue));
 return v;
}
 
public final long getandsetlong(object o, long offset, long newvalue) {
 long v;
 do {
 v = getlongvolatile(o, offset);
 } while (!compareandswaplong(o, offset, v, newvalue));
 return v;
}
 
public final object getandsetobject(object o, long offset, object newvalue) {
 object v;
 do {
 v = getobjectvolatile(o, offset);
 } while (!compareandswapobject(o, offset, v, newvalue));
 return v;

2.4.2線程調度相關

?
1
2
3
4
5
6
7
8
9
10
//取消阻塞線程
public native void unpark(object thread);
//阻塞線程
public native void park(boolean isabsolute, long time);
//獲得對象鎖
public native void monitorenter(object o);
//釋放對象鎖
public native void monitorexit(object o);
//嘗試獲取對象鎖,返回true或false表示是否獲取成功
public native boolean trymonitorenter(object o);

2.4.3volatile相關讀寫

java中的基本類型(boolean、byte、char、short、int、long、float、double)及對象引用類型都有以下方法。

?
1
2
3
4
5
6
7
//從對象的指定偏移量處獲取變量的引用,使用volatile的加載語義
//相當于getobject(object, long)的volatile版本
public native object getobjectvolatile(object o, long offset);
 
//存儲變量的引用到對象的指定的偏移量處,使用volatile的存儲語義
//相當于putobject(object, long, object)的volatile版本
public native void putobjectvolatile(object o, long offset, object x);
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * version of {@link #putobjectvolatile(object, long, object)}
 * that does not guarantee immediate visibility of the store to
 * other threads. this method is generally only useful if the
 * underlying field is a java volatile (or if an array cell, one
 * that is otherwise only accessed using volatile accesses).
 */
public native void putorderedobject(object o, long offset, object x);
 
/** ordered/lazy version of {@link #putintvolatile(object, long, int)} */
public native void putorderedint(object o, long offset, int x);
 
/** ordered/lazy version of {@link #putlongvolatile(object, long, long)} */
public native void putorderedlong(object o, long offset, long x);

2.4.4內存屏障相關

java 8引入 ,用于定義內存屏障,避免代碼重排序。

?
1
2
3
4
5
6
//內存屏障,禁止load操作重排序,即屏障前的load操作不能被重排序到屏障后,屏障后的load操作不能被重排序到屏障前
public native void loadfence();
//內存屏障,禁止store操作重排序,即屏障前的store操作不能被重排序到屏障后,屏障后的store操作不能被重排序到屏障前
public native void storefence();
//內存屏障,禁止load、store操作重排序
public native void fullfence();

2.5直接內存訪問(非堆內存)

allocatememory所分配的內存需要手動free(不被gc回收)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//(boolean、byte、char、short、int、long、float、double)都有以下get、put兩個方法。
//獲得給定地址上的int值
public native int getint(long address);
//設置給定地址上的int值
public native void putint(long address, int x);
//獲得本地指針
public native long getaddress(long address);
//存儲本地指針到給定的內存地址
public native void putaddress(long address, long x);
 
//分配內存
public native long allocatememory(long bytes);
//重新分配內存
public native long reallocatememory(long address, long bytes);
//初始化內存內容
public native void setmemory(object o, long offset, long bytes, byte value);
//初始化內存內容
public void setmemory(long address, long bytes, byte value) {
 setmemory(null, address, bytes, value);
}
//內存內容拷貝
public native void copymemory(object srcbase, long srcoffset,
    object destbase, long destoffset,
    long bytes);
//內存內容拷貝
public void copymemory(long srcaddress, long destaddress, long bytes) {
 copymemory(null, srcaddress, null, destaddress, bytes);
}
//釋放內存
public native void freememory(long address);

2.6系統相關

?
1
2
3
4
5
6
7
8
//返回指針的大小。返回值為4或8。
public native int addresssize();
 
/** the value of {@code addresssize()} */
public static final int address_size = theunsafe.addresssize();
 
//內存頁的大小。
public native int pagesize();

3.參考資料

 說一說java中的unsafe類

java魔法類:sun.misc.unsafe

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://www.cnblogs.com/yeyang/p/9074894.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国内精品久久久久香蕉 | 1769在线视频| 亚洲黄色片免费看 | 99这里都是精品 | 99热r| 性欧美高清强烈性视频 | 亚洲成人一区二区 | np高h疯狂黄暴宫口 narutomanga玖辛奈之乳 | 2022天堂岛日产 | 四虎影视在线影院在线观看观看 | 天天爱天天做天天爽天天躁 | 青青草原免费在线视频 | 欧洲美女人牲交一级毛片 | 国产99在线 | 亚洲四虎永久在线播放 | 99热这里只有精品国产免费 | 波多野结衣在线观看视频 | 韩国久久精品 | fc2成人免费共享视频 | 亚洲H成年动漫在线观看不卡 | 国产欧美日韩不卡一区二区三区 | 女人扒开下面让男人桶爽视频 | 国产全部视频 | 香蕉久久夜色精品国产尤物 | 国产成人综合网亚洲欧美在线 | 亚洲欧美激情日韩在线 | 午夜伦理:伦理片 | 教练你好大轻点漫 | 欧美成人免费草草影院视频 | 色呦呦入口| 久久国产精品人妻中文 | 草草在线免费视频 | 国产综合第一页 | 欧美添下面视频免费观看 | 91香蕉依人综合久久 | 无限在线看免费视频大全 | a v在线男人的天堂观看免费 | 国产成人99久久亚洲综合精品 | 国产小情侣自拍 | 日韩精品一区二区三区毛片 | 欧美色图日韩 |