我正在编写一个应用程序,它有 5 个线程同时从 Web 获取一些信息并填充缓冲区类中的 5 个不同字段。
当所有线程完成工作时,我需要验证缓冲区数据并将其存储在数据库中。
我该怎么做(当所有线程完成工作时收到警报)?
最佳答案
我采用的方法是使用 ExecutorService管理线程池。
ExecutorService es = Executors.newCachedThreadPool();
for(int i=0;i<5;i++)
es.execute(new Runnable() { /* your task */ });
es.shutdown();
boolean finished = es.awaitTermination(1, TimeUnit.MINUTES);
// all tasks have finished or the time has been reached.
关于java - 等到所有线程在java中完成它们的工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939257/