I am trying to use ScheduledExecutorService in an interval of 5 seconds with initial delay of 5 seconds till a condition is satisfied. Below is my code block
if (true) {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
Runnable runnable = new Runnable() {
public void run() {
try {
//API calls which gives boolean true/false
if (true) {
executorService.shutdown();
}
} catch (Exception ex) {
System.out.println("Excepton in shutting down executorService: " + ex);
}
}
};
executorService.scheduleAtFixedRate(runnable, 5, 5, TimeUnit.SECONDS);
System.out.println("Executor ended");
}
//Other lines of code which should get executed after ScheduledExecutorService is done executing
//Call to API etc.
I want my other lines and api call code to execute after ScheduledExecutorService is done executing (Basically stop further program execution apart from ScheduledExecutorService) but at the moment it's not happening. At present while ScheduledExecutorService is waiting to be executed my down api call code executes. Also, i am not able to use any variable declared globally to be used inside ScheduledExecutorService. How to handle this? Thanks.