StopWatch sw = new StopWatch();
sw.start();
ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
for (int i = 0; i < MYTHREADS; i++) {
Runnable worker = new SingleConnectionRunnable();
executor.execute(worker);
}
sw.stop();
System.out.println("total time"+sw.toString());
sw.reset();
sw.start();
for (int i = 0; i < MYTHREADS; i++) {
Runnable worker2 = new PooledConnectionRunnable();
executor.execute(worker2);
}
executor.shutdown();
executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
while (!executor.isTerminated()) {
}
sw.stop();
System.out.println("total time"+sw.toString());
上記のコードでいくつかのperfテストを実行しようとしています。私は別のRunnable
に同じexecutor
を使用して時間を測定しようとしています。しかし、それはまったく機能しません。最初の「合計時間」は正しくありません。これはミリ秒単位です。スレッドを終了するスレッドを待つ
最初のループで経過時間を印刷してから、2番目のループを印刷します。エグゼキュータを最初に終了してからエグゼキュータを再起動する方法を確認できない
これを行う正しい方法は何ですか?
[ExecutorService、すべてのタスクが完了するのを待つ方法]の可能な複製(http://stackoverflow.com/questions/3269445/executorservice-how-to-wait-for-all-tasks-to-finish) – AxelH