Created
June 17, 2015 07:57
-
-
Save liuxinglanyue/d941b99fbb57663bc849 to your computer and use it in GitHub Desktop.
这不是一个ABA问题,哈哈
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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