[英]Java running thread cykllu, waiting for the completion of thread
I need to run in a cycle of four parallel threads that will be something to process and thread of late, so the cycle began Dasle 4 parallel threads. 我需要在四个并行线程的循环中运行,这将是最近要处理的线程,因此循环开始了Dasle 4并行线程。 I have tried to implement, but the cycle starts over 4 threads.
我尝试实现,但是周期从4个线程开始。 I miss checking if there threads they are completed or not.
我错过了检查是否有线程完成的检查。 Can you advise me please?
你能告诉我吗? Thank you.
谢谢。
Something like: 就像是:
// This will run your jobs in threads
ExecutorService threadPoolExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
// Adding new jobs to list
List<Job> processingList= Collections.synchronizedList(new ArrayList<Job>());
processingList.add(someJob1);
processingList.add(someJob2);
processingList.add(someJob3);
processingList.add(someJob4);
...
Runnable processor = new Runnable() {
public void run() {
// Run all jobs from List in threads
for (Job job : processingList) {
threadPoolExecutor.execute(job);
}
// wait till jobs are completed
boolean areJobsCompleted = false;
while(!areJobsCompleted) {
boolean areJobsCompleted = true;
for (Job job : processingList) {
areJobsCompleted = areJobsCompleted && job.isComplete();
}
}
}
};
Executors.newSingleThreadExecutor().execute(processor);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.