下面这段代码中,为什么会打印出奇数值并且退出,但是将注释去掉,程序就不会退出呢?求大神解答啊
package com.zy.test.lock;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class AtomicityTest implements Runnable
{
private int i = 0;
public synchronized int getValue()
{
return i;
}
public synchronized void evenIncrement()
{
i++;
// i++;
}
public void run()
{
while (true)
{
evenIncrement();
}
}
public static void main(String[] args)
{
ExecutorService service = Executors.newCachedThreadPool();
AtomicityTest test = new AtomicityTest();
service.execute(test);
while (true)
{
int val = test.getValue();
if (val % 2 != 0)
{
System.out.println(val);
System.exit(0);
}
}
}
}