我的每個線程都會休眠2000毫秒,並且我有10個這樣的線程,所以我預計總的睡眠時間至少爲20秒,但它的唯一時間是在16-18秒之間。對不起,如果我問一些已經被問到的東西。這是我到目前爲止有:一個線程需要2秒,10個線程需要16-18秒。爲什麼?
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MyThreadPoolApp {
public static void main(String[] args) {
long execTime = System.currentTimeMillis();
CountDownLatch latch = new CountDownLatch(1);
ExecutorService executor = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
executor.submit(new Task());
}
System.out.println("threads submitted and waiting execution");
executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
}
execTime = System.currentTimeMillis() - execTime;
System.out.format("%d threads finished execution \n",Task.getCount());
System.out.println("thread time : " + Task.getTime());
System.out.println("main time : " + execTime);
}
}
其中任務是:
public class Task implements Runnable {
private static long totalTime;
private static int count;
public static long getTime(){ return totalTime; }
public static int getCount(){ return count; }
public void run() {
count++;
long startTime = System.currentTimeMillis();
try {
Thread.sleep(2000);
totalTime += System.currentTimeMillis() - startTime;
} catch (InterruptedException e) {}
}
}
我的輸出:
threads submitted and waiting execution
10 threads finished execution
thread time : 18001
main time : 2020
你捉'InterruptedException'並沒有做任何事的。你認爲這有可能出現問題,並中止睡眠? – Michael
好點。我沒有想過。我會嘗試一下。 @Michael Nope,什麼都沒有.. – Somjit