Last active
July 9, 2016 05:15
-
-
Save VenkataRaju/e7fef72535cda4c1d425a078eef340ad to your computer and use it in GitHub Desktop.
Cancel the next excution in case, the previous execution of the task is not completed yet, while using ScheduledThreadPool's scheduleAtFixedRate method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.time.LocalTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.ScheduledFuture; | |
import java.util.concurrent.TimeUnit; | |
import static test.SchedulesThreadPoolCongestionPreventionTest.log; | |
public final class SchedulesThreadPoolCongestionPreventionTest | |
{ | |
private static final DateTimeFormatter TIME_PATTERN = DateTimeFormatter.ofPattern("hh:mm:ss:SSS"); | |
public static void main(String[] args) | |
{ | |
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1); | |
Runnable actualRunnable = new Runnable() | |
{ | |
int[] sleepTimesInSeconds = { 6, 1, 1, 1, 1, 1 }; | |
int i = 0; | |
@Override | |
public void run() | |
{ | |
log("Started"); | |
long sleepTimeInSeconds = sleepTimesInSeconds[i++]; | |
log("Sleep time: " + sleepTimeInSeconds); | |
sleep(sleepTimeInSeconds); | |
log("Ended"); | |
if (i == sleepTimesInSeconds.length) | |
{ | |
log("Shutting down"); | |
scheduledThreadPool.shutdown(); | |
} | |
} | |
}; | |
scheduledThreadPool.scheduleAtFixedRate(new CongestionPreventerTask(scheduledThreadPool, actualRunnable), | |
0, 2, TimeUnit.SECONDS); | |
} | |
private static void sleep(long timeInSeconds) | |
{ | |
try | |
{ | |
Thread.sleep(timeInSeconds * 1000); | |
} | |
catch (InterruptedException e) | |
{ | |
throw new RuntimeException(e); | |
} | |
} | |
public static void log(String message) | |
{ | |
System.out.printf("%s %s %s%n", | |
LocalTime.now().format(TIME_PATTERN), | |
Thread.currentThread().getName(), | |
message); | |
} | |
} | |
class CongestionPreventerTask implements Runnable | |
{ | |
private final ScheduledExecutorService ses; | |
private final Runnable actual; | |
private ScheduledFuture<?> sf; | |
public CongestionPreventerTask(ScheduledExecutorService ses, Runnable actual) | |
{ | |
this.ses = ses; | |
this.actual = actual; | |
} | |
@Override | |
public void run() | |
{ | |
log("CongestionPreventerTask run() method invoked"); | |
if (sf != null) | |
{ | |
if (!sf.isDone()) | |
{ | |
log("Prev task is still running. Ignoring the current task"); | |
return; | |
} | |
try | |
{ | |
sf.get(); | |
} | |
catch (InterruptedException | ExecutionException e) | |
{ | |
throw new RuntimeException("Scheduled task failed", e); | |
} | |
} | |
log("Scheduling a task for immediate execution"); | |
sf = ses.schedule(actual::run, 0, TimeUnit.MICROSECONDS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment