Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package foo;
- import java.util.concurrent.*;
- import java.util.concurrent.atomic.AtomicInteger;
- public final class Scheduler {
- private static final int CORE_POOL_SIZE = 8;
- private static final String THREAD_NAME_PREFIX = "BugSimulator-thread-";
- private static AtomicInteger activeThreadsCount = new AtomicInteger();
- private Scheduler() {
- }
- public static synchronized ScheduledFuture<?> scheduleAtFixedRate(
- Runnable task, long initialDelay, long period, TimeUnit unit) {
- return Scheduler.getScheduledExecutorService().scheduleAtFixedRate(task, initialDelay, period, unit);
- }
- public static synchronized ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
- return Scheduler.getScheduledExecutorService().schedule(command, delay, unit);
- }
- public static synchronized void shutdownNow() {
- Scheduler.getScheduledExecutorService().shutdownNow();
- }
- private static ScheduledExecutorService getScheduledExecutorService() {
- return ScheduledExecutorServiceHolder.executor;
- }
- private static class ScheduledExecutorServiceHolder {
- private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(CORE_POOL_SIZE,
- new ThreadFactory() {
- public Thread newThread(Runnable runnable) {
- Thread thread = Executors.defaultThreadFactory().newThread(runnable);
- thread.setDaemon(true);
- thread.setPriority(Thread.MIN_PRIORITY);
- thread.setName(THREAD_NAME_PREFIX + activeThreadsCount.incrementAndGet());
- return thread;
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement