Bug 1430: New common/util concurrent classes
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / FastThreadPoolExecutor.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.util.concurrent;
10
11 import java.util.concurrent.LinkedBlockingQueue;
12 import java.util.concurrent.ThreadPoolExecutor;
13 import java.util.concurrent.TimeUnit;
14
15 import com.google.common.base.Objects;
16 import com.google.common.base.Objects.ToStringHelper;
17 import com.google.common.util.concurrent.ThreadFactoryBuilder;
18
19 /**
20  * A ThreadPoolExecutor with a specified bounded queue capacity that favors creating new threads
21  * over queuing, as the former is faster.
22  * <p>
23  * See {@link SpecialExecutors#newFastBlockingThreadPool} for more details.
24  *
25  * @author Thomas Pantelis
26  */
27 public class FastThreadPoolExecutor extends ThreadPoolExecutor {
28
29     private static final long DEFAULT_IDLE_TIMEOUT_IN_SEC = 15L;
30
31     private final String threadPrefix;
32     private final int maximumQueueSize;
33
34     /**
35      * Constructs a FastThreadPoolExecutor instance.
36      *
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.
42      * @param threadPrefix
43      *            the name prefix for threads created by this executor.
44      */
45     public FastThreadPoolExecutor( int maximumPoolSize, int maximumQueueSize, String threadPrefix ) {
46         this( maximumPoolSize, maximumQueueSize, DEFAULT_IDLE_TIMEOUT_IN_SEC, TimeUnit.SECONDS,
47               threadPrefix );
48     }
49
50     /**
51      * Constructs a FastThreadPoolExecutor instance.
52      *
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.
59      * @param unit
60      *            the time unit for the keepAliveTime argument
61      * @param threadPrefix
62      *            the name prefix for threads created by this executor.
63      */
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.
71
72         super( maximumPoolSize, maximumPoolSize, keepAliveTime, unit,
73                new LinkedBlockingQueue<Runnable>( maximumQueueSize ) );
74
75         this.threadPrefix = threadPrefix;
76         this.maximumQueueSize = maximumQueueSize;
77
78         setThreadFactory( new ThreadFactoryBuilder().setDaemon( true )
79                                                  .setNameFormat( threadPrefix + "-%d" ).build() );
80
81         if( keepAliveTime > 0 ) {
82             // Need to specifically configure core threads to timeout.
83             allowCoreThreadTimeOut( true );
84         }
85     }
86
87     protected ToStringHelper addToStringAttributes( ToStringHelper toStringHelper ) {
88         return toStringHelper;
89     }
90
91     @Override
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();
103     }
104 }