/** * Thread pool well suited for running CPU intensive computations on the tasks thread. * * @return Pool for CPU bound tasks */ public static SchedulerService computationPool() { return computationPool(null); }
/** * This returns a thread pool which is designed for an "isolated" task. An isolated task is one * where there is not other scheduling needs in this area of code. This is equivalent to * invoking {@link #singleThreadPool()} for every task submitted. However that implementation is * better if you have a multiple tasks you need to execute, and this one is much better if you * have a single task to execute / schedule. * <p> * Implementation wise every task submitted on the returned pool will increase the pool size * (if necessary) to allow execution, and then decrease the size once execution completes. * Because of this, {@link #singleThreadPool()} is much better if you can reuse the pool (to * reduce size churn), and this is much better if you only have a single task (to reduce memory * overhead). * * @return Pool which will ensure there is a thread available for every task executed on it */ public static SchedulerService isolatedTaskPool() { return isolatedTaskPool(null); }
/** * Low priority pool for scheduling cleanup or otherwise tasks which could be significantly * delayed. This pool will execute only on any general processing threads which are available. * By default there is only one, but it can be increased by invoking * {@link #increaseGenericThreads(int)}. * * @return Pool for running or scheduling out low priority tasks */ public static SchedulerService lowPriorityPool() { return lowPriorityPool(null); }
/** * Requests a pool with a given size. These threads are guaranteed to be available, but * general processing threads will not be available to any pools returned by this. If you want * to be able to use part of the shared general processing threads use * {@link #rangedThreadPool(int, int)} with either a higher or negative value for * {@code maxThreads}. * <p> * If returned pool will only be accepting one or two tasks, please see * {@link #isolatedTaskPool()} as an alternative. * * @param threadCount The number of threads that will be available to tasks submitted on the returned pool * @return A pool with the requested threads available for task scheduling or execution */ public static SchedulerService threadPool(int threadCount) { return rangedThreadPool(TaskPriority.High, threadCount, threadCount, null); }
/** * Return a single threaded pool. This can be useful for submitting tasks on where you don't * want to worry about any concurrency or shared memory issues. * <p> * If wanting guaranteed thread, and returned pool will only be accepting one or two tasks, * please see {@link #isolatedTaskPool(String)} as an alternative. * * @param threadGuaranteed {@code true} indicates that the pool manager needs to expand if necessary * @return Single threaded pool for running or scheduling tasks on */ public static PrioritySchedulerService singleThreadPool(boolean threadGuaranteed) { return singleThreadPool(threadGuaranteed, null); }
@Test public void isolatedPoolTest() { int testQty = 100; List<BlockingTestRunnable> blockingRunnables = new ArrayList<>(testQty); try { SubmitterExecutor executor = CentralThreadlyPool.isolatedTaskPool(); for (int i = 0; i < testQty; i++) { BlockingTestRunnable btr = new BlockingTestRunnable(); blockingRunnables.add(btr); executor.execute(btr); } for (BlockingTestRunnable btr : blockingRunnables) { btr.blockTillStarted(); } // verify we can still execute on pool with existing threads TestRunnable tr = new TestRunnable(); CentralThreadlyPool.lowPriorityPool().execute(tr); tr.blockTillStarted(); } finally { for (BlockingTestRunnable btr : blockingRunnables) { btr.unblock(); } } } }
/** * Low priority pool for scheduling cleanup or otherwise tasks which could be significantly * delayed. This pool will only have one thread, so tasks should complete quickly or they might * block other tasks. * * @return Single threaded pool for running or scheduling out low priority tasks */ public static PrioritySchedulerService lowPrioritySingleThreadPool() { return lowPrioritySingleThreadPool(null); }
protected static final SubmitterScheduler getStaticScheduler() { SubmitterScheduler ss = STATIC_SCHEDULER.get(); if (ss == null) { STATIC_SCHEDULER.compareAndSet(null, CentralThreadlyPool.threadPool(2, "WatchdogDefaultScheduler")); ss = STATIC_SCHEDULER.get(); } return ss; }
@Test public void getAndIncreaseGenericThreadsTest() { int startingCount = CentralThreadlyPool.getGenericThreadCount(); assertTrue(startingCount >= 1); CentralThreadlyPool.increaseGenericThreads(1); assertEquals(startingCount + 1, CentralThreadlyPool.getGenericThreadCount()); }
/** * Requests a pool with a given size. These threads are guaranteed to be available, but * general processing threads will not be available to any pools returned by this. If you want * to be able to use part of the shared general processing threads use * {@link #rangedThreadPool(int, int)} with either a higher or negative value for * {@code maxThreads}. * <p> * If returned pool will only be accepting one or two tasks, please see * {@link #isolatedTaskPool()} as an alternative. * * @param threadCount The number of threads that will be available to tasks submitted on the returned pool * @return A pool with the requested threads available for task scheduling or execution */ public static SchedulerService threadPool(int threadCount) { return rangedThreadPool(TaskPriority.High, threadCount, threadCount, null); }
/** * Return a single threaded pool. This can be useful for submitting tasks on where you don't * want to worry about any concurrency or shared memory issues. * <p> * If wanting guaranteed thread, and returned pool will only be accepting one or two tasks, * please see {@link #isolatedTaskPool(String)} as an alternative. * * @param threadGuaranteed {@code true} indicates that the pool manager needs to expand if necessary * @return Single threaded pool for running or scheduling tasks on */ public static PrioritySchedulerService singleThreadPool(boolean threadGuaranteed) { return singleThreadPool(threadGuaranteed, null); }
/** * Low priority pool for scheduling cleanup or otherwise tasks which could be significantly * delayed. This pool will only have one thread, so tasks should complete quickly or they might * block other tasks. * * @return Single threaded pool for running or scheduling out low priority tasks */ public static PrioritySchedulerService lowPrioritySingleThreadPool() { return lowPrioritySingleThreadPool(null); }
protected static final SubmitterScheduler getStaticScheduler() { SubmitterScheduler ss = STATIC_SCHEDULER.get(); if (ss == null) { STATIC_SCHEDULER.compareAndSet(null, CentralThreadlyPool.threadPool(2, "WatchdogDefaultScheduler")); ss = STATIC_SCHEDULER.get(); } return ss; }
/** * Requests a pool with a given size. These threads are guaranteed to be available, but * general processing threads will not be available to any pools returned by this. If you want * to be able to use part of the shared general processing threads use * {@link #rangedThreadPool(int, int)} with either a higher or negative value for * {@code maxThreads}. * <p> * If returned pool will only be accepting one or two tasks, please see * {@link #isolatedTaskPool(String)} as an alternative. * * @param threadCount The number of threads that will be available to tasks submitted on the returned pool * @param threadName Name to prefix to thread while tasks on this pool execute, or {@code null} * @return A pool with the requested threads available for task scheduling or execution */ public static SchedulerService threadPool(int threadCount, String threadName) { return rangedThreadPool(TaskPriority.High, threadCount, threadCount, threadName); }
/** * Return a single threaded pool. This can be useful for submitting tasks on where you don't * want to worry about any concurrency or shared memory issues. If you want a single threaded * pool which is forced to use the already established pool limits, consider using * {@link #singleThreadPool(boolean)} with {@code false} to ensure pool churn is reduced. * <p> * If returned pool will only be accepting one or two tasks, please see * {@link #isolatedTaskPool()} as an alternative. * * @return Single threaded pool for running or scheduling tasks on */ public static PrioritySchedulerService singleThreadPool() { return singleThreadPool(true, null); }
/** * Low priority pool for scheduling cleanup or otherwise tasks which could be significantly * delayed. This pool will execute only on any general processing threads which are available. * By default there is only one, but it can be increased by invoking * {@link #increaseGenericThreads(int)}. * * @return Pool for running or scheduling out low priority tasks */ public static SchedulerService lowPriorityPool() { return lowPriorityPool(null); }
/** * Thread pool well suited for running CPU intensive computations on the tasks thread. * * @return Pool for CPU bound tasks */ public static SchedulerService computationPool() { return computationPool(null); }
@Test public void lowPrioritySingleThreadRenamedTest() throws InterruptedException, TimeoutException { final String threadName = StringUtils.makeRandomString(5); AsyncVerifier av = new AsyncVerifier(); CentralThreadlyPool.lowPrioritySingleThreadPool(threadName).execute(() -> { av.assertTrue(Thread.currentThread().getName().startsWith(threadName)); av.signalComplete(); }); av.waitForTest(); }
@Test public void threadPoolsGuaranteedThreadTest() { int threadsPerScheduler = 10; List<SchedulerService> executors = new ArrayList<>(); for (int i = 0; i < TEST_QTY * 2; i++) { executors.add(CentralThreadlyPool.threadPool(threadsPerScheduler)); } verifyGuaranteedThreadProtection(executors, threadsPerScheduler); }
/** * This returns a thread pool which is designed for an "isolated" task. An isolated task is one * where there is not other scheduling needs in this area of code. This is equivalent to * invoking {@link #singleThreadPool()} for every task submitted. However that implementation is * better if you have a multiple tasks you need to execute, and this one is much better if you * have a single task to execute / schedule. * <p> * Implementation wise every task submitted on the returned pool will increase the pool size * (if necessary) to allow execution, and then decrease the size once execution completes. * Because of this, {@link #singleThreadPool()} is much better if you can reuse the pool (to * reduce size churn), and this is much better if you only have a single task (to reduce memory * overhead). * * @return Pool which will ensure there is a thread available for every task executed on it */ public static SchedulerService isolatedTaskPool() { return isolatedTaskPool(null); }