Created
September 26, 2017 20:11
-
-
Save chermehdi/2a3a8f509c1e254a00ef3865e594731b to your computer and use it in GitHub Desktop.
example of non thread safe code
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
import java.util.ArrayList; | |
import java.util.Arrays; | |
/** | |
* @author Mehdi Maick | |
*/ | |
public class Main { | |
static int var = 0; | |
static int ind = 0; | |
public static void main(String[] args) throws InterruptedException { | |
Thread t1 = new Thread(() -> { | |
while (var < 1000) { | |
System.out.println(var++); | |
} | |
}); | |
Thread t2 = new Thread(() -> { | |
while (var < 1000) { | |
System.out.println(var++); | |
} | |
}); | |
Thread t3 = new Thread(() -> { | |
while (var < 10000) { | |
System.out.println(var++); | |
} | |
}); | |
t1.start(); | |
t2.start(); | |
t3.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment