Skip to content

Instantly share code, notes, and snippets.

@embs
Created May 17, 2012 01:11
Show Gist options
  • Save embs/2715374 to your computer and use it in GitHub Desktop.
Save embs/2715374 to your computer and use it in GitHub Desktop.
Incrementador / Decrementador errôneo com threads sem exclusão mútua
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