Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created September 26, 2017 20:11
Show Gist options
  • Save chermehdi/2a3a8f509c1e254a00ef3865e594731b to your computer and use it in GitHub Desktop.
Save chermehdi/2a3a8f509c1e254a00ef3865e594731b to your computer and use it in GitHub Desktop.
example of non thread safe code
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