Advertisement
JetSerge

Untitled

Oct 29th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. package foo;
  2.  
  3. import java.util.concurrent.*;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5.  
  6. public final class Scheduler {
  7.  
  8. private static final int CORE_POOL_SIZE = 8;
  9. private static final String THREAD_NAME_PREFIX = "BugSimulator-thread-";
  10. private static AtomicInteger activeThreadsCount = new AtomicInteger();
  11.  
  12. private Scheduler() {
  13. }
  14.  
  15. public static synchronized ScheduledFuture<?> scheduleAtFixedRate(
  16. Runnable task, long initialDelay, long period, TimeUnit unit) {
  17. return Scheduler.getScheduledExecutorService().scheduleAtFixedRate(task, initialDelay, period, unit);
  18. }
  19.  
  20. public static synchronized ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
  21. return Scheduler.getScheduledExecutorService().schedule(command, delay, unit);
  22. }
  23.  
  24. public static synchronized void shutdownNow() {
  25. Scheduler.getScheduledExecutorService().shutdownNow();
  26. }
  27.  
  28. private static ScheduledExecutorService getScheduledExecutorService() {
  29. return ScheduledExecutorServiceHolder.executor;
  30. }
  31.  
  32. private static class ScheduledExecutorServiceHolder {
  33. private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(CORE_POOL_SIZE,
  34. new ThreadFactory() {
  35. public Thread newThread(Runnable runnable) {
  36. Thread thread = Executors.defaultThreadFactory().newThread(runnable);
  37. thread.setDaemon(true);
  38. thread.setPriority(Thread.MIN_PRIORITY);
  39. thread.setName(THREAD_NAME_PREFIX + activeThreadsCount.incrementAndGet());
  40. return thread;
  41. }
  42. }
  43. );
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement