0
在下面的代碼我試圖計算的次數execute
方法被調用,但count
不會增加:爲什麼在這個例子中var不會遞增?
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Test;
public class ThreadTest {
@Test
public void testThread() {
ExecutorService es = Executors.newCachedThreadPool();
for (int counter = 0; counter < 10; ++counter) {
int count = 0;
for (int i = 0; i < 4; i++) {
es.execute(new FooWorker());
}
count = count + 1;
System.out.println((count * 4) + "," + count + " threads created");
}
}
private final class FooWorker implements Runnable {
public void run() {
System.out.println("in run");
}
}
}
爲什麼count
不會增加?
你每增加4個'execute'調用也只增加一次'count'。 – 2014-12-01 16:24:24
您是否每次看到'創建4,1個線程? – sp00m 2014-12-01 16:24:27