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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| package in.hocg.lock;
import org.junit.jupiter.api.Test; import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class UnsafeTest { static class Demo { private Integer integer; static { System.out.println("static{}"); } { System.out.println("{}"); } public Demo() { integer = 0; System.out.println("Demo()"); } public Integer getInteger() { System.out.println("getInteger()"); return integer; } public void setInteger(Integer integer) { System.out.println("setInteger()"); this.integer = integer; } }
@Test public void instantiation() throws NoSuchFieldException, IllegalAccessException, InstantiationException { Demo o = ((Demo) getUnsafe().allocateInstance(Demo.class)); System.out.println(String.format("hashCode:: %d", o.hashCode())); }
@Test public void altering() throws NoSuchFieldException, IllegalAccessException { Demo demo = new Demo(); Field field = demo.getClass().getDeclaredField("integer"); Unsafe unsafe = getUnsafe(); unsafe.putObject(demo, unsafe.objectFieldOffset(field), 1); System.out.println(String.format("getInteger() = %d", demo.getInteger())); }
@Test public void throwing() throws NoSuchFieldException, IllegalAccessException { getUnsafe().throwException(new NullPointerException()); }
@Test public void memory() throws NoSuchFieldException, IllegalAccessException { Unsafe unsafe = getUnsafe(); int size = 1024; long address = unsafe.allocateMemory(size * 1); unsafe.putInt(address + 4 * 1, 100); int i = unsafe.getInt(address + 4 * 1); System.out.println(i); unsafe.freeMemory(address); }
@Test public void cas() throws NoSuchFieldException, IllegalAccessException { Unsafe unsafe = getUnsafe(); Demo demo = new Demo(); Field field = demo.getClass().getDeclaredField("integer");
boolean state; Integer integer1 = 0; Integer integer2 = demo.getInteger(); int o1 = 1; while (!(state = unsafe.compareAndSwapObject(demo, unsafe.objectFieldOffset(field), integer1, o1))) { System.out.println("再次尝试修改"); } System.out.println(String.format("1 .CAS State: %b, 认为的旧值 %d, 实际旧值 %d, 期望值 %d", state, integer1, integer2, o1)); integer2 = demo.getInteger(); state = unsafe.compareAndSwapObject(demo, unsafe.objectFieldOffset(field), integer1, o1); System.out.println(String.format("2 .CAS State: %b, 认为的旧值 %d, 实际旧值 %d, 期望值 %d", state, integer1, integer2, o1)); }
@Test public void park() throws NoSuchFieldException, IllegalAccessException, InterruptedException { Unsafe unsafe = getUnsafe(); Thread thread = new Thread(() -> {
unsafe.park(false, 0); }); thread.start(); Thread.sleep(100); System.out.println(String.format("线程 %s 状态 %s", thread.getName(), thread.getState())); Thread.sleep(100); unsafe.unpark(thread); Thread.sleep(100); System.out.println(String.format("线程 %s 状态 %s", thread.getName(), thread.getState())); }
public static Unsafe getUnsafe() throws IllegalAccessException, NoSuchFieldException { Field f = Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); return (Unsafe) f.get(null); } }
|