Skip to content

Instantly share code, notes, and snippets.

@listochkin
Created February 8, 2013 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save listochkin/4737882 to your computer and use it in GitHub Desktop.
Save listochkin/4737882 to your computer and use it in GitHub Desktop.
Fancy way to create Java Thread
// Looks a bit scary but works wonders
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
// Name your thread so you can later easily recognize it with a profiler
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "Thread Name");
}
});
// Now you can submit tasks, run them periodically, shutdown them from the outside, etc.
ses.scheduleAtFixedRate(command, initialDelay, period, unit);
// also, if single thread is not enough in a future
// you can easily change to different Executor method
// and keep the rest of your code inact.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment