I'm trying to understand what is the possible value of classMember
that two threads are updates, When I'm running the program the output is always 20, But I want to understand why its happend and what is the mimumum, maximum value of classMember
public class TestClass {
public int classMember = 0;
private void updateCM() {
for (int i = 0; i < 10; i++) {
classMember++;
}
}
public static void main(String[] args) {
TestClass mainClass = new TestClass();
Thread t1 = new Thread(mainClass::updateCM);
Thread t2 = new Thread(mainClass::updateCM);
t1.start();
t2.start();
while(t1.isAlive() || t2.isAlive()) {}
System.out.println(mainClass.classMember);
}
}