Created
February 8, 2013 10:09
-
-
Save listochkin/4737882 to your computer and use it in GitHub Desktop.
Fancy way to create Java Thread
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
// 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