J'ai planifié une tâche récurrente. Maintenant, je veux que le thread principal attende le ScheduledExecutorService
pour terminer son première exécution. Comment puis-je accomplir cela?Comment puis-je attendre la fin de la première itération d'une tâche planifiée?
public void test1(){
refreshFunction(); /* This should block until task has executed once. */
/* Continue with main thread. */
...
}
public void refreshFunction() {
try {
scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
loadInfo();
}
}, 1, 5, TimeUnit.MINUTES);
logger.info(" : Completed refreshing information : "
+ new Timestamp(System.currentTimeMillis()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
Vérifiez ma réponse dans: http://stackoverflow.com/questions/35075886/how-to-wait-for-a-thread-that-spawns-its-own-thread –
Avez-vous vu ce 'schedExecutorService. awaitTermination (timeout, unité) '? – mob41