日期:2014-05-20 浏览次数:20689 次
public class EvenCheckerEasy implements Runnable {
private static int val = 0;
@Override
public void run() {
while (true) {
++val;
++val;
System.out.println(val); // 在我电脑上测试,此处添加打印输出似乎防止了并发的产生,为什么?
if (val % 2 != 0) {
System.out.println(val + " not event!");
return;
}
}
}
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
exec.execute(new EvenCheckerEasy());
}
exec.shutdown();
}
}
public void println(int x) {
synchronized (this) {
print(x);
newLine();
}
}