Skip to content

Instantly share code, notes, and snippets.

@newfurniturey
Last active August 29, 2015 14:18
Show Gist options
  • Save newfurniturey/e01be253153b157d649e to your computer and use it in GitHub Desktop.
Save newfurniturey/e01be253153b157d649e to your computer and use it in GitHub Desktop.
Sample ScheduledExecutorService
// 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