Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Created November 22, 2014 14:54
Show Gist options
  • Save sreeprasad/ae8c842c761c297d319d to your computer and use it in GitHub Desktop.
Save sreeprasad/ae8c842c761c297d319d to your computer and use it in GitHub Desktop.
Thread Race probelm
public class ExampleThree{
private static int bal=0;
public static void updateBal(){
bal+=10;
bal-=10;
}
public static void monitorBal(){
if(bal!=0){
System.out.println(bal);
}
}
public static void updateBalThread(){
Thread t = new Thread( () -> {
while(true){
updateBal();
}
});
t.start();
}
public static void monitorBalThread(){
Thread t = new Thread( ()-> {
while(true){
monitorBal();
}
});
t.start();
}
public static void main(String[] args) {
updateBalThread();
monitorBalThread();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment