我创建了两个线程threadNew和incrementer。这会产生i count=2和增量 count=2的结果。
我的想法:它应该产生 i count=1和增量 count=2。有人可以调查并建议如何解决吗?
public class StaticThreadTest {
//i = 0 for all instance of this class.
private static int i = 0;
/**
* @param args
*/
public static void main(String[] args) {
//There is no guarantee of thread execution in sequence.
StaticThreadTest st = new StaticThreadTest();
StaticThreadTest st1 = new StaticThreadTest();
st.threadNew.start();
st1.incrementer.start();
}
Thread threadNew = new Thread(new Runnable() {
@Override
public void run() {
synchronized (this) {
i++;
System.out.println("i count=" + i);
}
}
});
Thread incrementer = new Thread(new Runnable() {
@Override
public void run() {
synchronized (this) {
i++;
System.out.println("increment count="+i);
}
}
});
}