完成蚂蚁金服的面试题

This commit is contained in:
xiongraorao
2018-08-14 17:29:37 +08:00
parent b618fa0249
commit 03c346c68f
5 changed files with 168 additions and 8 deletions

View File

@ -0,0 +1,28 @@
package com.raorao.java.thread;
/**
* i++ 的原子性证明.
*
* @author Xiong Raorao
* @since 2018-08-14-15:15
*/
public class IPlusPlus {
private static volatile int index = 0;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
new Thread(() -> {
synchronized (IPlusPlus.class){
for (int j = 0; j < 10000; j++) {
index++;
}
}
System.out.println(Thread.currentThread().getName() + ", index = " + index);
}).start();
}
Thread.sleep(2000);
System.out.println(index);
}
}