From 62705a295ff57961d33d687af9a2b294f3dacd58 Mon Sep 17 00:00:00 2001 From: Air3 Date: Sat, 14 Mar 2020 19:48:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=91=A8=E4=BD=9C=E4=B8=9A-8?= =?UTF-8?q?5=E5=8F=B7:=20AtomicInteger,Atomicstampedreference;=20Unsafe;St?= =?UTF-8?q?riped64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../week_02/85/AtomicInteger/AtomicInteger.md | 7 + .../AtomicStampedReference.md | 5 + second/week_02/85/Striped64/Stripe64.md | 8 + second/week_02/85/Unsafe/Student.java | 35 ++++ second/week_02/85/Unsafe/Unsafe.md | 7 + second/week_02/85/Unsafe/UnsafeTest.java | 152 ++++++++++++++++++ 6 files changed, 214 insertions(+) create mode 100644 second/week_02/85/AtomicInteger/AtomicInteger.md create mode 100644 second/week_02/85/AtomicStampedReference/AtomicStampedReference.md create mode 100644 second/week_02/85/Striped64/Stripe64.md create mode 100644 second/week_02/85/Unsafe/Student.java create mode 100644 second/week_02/85/Unsafe/Unsafe.md create mode 100644 second/week_02/85/Unsafe/UnsafeTest.java diff --git a/second/week_02/85/AtomicInteger/AtomicInteger.md b/second/week_02/85/AtomicInteger/AtomicInteger.md new file mode 100644 index 0000000..30a2038 --- /dev/null +++ b/second/week_02/85/AtomicInteger/AtomicInteger.md @@ -0,0 +1,7 @@ +# AtomicInteger + + +[深入解析Java AtomicInteger原子类型 - 简书](https://www.jianshu.com/p/932a66737076) +[详解java并发原子类AtomicInteger(基于jdk1.8源码分析) - 简书](https://www.jianshu.com/p/bcc6c0f9990f) + + diff --git a/second/week_02/85/AtomicStampedReference/AtomicStampedReference.md b/second/week_02/85/AtomicStampedReference/AtomicStampedReference.md new file mode 100644 index 0000000..9d9aa8a --- /dev/null +++ b/second/week_02/85/AtomicStampedReference/AtomicStampedReference.md @@ -0,0 +1,5 @@ +# AtomicStampedReference + +[J.U.C 原子类系列之AtomicReference、AtomicStampedReference、AtomicMarkableReference - 简书](https://www.jianshu.com/p/eb1a97b0a1b0) +[J.U.C 原子类系列之AtomicReference、AtomicStampedReference、AtomicMarkableReference - 简书](https://www.jianshu.com/p/eb1a97b0a1b0) +[J.U.C 原子类系列之AtomicReference、AtomicStampedReference、AtomicMarkableReference - 简书](https://www.jianshu.com/p/eb1a97b0a1b0) diff --git a/second/week_02/85/Striped64/Stripe64.md b/second/week_02/85/Striped64/Stripe64.md new file mode 100644 index 0000000..3a29632 --- /dev/null +++ b/second/week_02/85/Striped64/Stripe64.md @@ -0,0 +1,8 @@ +# Unsafe + + +[【Java源码计划】Striped64](https://www.jianshu.com/p/9a7de5644dd4) + + + + diff --git a/second/week_02/85/Unsafe/Student.java b/second/week_02/85/Unsafe/Student.java new file mode 100644 index 0000000..e4a9e0b --- /dev/null +++ b/second/week_02/85/Unsafe/Student.java @@ -0,0 +1,35 @@ +package wangzheng.practice1.Unsafe; + +public class Student { + private String name; + private int age; + public static String info; + + static { + info = "һѧ"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student() { + } +} \ No newline at end of file diff --git a/second/week_02/85/Unsafe/Unsafe.md b/second/week_02/85/Unsafe/Unsafe.md new file mode 100644 index 0000000..934646b --- /dev/null +++ b/second/week_02/85/Unsafe/Unsafe.md @@ -0,0 +1,7 @@ +# Unsafe +[Unsafe类源码解析 - 简书](https://www.jianshu.com/p/c033bdcbf309) +[Java魔法类:Unsafe应用解析 - 简书](https://www.jianshu.com/p/5dba0bd63429) +[CAS算法 - 简书](https://www.jianshu.com/p/ae00ab3fe932) + + + diff --git a/second/week_02/85/Unsafe/UnsafeTest.java b/second/week_02/85/Unsafe/UnsafeTest.java new file mode 100644 index 0000000..63e5ebc --- /dev/null +++ b/second/week_02/85/Unsafe/UnsafeTest.java @@ -0,0 +1,152 @@ +package wangzheng.practice1.Unsafe; + +import org.junit.Test; +import sun.misc.Unsafe; + +import java.lang.reflect.Field; + +public class UnsafeTest { + + @Test + public void testGetFieldOffset() throws NoSuchFieldException, IllegalAccessException { + final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); + + //ȡֶƫ + long address = unsafe.objectFieldOffset(Student.class.getDeclaredField("name")); + System.out.println(address); + + //ȡֶ̬ƫ + long staticAddress = unsafe.staticFieldOffset(Student.class.getDeclaredField("info")); + System.out.println(staticAddress); + } + + + @Test + public void testMemoryOperate() throws NoSuchFieldException, IllegalAccessException { + final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); + //ڴ + long address = unsafe.allocateMemory(4); + System.out.println(address); + //չڴ + long newAddress = unsafe.reallocateMemory(address, 8); + System.out.println(newAddress); + //ͷڴ + unsafe.freeMemory(newAddress); + } + + + @Test + public void testGetAndPutInt() throws NoSuchFieldException, IllegalAccessException { + final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); + + Student student = new Student("liming", 16); + //ͨobjectFieldOffsetȡѧڴеƫ + long offset = unsafe.objectFieldOffset(Student.class.getDeclaredField("age")); + System.out.println(offset); + //ͨڴķʽȡѧ + int anInt = unsafe.getInt(student, offset); + System.out.println(anInt); + System.out.println(student.getAge()); + //ͨڴķʽ޸ѧ + unsafe.putInt(student, offset, 30); + System.out.println(student.getAge()); + } + + + @Test + public void testGetAndPutInt2() throws NoSuchFieldException, IllegalAccessException { + final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); + + //ͨallocateMemoryһ88ֽڵڴռ + long address = unsafe.allocateMemory(8); + + //ָڴռдһֵ + unsafe.putInt(address,60); + //ȡڴռֵ + System.out.println(unsafe.getInt(address)); + + //·ڴ棬16ֽ + final long newAddress = unsafe.reallocateMemory(address, 16); + unsafe.putInt(newAddress,100); + System.out.println(unsafe.getInt(newAddress)); + + //շڴռ + unsafe.freeMemory(newAddress); + } + + @Test + public void testArrayOperate() throws NoSuchFieldException, IllegalAccessException { + final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); + + int[] array = new int[]{1,5,8,9,6,4,7}; + + //ȡеһԪڴƫƵַ + int offset = unsafe.arrayBaseOffset(array.getClass()); + //ȡԪڴеĵַ + int indexScale = unsafe.arrayIndexScale(array.getClass()); + + //ͨڴַȡĵһԪ + System.out.println(unsafe.getInt(array,offset)); + //ͨڴ޸ĵһԪ + unsafe.putInt(array,offset,3); + System.out.println(unsafe.getInt(array,offset)); + //ͨڴȡĵԪأarrayBaseOffsetarrayIndexScaleͬʹãͿԻһԪڴĵַ + System.out.println(unsafe.getInt(array,indexScale*2+offset)); + + //ͨѭԪ + for (int i = 0; i < array.length; i++) { + System.out.print(unsafe.getInt(array,i*indexScale+offset) + " "); + } + } + + @Test + public void testGetAndPutAddress() throws NoSuchFieldException, IllegalAccessException { + final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); + + //ͨallocateMemoryһ88ֽڵڴռ + long address = unsafe.allocateMemory(8); + + //ڴдһָ + unsafe.putAddress(address,200); + //ȡָڴַָ + System.out.println(unsafe.getAddress(address)); + //ӡͨputAddressŵıָĴС + System.out.println(unsafe.addressSize()); + //ӡڴҳĴСͨΪ4k,4096 //ڴҳ治˽ĿԿ²ϵͳص֪ʶ + System.out.println(unsafe.pageSize()); + //ͷڴ + unsafe.freeMemory(address); + } + + @Test + public void testCAS() throws NoSuchFieldException, IllegalAccessException { + final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Unsafe unsafe = (Unsafe) theUnsafe.get(null); + + long offset = unsafe.objectFieldOffset(Student.class.getDeclaredField("age")); + + unsafe.putInt(Student.class,offset,25); + System.out.println(unsafe.getInt(Student.class,offset)); + //ͨUnsafeṩӲԭӲ CAS㷨ѧ + unsafe.compareAndSwapInt(Student.class,offset,24,35); + System.out.println(unsafe.getInt(Student.class,offset)); + + unsafe.compareAndSwapInt(Student.class,offset,25,35); + System.out.println(unsafe.getInt(Student.class,offset)); + } + + +} -- Gitee