com.hfg.util.PrioritizedThreadPoolExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.util;
import java.util.concurrent.*;
//------------------------------------------------------------------------------
/**
* A version of ThreadPoolExecutor that works with a PriorityBlockingQueue.
*
* @author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class PrioritizedThreadPoolExecutor extends ThreadPoolExecutor
{
//--------------------------------------------------------------------------
/**
* Creates a new {@code PrioritizedThreadPoolExecutor} with the given initial
* parameters and
* {@linkplain Executors#defaultThreadFactory default thread factory}.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @throws IllegalArgumentException if one of the following holds:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code handler} is null
*/
public PrioritizedThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
PriorityBlockingQueue workQueue)
{
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
//--------------------------------------------------------------------------
/**
* Creates a new {@code PrioritizedThreadPoolExecutor} with the given initial
* parameters and
* {@linkplain Executors#defaultThreadFactory default thread factory}.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code handler} is null
*/
public PrioritizedThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
PriorityBlockingQueue workQueue,
RejectedExecutionHandler handler)
{
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
}
//--------------------------------------------------------------------------
@Override
public Future> submit(Runnable inTask)
{
return submit(inTask, Priority.MEDIUM);
}
//--------------------------------------------------------------------------
public Future> submit(Runnable inTask, Priority inPriority)
{
if (inTask == null) throw new NullPointerException();
RunnableFuture ftask = newTaskFor(inTask, null, inPriority);
execute(ftask);
return ftask;
}
//--------------------------------------------------------------------------
@Override
public Future submit(Runnable inTask, T inResult)
{
return submit(inTask, inResult, Priority.MEDIUM);
}
//--------------------------------------------------------------------------
public Future submit(Runnable task, T result, Priority inPriority)
{
if (task == null) throw new NullPointerException();
RunnableFuture ftask = newTaskFor(task, result, inPriority);
execute(ftask);
return ftask;
}
//--------------------------------------------------------------------------
@Override
public Future submit(Callable inTask)
{
if (inTask == null) throw new NullPointerException();
RunnableFuture ftask = newTaskFor(inTask);
execute(ftask);
return ftask;
}
//--------------------------------------------------------------------------
public Future submit(Callable inTask, Priority inPriority)
{
if (inTask == null) throw new NullPointerException();
RunnableFuture ftask = newTaskFor(inTask, inPriority);
execute(ftask);
return ftask;
}
//--------------------------------------------------------------------------
/**
* Returns a {@code RunnableFuture} for the given runnable and default
* value.
*
* @param inRunnable the runnable task being wrapped
* @param inValue the default value for the returned future
* @param the type of the given value
* @return a {@code RunnableFuture} which, when run, will run the
* underlying runnable and which, as a {@code Future}, will yield
* the given value as its result and provide for cancellation of
* the underlying task
* @since 1.6
*/
@Override
protected RunnableFuture newTaskFor(Runnable inRunnable, T inValue)
{
return newTaskFor(inRunnable, inValue, Priority.MEDIUM);
}
//--------------------------------------------------------------------------
/**
* Returns a {@code RunnableFuture} for the given runnable and default
* value.
*
* @param inRunnable the runnable task being wrapped
* @param inValue the default value for the returned future
* @param the type of the given value
* @param inPriority the job's priority
* @return a {@code RunnableFuture} which, when run, will run the
* underlying runnable and which, as a {@code Future}, will yield
* the given value as its result and provide for cancellation of
* the underlying task
* @since 1.6
*/
protected RunnableFuture newTaskFor(Runnable inRunnable, T inValue, Priority inPriority)
{
return new PrioritizedFutureTask(inRunnable, inValue).setPriority(inPriority);
}
//--------------------------------------------------------------------------
/**
* Returns a {@code RunnableFuture} for the given callable task.
*
* @param inCallable the callable task being wrapped
* @param the type of the callable's result
* @return a {@code RunnableFuture} which, when run, will call the
* underlying callable and which, as a {@code Future}, will yield
* the callable's result as its result and provide for
* cancellation of the underlying task
* @since 1.6
*/
@Override
protected RunnableFuture newTaskFor(Callable inCallable)
{
return newTaskFor(inCallable, Priority.MEDIUM);
}
//--------------------------------------------------------------------------
/**
* Returns a {@code RunnableFuture} for the given callable task.
*
* @param inCallable the callable task being wrapped
* @param the type of the callable's result
* @param inPriority the job's priority
* @return a {@code RunnableFuture} which, when run, will call the
* underlying callable and which, as a {@code Future}, will yield
* the callable's result as its result and provide for
* cancellation of the underlying task
* @since 1.6
*/
protected RunnableFuture newTaskFor(Callable inCallable, Priority inPriority)
{
return new PrioritizedFutureTask(inCallable).setPriority(inPriority);
}
}