请问多线程的例子,明明是输出偶数的,怎么回事?在线等,解决了就加分!!
public class AlwaysEven {
private int i;
public void next() { i++; i++; }
public int getValue() { return i; }
public static void main(String[] args) {
final AlwaysEven ae = new AlwaysEven();
new Thread("Watcher") {
public void run() {
while(true) {
int val = ae.getValue();
if(val % 2 != 0) {
System.out.println(val);
System.exit(0);
}
}
}
}.start();
while(true)
ae.next();
}
}
是都应该产生偶数吧,但是为什么会有奇数打印出来呢?
在线等,解决了就加分!!!!