Please look at the below code -
class A {
public int a;
public int b;
}
A a = new A();
new Thread(new Runnable() {
public void run() {
System.out.println(a.a +" "+ a.b);
}
}).start();
new Thread(new Runnable() {
public void run() {
a.a = 1;
a.b = 3;
}
}).start();
new Thread(new Runnable() {
public void run() {
a.a = 2;
a.b = 4;
}
}).start();
I know in a multi-threaded environment the output of above could not be predicted. Instance of A class is exposed to be updated by both the threads here and It's state is not thread-safe and rightly when I run the code I get output among {0 0}, {2 4} and {1 3}. Is it possible to get output as {1 4},{2 3},{0 4} or {2 0}? Why or Why Not ?