Last active
August 29, 2015 14:18
-
-
Save newfurniturey/e01be253153b157d649e to your computer and use it in GitHub Desktop.
Sample ScheduledExecutorService
This file contains 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
// create a new thread pool service with 6 threads | |
ScheduledExecutorService service = Executors.newScheduledThreadPool(6); | |
// iterate over your list of trains | |
for (Train train : trainList) { | |
// determine how long of a delay between the current station and the next | |
int delay = ...; | |
// schedule the future service | |
ScheduledFuture future = service.schedule(new DispatchTask(train), delay, TimeUnit.SECONDS); | |
} | |
// alternatively, for a single train (since they'll finish separately): | |
public void scheduleNextRun(Train train) { | |
// determine how long of a delay between the current station and the next | |
int delay = ...; | |
// schedule the future service | |
this.service.schedule(new DispatchTask(train), delay, TimeUnit.SECONDS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment