| 1 | /** | 
| 2 |  * Licensed to jclouds, Inc. (jclouds) under one or more | 
| 3 |  * contributor license agreements.  See the NOTICE file | 
| 4 |  * distributed with this work for additional information | 
| 5 |  * regarding copyright ownership.  jclouds licenses this file | 
| 6 |  * to you under the Apache License, Version 2.0 (the | 
| 7 |  * "License"); you may not use this file except in compliance | 
| 8 |  * with the License.  You may obtain a copy of the License at | 
| 9 |  * | 
| 10 |  *   http://www.apache.org/licenses/LICENSE-2.0 | 
| 11 |  * | 
| 12 |  * Unless required by applicable law or agreed to in writing, | 
| 13 |  * software distributed under the License is distributed on an | 
| 14 |  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
| 15 |  * KIND, either express or implied.  See the License for the | 
| 16 |  * specific language governing permissions and limitations | 
| 17 |  * under the License. | 
| 18 |  */ | 
| 19 | package org.jclouds.concurrent; | 
| 20 |   | 
| 21 | import java.util.concurrent.ExecutorService; | 
| 22 | import java.util.concurrent.Executors; | 
| 23 | import java.util.concurrent.RejectedExecutionException; | 
| 24 | import java.util.concurrent.ThreadFactory; | 
| 25 | import java.util.concurrent.ThreadPoolExecutor; | 
| 26 | import java.util.concurrent.TimeUnit; | 
| 27 |   | 
| 28 | /** | 
| 29 |  * Factory and utility methods for handling {@link DynamicThreadPoolExecutor}. | 
| 30 |  *  | 
| 31 |  * @author kimchy (shay.banon) | 
| 32 |  */ | 
| 33 | public class DynamicExecutors { | 
| 34 |    /** | 
| 35 |     * Creates a thread pool that creates new threads as needed, but will reuse previously | 
| 36 |     * constructed threads when they are available. Calls to <tt>execute</tt> will reuse previously | 
| 37 |     * constructed threads if available. If no existing thread is available, a new thread will be | 
| 38 |     * created and added to the pool. No more than <tt>max</tt> threads will be created. Threads that | 
| 39 |     * have not been used for a <tt>keepAlive</tt> timeout are terminated and removed from the cache. | 
| 40 |     * Thus, a pool that remains idle for long enough will not consume any resources other than the | 
| 41 |     * <tt>min</tt> specified. | 
| 42 |     *  | 
| 43 |     * @param min | 
| 44 |     *           the number of threads to keep in the pool, even if they are idle. | 
| 45 |     * @param max | 
| 46 |     *           the maximum number of threads to allow in the pool. | 
| 47 |     * @param keepAliveTime | 
| 48 |     *           when the number of threads is greater than the min, this is the maximum time that | 
| 49 |     *           excess idle threads will wait for new tasks before terminating (in milliseconds). | 
| 50 |     * @return the newly created thread pool | 
| 51 |     */ | 
| 52 |    public static ExecutorService newScalingThreadPool(int min, int max, long keepAliveTime) { | 
| 53 |       return newScalingThreadPool(min, max, keepAliveTime, Executors.defaultThreadFactory()); | 
| 54 |    } | 
| 55 |   | 
| 56 |    /** | 
| 57 |     * Creates a thread pool, same as in {@link #newScalingThreadPool(int, int, long)}, using the | 
| 58 |     * provided ThreadFactory to create new threads when needed. | 
| 59 |     *  | 
| 60 |     * @param min | 
| 61 |     *           the number of threads to keep in the pool, even if they are idle. | 
| 62 |     * @param max | 
| 63 |     *           the maximum number of threads to allow in the pool. | 
| 64 |     * @param keepAliveTime | 
| 65 |     *           when the number of threads is greater than the min, this is the maximum time that | 
| 66 |     *           excess idle threads will wait for new tasks before terminating (in milliseconds). | 
| 67 |     * @param threadFactory | 
| 68 |     *           the factory to use when creating new threads. | 
| 69 |     * @return the newly created thread pool | 
| 70 |     */ | 
| 71 |    public static ExecutorService newScalingThreadPool(int min, int max, long keepAliveTime, | 
| 72 |             ThreadFactory threadFactory) { | 
| 73 |       DynamicThreadPoolExecutor.DynamicQueue<Runnable> queue = new DynamicThreadPoolExecutor.DynamicQueue<Runnable>(); | 
| 74 |       ThreadPoolExecutor executor = new DynamicThreadPoolExecutor(min, max, keepAliveTime, | 
| 75 |                TimeUnit.MILLISECONDS, queue, threadFactory); | 
| 76 |       executor.setRejectedExecutionHandler(new DynamicThreadPoolExecutor.ForceQueuePolicy()); | 
| 77 |       queue.setThreadPoolExecutor(executor); | 
| 78 |       return executor; | 
| 79 |    } | 
| 80 |   | 
| 81 |    /** | 
| 82 |     * Creates a thread pool similar to that constructed by | 
| 83 |     * {@link #newScalingThreadPool(int, int, long)}, but blocks the call to <tt>execute</tt> if the | 
| 84 |     * queue has reached it's capacity, and all <tt>max</tt> threads are busy handling requests. | 
| 85 |     * <p/> | 
| 86 |     * If the wait time of this queue has elapsed, a {@link RejectedExecutionException} will be | 
| 87 |     * thrown. | 
| 88 |     *  | 
| 89 |     * @param min | 
| 90 |     *           the number of threads to keep in the pool, even if they are idle. | 
| 91 |     * @param max | 
| 92 |     *           the maximum number of threads to allow in the pool. | 
| 93 |     * @param keepAliveTime | 
| 94 |     *           when the number of threads is greater than the min, this is the maximum time that | 
| 95 |     *           excess idle threads will wait for new tasks before terminating (in milliseconds). | 
| 96 |     * @param capacity | 
| 97 |     *           the fixed capacity of the underlying queue (resembles backlog). | 
| 98 |     * @param waitTime | 
| 99 |     *           the wait time (in milliseconds) for space to become available in the queue. | 
| 100 |     * @return the newly created thread pool | 
| 101 |     */ | 
| 102 |    public static ExecutorService newBlockingThreadPool(int min, int max, long keepAliveTime, | 
| 103 |             int capacity, long waitTime) { | 
| 104 |       return newBlockingThreadPool(min, max, keepAliveTime, capacity, waitTime, Executors | 
| 105 |                .defaultThreadFactory()); | 
| 106 |    } | 
| 107 |   | 
| 108 |    /** | 
| 109 |     * Creates a thread pool, same as in {@link #newBlockingThreadPool(int, int, long, int, long)}, | 
| 110 |     * using the provided ThreadFactory to create new threads when needed. | 
| 111 |     *  | 
| 112 |     * @param min | 
| 113 |     *           the number of threads to keep in the pool, even if they are idle. | 
| 114 |     * @param max | 
| 115 |     *           the maximum number of threads to allow in the pool. | 
| 116 |     * @param keepAliveTime | 
| 117 |     *           when the number of threads is greater than the min, this is the maximum time that | 
| 118 |     *           excess idle threads will wait for new tasks before terminating (in milliseconds). | 
| 119 |     * @param capacity | 
| 120 |     *           the fixed capacity of the underlying queue (resembles backlog). | 
| 121 |     * @param waitTime | 
| 122 |     *           the wait time (in milliseconds) for space to become available in the queue. | 
| 123 |     * @param threadFactory | 
| 124 |     *           the factory to use when creating new threads. | 
| 125 |     * @return the newly created thread pool | 
| 126 |     */ | 
| 127 |    public static ExecutorService newBlockingThreadPool(int min, int max, long keepAliveTime, | 
| 128 |             int capacity, long waitTime, ThreadFactory threadFactory) { | 
| 129 |       DynamicThreadPoolExecutor.DynamicQueue<Runnable> queue = new DynamicThreadPoolExecutor.DynamicQueue<Runnable>( | 
| 130 |                capacity); | 
| 131 |       ThreadPoolExecutor executor = new DynamicThreadPoolExecutor(min, max, keepAliveTime, | 
| 132 |                TimeUnit.MILLISECONDS, queue, threadFactory); | 
| 133 |       executor.setRejectedExecutionHandler(new DynamicThreadPoolExecutor.TimedBlockingPolicy( | 
| 134 |                waitTime)); | 
| 135 |       queue.setThreadPoolExecutor(executor); | 
| 136 |       return executor; | 
| 137 |    } | 
| 138 |   | 
| 139 |    /** | 
| 140 |     * A priority based thread factory, for all Thread priority constants: | 
| 141 |     * <tt>Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY</tt>; | 
| 142 |     * <p/> | 
| 143 |     * This factory is used instead of Executers.DefaultThreadFactory to allow manipulation of | 
| 144 |     * priority and thread owner name. | 
| 145 |     *  | 
| 146 |     * @param namePrefix | 
| 147 |     *           a name prefix for this thread | 
| 148 |     * @return a thread factory based on given priority. | 
| 149 |     */ | 
| 150 |    public static ThreadFactory daemonThreadFactory(String namePrefix) { | 
| 151 |       final ThreadFactory f = Executors.defaultThreadFactory(); | 
| 152 |       final String o = namePrefix + "-"; | 
| 153 |   | 
| 154 |       return new ThreadFactory() { | 
| 155 |          public Thread newThread(Runnable r) { | 
| 156 |             Thread t = f.newThread(r); | 
| 157 |   | 
| 158 |             /* | 
| 159 |              * Thread name: owner-pool-N-thread-M, where N is the sequence number of this factory, | 
| 160 |              * and M is the sequence number of the thread created by this factory. | 
| 161 |              */ | 
| 162 |             t.setName(o + t.getName()); | 
| 163 |   | 
| 164 |             /* override default definition t.setDaemon(false); */ | 
| 165 |             t.setDaemon(true); | 
| 166 |   | 
| 167 |             return t; | 
| 168 |          } | 
| 169 |       }; | 
| 170 |    } | 
| 171 |   | 
| 172 |    /** | 
| 173 |     * A priority based thread factory, for all Thread priority constants: | 
| 174 |     * <tt>Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY</tt>; | 
| 175 |     * <p/> | 
| 176 |     * This factory is used instead of Executers.DefaultThreadFactory to allow manipulation of | 
| 177 |     * priority and thread owner name. | 
| 178 |     *  | 
| 179 |     * @param priority | 
| 180 |     *           The priority to be assigned to each thread; can be either | 
| 181 |     *           <tt>Thread.MIN_PRIORITY, Thread.NORM_PRIORITY</tt> or Thread.MAX_PRIORITY. | 
| 182 |     * @param namePrefix | 
| 183 |     *           a name prefix for this thread | 
| 184 |     * @return a thread factory based on given priority. | 
| 185 |     */ | 
| 186 |    public static ThreadFactory priorityThreadFactory(int priority, String namePrefix) { | 
| 187 |       final ThreadFactory f = DynamicExecutors.daemonThreadFactory(namePrefix); | 
| 188 |       final int p = priority; | 
| 189 |   | 
| 190 |       return new ThreadFactory() { | 
| 191 |          public Thread newThread(Runnable r) { | 
| 192 |             Thread t = f.newThread(r); | 
| 193 |   | 
| 194 |             /* override default thread priority of Thread.NORM_PRIORITY */ | 
| 195 |             if (p != Thread.NORM_PRIORITY) | 
| 196 |                t.setPriority(p); | 
| 197 |   | 
| 198 |             return t; | 
| 199 |          } | 
| 200 |       }; | 
| 201 |    } | 
| 202 |   | 
| 203 |    /** | 
| 204 |     * Cannot instantiate. | 
| 205 |     */ | 
| 206 |    private DynamicExecutors() { | 
| 207 |    } | 
| 208 | } |