这个主要给予一个ExecutorService 1000个runnables(测试者),他们所做的全部就是睡眠10毫秒,然后向静态计数器加1,主要是等待直到所有执行都完成了,但是计数器起了一些作用970处决......为什么?ExecutorService没有完成它的工作?
public class Testit {
public static void main (String arg[]) {
int n=1000;
ExecutorService e1 = Executors.newFixedThreadPool(20);
for (int i=0 ;i <n ;i++) {
e1.execute(new Tester());
}
e1.shutdown();
try {
e1.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Executed "+Tester.tester()+" Tasks.");
}
}
和Tester类:
public class Tester implements Runnable {
public static long tester=0;
@Override
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally { tester++; }
}
public static long tester() {
long temp=tester;
tester=0;
return temp;
}
}
编辑
问题解决了:
finally { synchronized (lock) {tester++;} }
感谢JB Nizet!
如果JB的答案帮助你解决了你的问题,你应该将它标记为已接受... – 2012-07-26 09:42:13
http://en.wikipedia.org/wiki/Race_condition – 2012-07-26 09:42:42
@Louis Wasserman我会,我不能只是 – 2012-07-26 09:43:25