Created
November 22, 2014 14:54
-
-
Save sreeprasad/ae8c842c761c297d319d to your computer and use it in GitHub Desktop.
Thread Race probelm
This file contains hidden or 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 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