Given the following code, after calling the join() method first for t1 and then for t2, I was expecting the value of 'count' to be at least 20.000. But the output is a random number. How I see the execution:
1. t1.join(): main thread waits for t1 to finish, expected 'count' value: 10.000
2. t2.join(): main thread waits for t2 to finish, expected 'count' value: 20.000
public class Main {
private static int count = 0;
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10000 ; i++) {
count++;
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10000 ; i++) {
count++;
}
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
}catch (InterruptedException ie){
}
System.out.println(count);
}
}