简体   繁体   中英

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.

So far the above is my code. But seems it would never get to PointA.. Please help, thanks!

instead of doing

if (scheduleAtFixedRate != null)
{    
    while(!scheduleAtFixedRate.isDone())
    //wait...
}

In your Runnable class add a volatile variable like below.

public  volatile boolean isRunning = false;

and once your thread completes the work, change it to true and in your checking part put like .

while(!autoUpdate.isRunning);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM