diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4f54c08
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,35 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**
+!**/src/test/**
+
+.idea/
+hy-lyxxfb-information-issuing-platform.iml
+.settings
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+*.hprof
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+
+### VS Code ###
+.vscode/
+
+src/main/.DS_Store
+src/main/java/.DS_Store
+src/main/java/net/.DS_Store
+src/main/java/net/cdsunrise/.DS_Store
+src/main/java/net/cdsunrise/hy/.DS_Store
+src/main/java/net/cdsunrise/hy/lyxxfb/.DS_Store
+src/main/java/net/cdsunrise/hy/lyxxfb/iip/.DS_Store
+src/main/resources/.DS_Store
+
diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml
deleted file mode 100644
index 3364bae..0000000
--- a/.idea/checkstyle-idea.xml
+++ /dev/null
@@ -1,16 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
deleted file mode 100644
index 217af47..0000000
--- a/.idea/compiler.xml
+++ /dev/null
@@ -1,23 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/.idea/description.html b/.idea/description.html
deleted file mode 100644
index db5f129..0000000
--- a/.idea/description.html
+++ /dev/null
@@ -1 +0,0 @@
-Simple Java application that includes a class with main() method
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
deleted file mode 100644
index ada92a5..0000000
--- a/.idea/encodings.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
deleted file mode 100644
index 6560a98..0000000
--- a/.idea/inspectionProfiles/Project_Default.xml
+++ /dev/null
@@ -1,36 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index c9209a5..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index e04ce1a..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 94a25f7..0000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
deleted file mode 100644
index 2149637..0000000
--- a/.idea/workspace.xml
+++ /dev/null
@@ -1,139 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1562850730821
-
-
- 1562850730821
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Java-Interview-Advanced.iml b/Java-Interview-Advanced.iml
deleted file mode 100644
index d5c0743..0000000
--- a/Java-Interview-Advanced.iml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/docs/distributed-system/code/code.zip b/docs/distributed-system/code/code.zip
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/code/code2.zip b/docs/distributed-system/code/code2.zip
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/code/code4.zip b/docs/distributed-system/code/code4.zip
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/SpringCloud-core-architecture.png b/docs/distributed-system/images/SpringCloud-core-architecture.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/distributed-lock.png b/docs/distributed-system/images/distributed-lock.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/dubbo-framework-principle.png b/docs/distributed-system/images/dubbo-framework-principle.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/dubbo-rock-bottom.png b/docs/distributed-system/images/dubbo-rock-bottom.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/eureka-register.png b/docs/distributed-system/images/eureka-register.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/gateway-high-concurrency.png b/docs/distributed-system/images/gateway-high-concurrency.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/redis-distribute-lock.png b/docs/distributed-system/images/redis-distribute-lock.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/registration-center-optimize.png b/docs/distributed-system/images/registration-center-optimize.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/rocketmq-transaction.png b/docs/distributed-system/images/rocketmq-transaction.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/springCloud-study-theory.png b/docs/distributed-system/images/springCloud-study-theory.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/zookeeper-distribute-lock-optimize.png b/docs/distributed-system/images/zookeeper-distribute-lock-optimize.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/zookeeper-distribute-lock.png b/docs/distributed-system/images/zookeeper-distribute-lock.png
old mode 100755
new mode 100644
diff --git a/docs/distributed-system/images/zookeeper-register.png b/docs/distributed-system/images/zookeeper-register.png
old mode 100755
new mode 100644
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..6dbbf12
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,21 @@
+
+
+ 4.0.0
+ net.cdsunrise.interview.advanced
+ getoffer
+ 1.0
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.12
+ provided
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/atguigu/interview/juc/ABADemo.java b/src/main/java/com/atguigu/interview/juc/ABADemo.java
new file mode 100644
index 0000000..78b5fff
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/ABADemo.java
@@ -0,0 +1,54 @@
+package com.atguigu.interview.juc;
+
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.atomic.AtomicStampedReference;
+
+/**
+ * @author
+ */
+public class ABADemo {
+ static AtomicReference atomicReference = new AtomicReference<>(100);
+ static AtomicStampedReference atomicStampedReference = new AtomicStampedReference<>(100,1);
+ public static void main(String[] args) {
+ System.out.println("----------------ABA Demo------------------");
+ new Thread(() -> {
+ atomicReference.compareAndSet(100, 101);
+ atomicReference.compareAndSet(101, 100);
+ }, "t1").start();
+
+ new Thread(() -> {
+ try {
+ // 保证t1线程完成一次ABA
+ Thread.sleep(1000L);
+ System.out.println(atomicReference.compareAndSet(100, 102) +
+ "\t" + atomicReference.get());
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }, "t2").start();
+
+ System.out.println("----------------ABA Solution------------------");
+ new Thread(() -> {
+ int stamp = atomicStampedReference.getStamp();
+ System.out.println(Thread.currentThread().getName() + "\t第1次版本号" + stamp);
+ try {Thread.sleep(1000L);} catch (Exception e) {e.printStackTrace();}
+ atomicStampedReference.compareAndSet(100, 101,
+ atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
+ System.out.println(Thread.currentThread().getName() + "\t第2次版本号" + atomicStampedReference.getStamp());
+ atomicStampedReference.compareAndSet(101, 100,
+ atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
+ System.out.println(Thread.currentThread().getName() + "\t第3次版本号" + atomicStampedReference.getStamp());
+
+ }, "t3").start();
+
+ new Thread(() -> {
+ int stamp = atomicStampedReference.getStamp();
+ System.out.println(Thread.currentThread().getName() + "\t第1次版本号" + stamp);
+ try {Thread.sleep(3000L);} catch (Exception e) {e.printStackTrace();}
+ boolean b = atomicStampedReference.compareAndSet(100, 102,
+ stamp, stamp + 1);
+ System.out.println(Thread.currentThread().getName()+"\t 修改成功:"+b);
+ }, "t4").start();
+
+ }
+}
diff --git a/src/main/java/com/atguigu/interview/juc/AtomicReferenceDemo.java b/src/main/java/com/atguigu/interview/juc/AtomicReferenceDemo.java
new file mode 100644
index 0000000..10c9ca8
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/AtomicReferenceDemo.java
@@ -0,0 +1,31 @@
+package com.atguigu.interview.juc;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.ToString;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * @author
+ * CAS->Unsafe->CAS底层思想->ABA->原子引用更新->如何规避ABA问题
+ */
+public class AtomicReferenceDemo {
+ public static void main(String[] args){
+ User z3 = new User("z3",22);
+ User l4 = new User("l4",25);
+ AtomicReference atomicReference = new AtomicReference<>();
+ atomicReference.set(z3);
+ System.out.println(atomicReference.compareAndSet(z3,l4)+"\t"+atomicReference.get());
+ System.out.println(atomicReference.compareAndSet(z3,l4)+"\t"+atomicReference.get());
+
+ }
+}
+
+@Data
+@ToString
+@AllArgsConstructor
+class User{
+ String userName;
+ int age;
+}
diff --git a/src/main/java/com/atguigu/interview/juc/CASDemo.java b/src/main/java/com/atguigu/interview/juc/CASDemo.java
new file mode 100644
index 0000000..b9c6d5c
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/CASDemo.java
@@ -0,0 +1,16 @@
+package com.atguigu.interview.juc;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * @author
+ * CAS 比较并交换
+ */
+ public class CASDemo {
+ public static void main(String[] args){
+ AtomicInteger atomicInteger = new AtomicInteger();
+ System.out.println(atomicInteger.compareAndSet(0,1)+" current data = "+atomicInteger.get());
+ System.out.println(atomicInteger.compareAndSet(0,2)+" current data = "+atomicInteger.get());
+ atomicInteger.getAndIncrement();
+ }
+}
diff --git a/src/main/java/com/atguigu/interview/juc/ContainerNotSafeDemo.java b/src/main/java/com/atguigu/interview/juc/ContainerNotSafeDemo.java
new file mode 100644
index 0000000..7a90ebb
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/ContainerNotSafeDemo.java
@@ -0,0 +1,76 @@
+package com.atguigu.interview.juc;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
+
+/**
+ * @author
+ */
+public class ContainerNotSafeDemo {
+ public static void main(String[] args){
+ mapNotSafe();
+ }
+
+ private static void mapNotSafe() {
+ Map map = new ConcurrentHashMap<>();
+ for (int i = 0; i < 30; i++) {
+ new Thread(()->{
+ map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,8));
+ System.out.println(map);
+ },String.valueOf(i)).start();
+ }
+ }
+
+ private static void setNotSafe() {
+ Set set = new CopyOnWriteArraySet<>();
+ for (int i = 0; i < 30; i++) {
+ new Thread(()->{
+ set.add(UUID.randomUUID().toString().substring(0,8));
+ System.out.println(set);
+ },String.valueOf(i)).start();
+ }
+ // HashSet 与 HashMap
+ new HashSet().add("a");
+ }
+
+ private static void listNotSafe() {
+ List list = new CopyOnWriteArrayList<>();
+ for (int i = 0; i < 30; i++) {
+ new Thread(()->{
+ list.add(UUID.randomUUID().toString().substring(0,8));
+ System.out.println(list);
+ },String.valueOf(i)).start();
+ }
+
+ /**
+ * 故障现场
+ * java.util.ConcurrentModificationException
+ * 导致原因
+ *
+ * 解决方案
+ * 1.new Vector<>();
+ * 2.Collections.synchronizedList(new ArrayList<>());
+ * 3.CopyOnWriteArrayList();
+ * CopyOnWrite容器即写时复制的容器。通俗的理解是当我们往一个容器添加元素的时候,
+ * 不直接往当前容器添加,而是先将当前容器进行Copy,复制出一个新的容器,然后新的容
+ * 器里添加元素,添加完元素之后,再将原容器的引用指向新的容器。这样做的好处是我们
+ * 可以对CopyOnWrite容器进行并发的读,而不需要加锁,因为当前容器不会添加任何元素。
+ * 所以CopyOnWrite容器也是一种读写分离的思想,读和写不同的容器。
+ * CopyOnWrite的缺点
+ * 内存占用问题。因为CopyOnWrite的写时复制机制,所以在进行写操作的时候,内存
+ * 里会同时驻扎两个对象的内存,旧的对象和新写入的对象(注意:在复制的时候只是复制容
+ * 器里的引用,只是在写的时候会创建新对象添加到新容器里,而旧容器的对象还在使用,
+ * 所以有两份对象内存)。如果这些对象占用的内存比较大,比如说200M左右,那么再
+ * 写入100M数据进去,内存就会占用300M,那么这个时候很有可能造成频繁的Yong GC和Full GC。
+ * 之前我们系统中使用了一个服务由于每晚使用CopyOnWrite机制更新大对象,造成了每晚15秒的
+ * Full GC,应用响应时间也随之变长。
+ * 针对内存占用问题,可以通过压缩容器中的元素的方法来减少大对象的内存消耗,比如,如果
+ * 元素全是10进制的数字,可以考虑把它压缩成36进制或64进制。或者不使用CopyOnWrite容器,
+ * 而使用其他的并发容器,如ConcurrentHashMap。
+ *
+ * 数据一致性问题。CopyOnWrite容器只能保证数据的最终一致性,不能保证数据的实时一致性。
+ * 所以如果你希望写入的的数据,马上能读到,请不要使用CopyOnWrite容器。
+ */}
+}
diff --git a/src/main/java/com/atguigu/interview/juc/LockDemo.java b/src/main/java/com/atguigu/interview/juc/LockDemo.java
new file mode 100644
index 0000000..5c3fd66
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/LockDemo.java
@@ -0,0 +1,64 @@
+package com.atguigu.interview.juc;
+
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.atomic.AtomicStampedReference;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * @author
+ */
+public class LockDemo {
+ public static void main(String[] args){
+ // 默认非公平锁
+ // 可重入锁 避免死锁
+ ReentrantLock lock = new ReentrantLock();
+
+ // synchronized 可重入锁
+ Phone phone = new Phone();
+ new Thread(()->{phone.sendMsg();},"t1").start();
+ new Thread(()->{phone.sendMsg();},"t2").start();
+
+ // ReentrantLock 可重入锁
+ new Thread(phone).start();
+ new Thread(phone).start();
+
+ // 自旋锁 spinlock CAS核心原理
+ // 尝试获取锁的线程不会阻塞,而是采用循环的方式尝试获取锁。
+
+ }
+}
+
+class Phone implements Runnable{
+ public synchronized void sendMsg() {
+ System.out.println(Thread.currentThread().getName()+"\t invoked sendMsg()");
+ sendEmail();
+ }
+ public synchronized void sendEmail() {
+ System.out.println(Thread.currentThread().getName()+"\t invoked sendEmail()");
+ }
+
+ Lock lock = new ReentrantLock();
+ @Override
+ public void run() {
+ get();
+ }
+ public void get(){
+ // 可多次加锁,只要和unlock匹配即可
+ lock.lock();
+ try {
+ System.out.println(Thread.currentThread().getName()+"\t invoked get()");
+ set();
+ }finally {
+ lock.unlock();
+ }
+ }
+ public void set(){
+ lock.lock();
+ try {
+ System.out.println(Thread.currentThread().getName()+"\t invoked set()");
+ }finally {
+ lock.unlock();
+ }
+ }
+}
diff --git a/src/main/java/com/atguigu/interview/juc/ReadWriteLockDemo.java b/src/main/java/com/atguigu/interview/juc/ReadWriteLockDemo.java
new file mode 100644
index 0000000..d7f9c30
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/ReadWriteLockDemo.java
@@ -0,0 +1,65 @@
+package com.atguigu.interview.juc;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * @author
+ */
+public class ReadWriteLockDemo {
+ public static void main(String[] args){
+ MyCache cache = new MyCache();
+ for (int i = 0; i < 5; i++) {
+ final int temp = i;
+ new Thread(()->{
+ cache.put(String.valueOf(temp),temp);
+ },String.valueOf(i)).start();
+ }
+ for (int i = 0; i < 5; i++) {
+ final int temp = i;
+ new Thread(()->{
+ cache.get(String.valueOf(temp));
+ },String.valueOf(i)).start();
+ }
+ }
+}
+
+class MyCache{
+ private volatile Map map = new ConcurrentHashMap<>();
+ /**
+ * 尝试注释锁,体验效果
+ */
+ private ReadWriteLock lock = new ReentrantReadWriteLock();
+
+ public void put(String key,Object value){
+ System.out.println(Thread.currentThread().getName()+"\t 正在写入");
+ lock.writeLock().lock();
+ try {
+ TimeUnit.MILLISECONDS.sleep(300L);
+ map.put(key, value);
+ System.out.println(Thread.currentThread().getName()+"\t 写入完成");
+ } catch(Exception e) {
+ e.printStackTrace();
+ }finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ public void get(String key){
+ System.out.println(Thread.currentThread().getName()+"\t 正在读取");
+ lock.readLock().lock();
+ try {
+ TimeUnit.MILLISECONDS.sleep(100L);
+ Object o = map.get(key);
+ System.out.println(Thread.currentThread().getName()+"\t 读取完成" + o);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }finally {
+ lock.readLock().unlock();
+ }
+ }
+
+}
diff --git a/src/main/java/com/atguigu/interview/juc/Singleton.java b/src/main/java/com/atguigu/interview/juc/Singleton.java
new file mode 100644
index 0000000..f70bfe7
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/Singleton.java
@@ -0,0 +1,53 @@
+package com.atguigu.interview.juc;
+
+/**
+ * @author
+ *
+ */
+public class Singleton {
+ private static volatile Singleton instance = null;
+ private Singleton(){
+ System.out.println(Thread.currentThread().getName()+" 创建单例");
+ }
+
+ /**
+ * 懒汉模式
+ * 存在多线程问题
+ * @return
+ */
+ public static Singleton getInstance() {
+ if(instance == null){
+ instance = new Singleton();
+ }
+ return instance;
+ }
+
+ /**
+ * DCL(double check lock)+ volatile
+ * @return
+ */
+ public static Singleton getDCLInstance() {
+ if(instance == null){
+ synchronized (Singleton.class){
+ if(instance == null){
+ instance = new Singleton();
+ }
+ }
+ }
+ return instance;
+ }
+
+ public static void main(String[] args){
+ // 单线程环境
+// System.out.println("单线程环境:");
+// System.out.println(Singleton.getInstance() == Singleton.getInstance());
+
+ // 多线程并发环境
+ System.out.println("多线程并发环境:");
+ for (int i = 0; i < 10 ; i++) {
+ new Thread(()->{
+ Singleton.getDCLInstance();
+ }).start();
+ }
+ }
+}
diff --git a/src/main/java/com/atguigu/interview/juc/SpinlockDemo.java b/src/main/java/com/atguigu/interview/juc/SpinlockDemo.java
new file mode 100644
index 0000000..f68629a
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/SpinlockDemo.java
@@ -0,0 +1,54 @@
+package com.atguigu.interview.juc;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * @author
+ */
+public class SpinlockDemo {
+ AtomicReference atomicReference = new AtomicReference<>();
+
+ public void lock(){
+ Thread thread = Thread.currentThread();
+ System.out.println(thread.getName()+"\t invoked lock");
+ while(!atomicReference.compareAndSet(null,thread)){}
+ }
+ public void unlock(){
+ Thread thread = Thread.currentThread();
+ while(atomicReference.compareAndSet(thread,null)){}
+ System.out.println(thread.getName()+"\t invoked unlock");
+ }
+
+ public static void main(String[] args){
+ SpinlockDemo lock = new SpinlockDemo();
+ new Thread(()->{
+ lock.lock();
+ try {
+ TimeUnit.SECONDS.sleep(5);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }finally {
+ lock.unlock();
+ }
+ },"AA").start();
+
+ try {
+ TimeUnit.SECONDS.sleep(1);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+
+ new Thread(()->{
+ lock.lock();
+ try {
+ TimeUnit.SECONDS.sleep(1);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }finally {
+ lock.unlock();
+ }
+ },"BB").start();
+ }
+
+}
diff --git a/src/main/java/com/atguigu/interview/juc/VolatileDemo.java b/src/main/java/com/atguigu/interview/juc/VolatileDemo.java
new file mode 100644
index 0000000..81e84db
--- /dev/null
+++ b/src/main/java/com/atguigu/interview/juc/VolatileDemo.java
@@ -0,0 +1,94 @@
+package com.atguigu.interview.juc;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+class MyData{
+ /**
+ * 可见性验证,可注释后体验效果
+ */
+ volatile int number = 0;
+ AtomicInteger atomic = new AtomicInteger(0);
+ public void add(){
+ this.number = 10;
+ }
+ public void plus(){
+ this.number++;
+ }
+ public void atomicPlus(){
+ atomic.getAndIncrement();
+ }
+}
+class ReSortSeqDemo{
+ int a = 0;
+ boolean flag = false;
+
+ public void method(){
+ a = 1;
+ flag = true;
+ }
+ /**
+ * 多线程环境中线程交替执行,由于编译器优化指令重排序的存在
+ * 两个线程中使用的变量能够保证一致性无法确定
+ */
+ public void method2(){
+ if(flag){
+ a = a + 5;
+ System.out.println("a="+a);
+ }
+ }
+}
+
+/**
+ * @author
+ */
+public class VolatileDemo {
+ public static void main(String[] args) {
+ noAtomicVolatile();
+ }
+
+ /**
+ * 不保证原子性验证
+ */
+ private static void noAtomicVolatile() {
+ MyData myData = new MyData();
+ for (int i = 0; i < 20 ; i++) {
+ new Thread(()->{
+ for (int j = 0; j < 1000; j++) {
+ myData.plus();
+ myData.atomicPlus();
+ }
+ },String.valueOf(i)).start();
+ }
+ while(Thread.activeCount() >2){
+ Thread.yield();
+ }
+ System.out.println(Thread.currentThread().getName() + " finally number is :"+myData.number);
+ System.out.println(Thread.currentThread().getName() + " finally atomic is :"+myData.atomic);
+ }
+
+ /**
+ * 可见性验证
+ */
+ private static void seeOkByVolatile() {
+ MyData myData = new MyData();
+ new Thread(()->{
+ System.out.println(Thread.currentThread().getName());
+ try {
+ TimeUnit.SECONDS.sleep(3);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ myData.add();
+ System.out.println(Thread.currentThread().getName()+" updated number");
+ },"thread0").start();
+
+ while(myData.number == 0){
+ // main 循环等待
+ }
+ System.out.println(Thread.currentThread().getName()+" task is over");
+ }
+
+
+
+}