|
| 1 | +package com.sedmelluq.discord.lavaplayer.tools; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.util.concurrent.ExecutionException; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Future; |
| 9 | +import java.util.concurrent.LinkedBlockingQueue; |
| 10 | +import java.util.concurrent.RejectedExecutionException; |
| 11 | +import java.util.concurrent.RejectedExecutionHandler; |
| 12 | +import java.util.concurrent.ThreadFactory; |
| 13 | +import java.util.concurrent.ThreadPoolExecutor; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.concurrent.TimeoutException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Utility methods for working with executors. |
| 19 | + */ |
| 20 | +public class ExecutorTools { |
| 21 | + private static final Logger log = LoggerFactory.getLogger(ExecutorTools.class); |
| 22 | + |
| 23 | + private static final long WAIT_TIME = 1000L; |
| 24 | + |
| 25 | + /** |
| 26 | + * A completed Future<Void> instance. |
| 27 | + */ |
| 28 | + public static final CompletedVoidFuture COMPLETED_VOID = new CompletedVoidFuture(); |
| 29 | + |
| 30 | + /** |
| 31 | + * Shut down an executor and log the shutdown result. The executor is given a fixed amount of time to shut down, if it |
| 32 | + * does not manage to do it in that time, then this method just returns. |
| 33 | + * |
| 34 | + * @param executorService Executor service to shut down |
| 35 | + * @param description Description of the service to use for logging |
| 36 | + */ |
| 37 | + public static void shutdownExecutor(ExecutorService executorService, String description) { |
| 38 | + if (executorService == null) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + log.debug("Shutting down executor {}", description); |
| 43 | + |
| 44 | + executorService.shutdownNow(); |
| 45 | + |
| 46 | + try { |
| 47 | + if (!executorService.awaitTermination(WAIT_TIME, TimeUnit.MILLISECONDS)) { |
| 48 | + log.debug("Executor {} did not shut down in {}", description, WAIT_TIME); |
| 49 | + } else { |
| 50 | + log.debug("Executor {} successfully shut down", description); |
| 51 | + } |
| 52 | + } catch (InterruptedException e) { |
| 53 | + log.debug("Received an interruption while shutting down executor {}", description); |
| 54 | + Thread.currentThread().interrupt(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates an executor which will use the queue only when maximum number of threads has been reached. The core pool |
| 60 | + * size here only means the number of threads that are always alive, it is no longer used to check whether a new |
| 61 | + * thread should start or not. The maximum size is otherwise pointless unless you have a bounded queue, which in turn |
| 62 | + * would cause tasks to be rejected if it is too small. |
| 63 | + * |
| 64 | + * @param coreSize Number of threads that are always alive |
| 65 | + * @param maximumSize The maximum number of threads in the pool |
| 66 | + * @param timeout Non-core thread timeout in milliseconds |
| 67 | + * @param threadFactory Thread factory to create pool threads with |
| 68 | + * @return An eagerly scaling thread pool executor |
| 69 | + */ |
| 70 | + public static ThreadPoolExecutor createEagerlyScalingExecutor(int coreSize, int maximumSize, long timeout, |
| 71 | + int queueCapacity, ThreadFactory threadFactory) { |
| 72 | + |
| 73 | + ThreadPoolExecutor executor = new ThreadPoolExecutor(coreSize, maximumSize, timeout, TimeUnit.MILLISECONDS, |
| 74 | + new EagerlyScalingTaskQueue(queueCapacity), threadFactory); |
| 75 | + |
| 76 | + executor.setRejectedExecutionHandler(new EagerlyScalingRejectionHandler()); |
| 77 | + return executor; |
| 78 | + } |
| 79 | + |
| 80 | + private static class EagerlyScalingTaskQueue extends LinkedBlockingQueue<Runnable> { |
| 81 | + public EagerlyScalingTaskQueue(int capacity) { |
| 82 | + super(capacity); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean offer(Runnable runnable) { |
| 87 | + return isEmpty() && super.offer(runnable); |
| 88 | + } |
| 89 | + |
| 90 | + public boolean offerDirectly(Runnable runnable) { |
| 91 | + return super.offer(runnable); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static class EagerlyScalingRejectionHandler implements RejectedExecutionHandler { |
| 96 | + @Override |
| 97 | + public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) { |
| 98 | + if (!((EagerlyScalingTaskQueue) executor.getQueue()).offerDirectly(runnable)) { |
| 99 | + throw new RejectedExecutionException("Task " + runnable.toString() + " rejected from " + runnable.toString()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static class CompletedVoidFuture implements Future<Void> { |
| 105 | + @Override |
| 106 | + public boolean cancel(boolean mayInterruptIfRunning) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean isCancelled() { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean isDone() { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Void get() throws InterruptedException, ExecutionException { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 127 | + return null; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments