Created
May 17, 2012 01:11
-
-
Save embs/2715374 to your computer and use it in GitHub Desktop.
Incrementador / Decrementador errôneo com threads sem exclusão mútua
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 CrazyIncrementer { | |
private static Integer n = 0; | |
private static final int c = 100000; | |
public static void main(String[] args) { | |
try { | |
Thread t1 = new Thread(new Runnable() {public void run() { | |
for(int i = 0; i < c; i++) { n++; }; | |
}}); | |
Thread t2 = new Thread(new Runnable() {public void run() { | |
for(int i = 0; i < c; i++) { n--; }; | |
}}); | |
t1.start(); | |
t2.start(); | |
System.out.println(String.format("%d", n)); | |
} | |
catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment