Skip to content

Instantly share code, notes, and snippets.

@liuxinglanyue
Created June 17, 2015 07:57
Show Gist options
  • Save liuxinglanyue/d941b99fbb57663bc849 to your computer and use it in GitHub Desktop.
Save liuxinglanyue/d941b99fbb57663bc849 to your computer and use it in GitHub Desktop.
这不是一个ABA问题,哈哈
public class ABA {
private static int count = 0;
public static void main(String[] args) throws Exception {
ABA aba = new ABA();
aba.increase();
Thread.sleep(10000);
System.out.println(count);
}
public void increase() {
for(int i=0; i<1000; i++) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
//if not synchronized , count <= 1000
synchronized(ABA.class) {
count++;
}
}
});
t.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment