次のようなコードがあります。
public class Count extends Thread {
static IntCell n = new IntCell();
public void run() {
int temp;
for (int i = 0; i < 200000; i++) {
temp = n.getN();
n.setN(temp + 1);
}
}
public static void main(String[] args) {
Count p = new Count();
p.setName("Watek1");
Count q = new Count();
p.start();
q.start();
try {
p.join();
q.join();
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("The value of n is " + n.getN());
}
}
class IntCell {
private int n = 0;
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
}
2 つのスレッドがあり、n の値に 1 を追加します (静的クラス内)。このコードを実行すると、n 値の値が 400000 に等しくなることはありませんが、それについての何かです。なぜこのようなことが起こっているのですか?