マルチスレッドの実行があり、実行時間を追跡して出力したいのですが、コードを実行すると、子スレッドはメインの実行よりも時間がかかるため、出力が表示されず、正しい値も出力されません。早期終了。
コードは次のとおりです。
public static void main(String[] args) throws CorruptIndexException, IOException, LangDetectException, InterruptedException {
/* Initialization */
long startingTime = System.currentTimeMillis();
Indexer main = new Indexer(); // this class extends Thread
File file = new File(SITES_PATH);
main.addFiles(file);
/* Multithreading through ExecutorService */
ExecutorService es = Executors.newFixedThreadPool(4);
for (File f : main.queue) {
Indexer ind = new Indexer(main.writer, main.identificatore, f);
ind.join();
es.submit(ind);
}
es.shutdown();
/* log creation - code I want to execute when all the threads execution ended */
long executionTime = System.currentTimeMillis()-startingTime;
long minutes = TimeUnit.MILLISECONDS.toMinutes(executionTime);
long seconds = TimeUnit.MILLISECONDS.toSeconds(executionTime)%60;
String fileSize = sizeConversion(FileUtils.sizeOf(file));
Object[] array = {fileSize,minutes,seconds};
logger.info("{} indexed in {} minutes and {} seconds.",array);
}
join()、wait()、notifyAll() などのいくつかのソリューションを試しましたが、どれも機能しませんでした。
私の問題を扱うstackoverflowでこのQ&Aを見つけましたが、join()は無視され、
es.awaitTermination(タイムアウト、TimeUnit.SECONDS);
実際、executor サービスはスレッドを実行しません。
ExecutorService ブロックでのみマルチスレッドを実行し、最後にメインの実行で終了するソリューションはどれですか?