あなたがスレッドのジョブを実行するためにExecutorService
を使用している場合、あなたはすべてのスレッドが終了した時に知ってawaitTermination()
メソッドを使用することができます。
ExecutorService pool = Executors.newFixedThreadPool(10);
pool.submit(yourSolutionsRunnable);
pool.submit(yourSolutionsRunnable);
...
// once you've submitted your last job you can do
pool.shutdown();
次にあなたがフィニッシュに送信されたジョブのすべてのを待つことができます:
pool.waitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
解決策を提出した後にスレッドを実行し続ける必要がある場合、これはさらに複雑になります。あなたがあなたの質問を編集し、これをより明白にすると私は自分の答えを編集します。
編集:
ああ、私はあなたが道に沿っていくつかの結果を処理するが、すべてのスレッドが完了するまで停止しないようにしたい参照してください。
すべてのジョブが完了したかどうかを示すpool.isTerminated()
テストを使用できます。だからあなたのループは次のようになります。
// this is the main thread so waiting for solutions in a while(true) loop is ok
while (true) {
// are all the workers done?
if (pool.isTerminated()) {
// if there are results process one last time
if (!solutions_results.isEmpty()) {
processTheSolutions();
}
break;
} else {
if (solutions_results.isEmpty()) {
// wait a bit to not spin, you could also use a wait/notify here
Thread.sleep(1000);
} else {
processTheSolutions();
}
}
}
編集:
あなたはまた、2つのスレッド・プール、ソリューション、別の処理を生成するための1つを持つことができます。メインスレッドは、ワーカープールが空になるのを待ってから、ソリューション処理プールを待ちます。ワーカープールは、ソリューションプール(存在する場合)をソリューションプールに提出する。必要に応じて、ソリューション処理プールに1つのスレッドしか持たないかもしれません。
ExecutorService workerPool = Executors.newFixedThreadPool(10);
final ExecutorService solutionsPool = Executors.newFixedThreadPool(1);
solutionsPool.submit(workerThatPutsSolutionsIntoSolutionsPool);
...
// once you've submitted your last worker you can do
workerPool.shutdown();
workerPool.waitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
// once the workers have finished you shutdown the solutions pool
solutionsPool.shutdown();
// and then wait for it to finish
solutionsPool.waitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
これは[生産者の消費者問題](http://en.wikipedia.org/wiki/Producer-consumer_problem)です。たぶんその記事はあなたにいくつかの洞察を与えるでしょう。 – jpm
@jpmそんなにありがとう、私はそれを読むでしょう.. –