[英]Java Scheduled Executor Service wait all threads complete
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4);
for (int i = 0; i < 4; i++)
{Runnable autoUpdate = new autoUpdateWork(i);
ScheduledFuture<?> scheduleAtFixedRate = scheduler.scheduleAtFixedRate(autoUpdate, 0, 60, TimeUnit.SECONDS);
}
if (scheduleAtFixedRate != null)
{
while(!scheduleAtFixedRate.isDone())
//wait...
}
//PointA :until all threads are finished, do sth, then invoke the scheduled task with 60 sec interval again.
到目前為止,以上是我的代碼。 但是似乎永遠無法到達PointA。.請幫助,謝謝!
而不是做
if (scheduleAtFixedRate != null)
{
while(!scheduleAtFixedRate.isDone())
//wait...
}
在您的Runnable類中,添加一個volatile變量,如下所示。
public volatile boolean isRunning = false;
並且當您的線程完成工作后,將其更改為true,並在您的檢查部分中放入like。
while(!autoUpdate.isRunning);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.