Skip to content

Commit f2f6e4c

Browse files
author
Jossc
committed
还是为你帮他买对车票。
1 parent e1792ca commit f2f6e4c

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.basics.UnsafeTest.unsafe_voliate;
2+
3+
import sun.misc.Unsafe;
4+
5+
import java.lang.reflect.Field;
6+
import java.util.ArrayList;
7+
8+
/**
9+
* @ClassName UnsafeTestTwo
10+
* @Description TODO
11+
* @Author chenzhuo
12+
* @Date 2018/8/22 11:20
13+
* @Version 1.0
14+
*
15+
* pageSize :4096
16+
* address :8 变量指针的长度(4/8 32/64系统)
17+
* anLong :12
18+
**/
19+
public class UnsafeTestTwo {
20+
public static Unsafe unsafe;
21+
public int anInt;
22+
public static long anLong;
23+
static {
24+
try {
25+
Field field = Unsafe.class.getDeclaredField("theUnsafe");
26+
field.setAccessible(true);
27+
unsafe = (Unsafe) field.get(null);
28+
anLong = unsafe.objectFieldOffset(UnsafeTestTwo.class.getDeclaredField("anInt"));
29+
} catch (NoSuchFieldException e) {
30+
e.printStackTrace();
31+
} catch (IllegalAccessException e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
36+
public static void main(String[] args) {
37+
UnsafeTestTwo unsafeTestTwo = new UnsafeTestTwo();
38+
System.out.println(" pageSize :" +unsafe.pageSize());
39+
System.out.println("address :" + unsafe.addressSize());
40+
unsafe.putInt(UnsafeTestTwo.class,anLong,10);
41+
int unsafeInt = unsafe.getInt(UnsafeTestTwo.class, anLong);
42+
System.out.println("anInt: " + unsafeInt);
43+
System.out.println("anLong : " + anLong);
44+
unsafeTestTwo.testSys();
45+
}
46+
47+
public void testSys() {
48+
/* System. out. println("array scale "+unsafe .arrayIndexScale(ArrayList.class));*/
49+
System. out . println("array scale "+unsafe. arrayIndexScale(UnsafeTestTwo[].class));
50+
System. out . println("array scale "+unsafe. arrayIndexScale(Long.class));
51+
System. out . println("array scale "+unsafe . arrayIndexScale(Boolean[].class));
52+
/* System. out . println("array i scale "+unsafe. arrayIndexScale(int[]. class));*/
53+
System. out . println("array scale"+unsafe . arrayIndexScale(Integer.class));
54+
System. out . println("array scale "+unsafe . arrayIndexScale(Byte.class));
55+
}
56+
}
57+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.sun.proxy;
2+
3+
/**
4+
* @ClassName Memony
5+
* @Description TODO
6+
* @Author chenzhuo
7+
* @Date 2018/8/21 18:27
8+
* @Version 1.0
9+
**/
10+
public class Memony {
11+
byte b;
12+
int i ;
13+
boolean aBoolean;
14+
long e;
15+
Object object;
16+
public static void main(String[] args) {
17+
18+
}
19+
}

0 commit comments

Comments
 (0)