Description
A task A is running, that can last from 2s to 60+s.
While A is running, I scheduled a ExecutorService that after 10s (delay), would print "running" every 5s (fixed rate).
ScheduledExecutorService scheduleNotifications = Executors
.newSingleThreadScheduledExecutor();
scheduleNotifications.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("A is running");
}
}, 10, 5, TimeUnit.SECONDS);
Once A is done, I wish to print "A is done." but only if my ScheduledExecutorService task has started , meaning it printed "running" at least once.
How could I do that without using a boolean flag inside the schduled runnable
. Is there a way to check that my service started?
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("A is running");
}
}, 10, 5, TimeUnit.SECONDS);
Thread.sleep(20000);
scheduler.shutdown();
if (scheduler.getCompletedTaskCount() > 0) {
System.out.println("A is done.");
}
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.