2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.util.concurrent;
11 import java.util.concurrent.LinkedBlockingQueue;
12 import java.util.concurrent.ThreadPoolExecutor;
13 import java.util.concurrent.TimeUnit;
15 import com.google.common.base.Objects;
16 import com.google.common.base.Objects.ToStringHelper;
17 import com.google.common.util.concurrent.ThreadFactoryBuilder;
20 * A ThreadPoolExecutor with a specified bounded queue capacity that favors creating new threads
21 * over queuing, as the former is faster.
23 * See {@link SpecialExecutors#newFastBlockingThreadPool} for more details.
25 * @author Thomas Pantelis
27 public class FastThreadPoolExecutor extends ThreadPoolExecutor {
29 private static final long DEFAULT_IDLE_TIMEOUT_IN_SEC = 15L;
31 private final String threadPrefix;
32 private final int maximumQueueSize;
35 * Constructs a FastThreadPoolExecutor instance.
37 * @param maximumPoolSize
38 * the maximum number of threads to allow in the pool. Threads will terminate after
39 * being idle for 15 seconds.
40 * @param maximumQueueSize
41 * the capacity of the queue.
43 * the name prefix for threads created by this executor.
45 public FastThreadPoolExecutor( int maximumPoolSize, int maximumQueueSize, String threadPrefix ) {
46 this( maximumPoolSize, maximumQueueSize, DEFAULT_IDLE_TIMEOUT_IN_SEC, TimeUnit.SECONDS,
51 * Constructs a FastThreadPoolExecutor instance.
53 * @param maximumPoolSize
54 * the maximum number of threads to allow in the pool.
55 * @param maximumQueueSize
56 * the capacity of the queue.
57 * @param keepAliveTime
58 * the maximum time that idle threads will wait for new tasks before terminating.
60 * the time unit for the keepAliveTime argument
62 * the name prefix for threads created by this executor.
64 public FastThreadPoolExecutor( int maximumPoolSize, int maximumQueueSize, long keepAliveTime,
65 TimeUnit unit, String threadPrefix ) {
66 // We use all core threads (the first 2 parameters below equal) so, when a task is submitted,
67 // if the thread limit hasn't been reached, a new thread will be spawned to execute
68 // the task even if there is an existing idle thread in the pool. This is faster than
69 // handing the task to an existing idle thread via the queue. Once the thread limit is
70 // reached, subsequent tasks will be queued. If the queue is full, tasks will be rejected.
72 super( maximumPoolSize, maximumPoolSize, keepAliveTime, unit,
73 new LinkedBlockingQueue<Runnable>( maximumQueueSize ) );
75 this.threadPrefix = threadPrefix;
76 this.maximumQueueSize = maximumQueueSize;
78 setThreadFactory( new ThreadFactoryBuilder().setDaemon( true )
79 .setNameFormat( threadPrefix + "-%d" ).build() );
81 if( keepAliveTime > 0 ) {
82 // Need to specifically configure core threads to timeout.
83 allowCoreThreadTimeOut( true );
87 protected ToStringHelper addToStringAttributes( ToStringHelper toStringHelper ) {
88 return toStringHelper;
92 public final String toString() {
93 return addToStringAttributes( Objects.toStringHelper( this )
94 .add( "Thread Prefix", threadPrefix )
95 .add( "Current Thread Pool Size", getPoolSize() )
96 .add( "Largest Thread Pool Size", getLargestPoolSize() )
97 .add( "Max Thread Pool Size", getMaximumPoolSize() )
98 .add( "Current Queue Size", getQueue().size() )
99 .add( "Max Queue Size", maximumQueueSize )
100 .add( "Active Thread Count", getActiveCount() )
101 .add( "Completed Task Count", getCompletedTaskCount() )
102 .add( "Total Task Count", getTaskCount() ) ).toString();