/** * Pool for running { @link TimedCancelable}, time limited tasks. * *
* Users are provided a {
@link TaskHandle} for each task. The { @link TaskHandle} * supports canceling the task and determining if the task is done running. * *
* The ThreadPool enforces a configurable maximum time interval for tasks. Each * task is guarded by a time out task that will cancel the primary task * if the primary task does not complete within the allowed interval. * *
* Task cancellation includes two actions that are visible for the task task's * {
@link TimedCancelable} * *
Calling { @link Future#cancel(boolean)} to send the task an interrupt and * mark it as done. *
Calling { @link TimedCancelable#cancel()} to send the task a second signal * that it is being canceled. This signal has the benefit that it does not * depend on the tasks interrupt handling policy. *
* Once a task has been canceled its { @link TaskHandle#isDone()} method will * immediately start returning true. * *
* {
@link ThreadPool} performs the following processing when a task completes * *
Cancel the time out task for the completed task. *
Log exceptions that indicate the task did not complete normally. *
*/public class ThreadPool { private static final Logger LOGGER = Logger.getLogger(ThreadPool.class.getName()); /** * The default amount of time in to wait for tasks to complete during during * shutdown. */ public static final int DEFAULT_SHUTDOWN_TIMEOUT_MILLIS = 10 * 1000; /** * Configured amount of time to let tasks run before automatic cancellation. */ private final long maximumTaskLifeMillis; /** * ExecutorService for running submitted tasks. Tasks are only submitted * through completionService. */ private final ExecutorService executor = Executors.newCachedThreadPool( new ThreadNamingThreadFactory("ThreadPoolExecutor")); /** * CompletionService for running submitted tasks. All tasks are submitted * through this CompletionService to provide blocking, queued access to * completion information. */ private final CompletionService completionService = new ExecutorCompletionService
这里主要用到了java的多线程处理框架
TaskHandle类封装了线程执行的返回信息并提供对线程进行操作的方法,其源码如下:
/** * Handle for the management of a { @link TimedCancelable} primary task. */public class TaskHandle { /** * The primary { @link TimedCancelable} that is run by this task to * perform some useful work. */ final TimedCancelable cancelable; /* * The {@link future} for the primary task. */ final Future taskFuture; /* * The time the task starts. */ final long startTime; /** * Create a TaskHandle. * * @param cancelable { @link TimedCancelable} for the primary task. * @param taskFuture { @link Future} for the primary task. * @param startTime startTime for the primary task. */ TaskHandle(TimedCancelable cancelable, Future taskFuture, long startTime) { this.cancelable = cancelable; this.taskFuture = taskFuture; this.startTime = startTime; } /** * Cancel the primary task and the time out task. */ public void cancel() { cancelable.cancel(); taskFuture.cancel(true); } /** * Return true if the primary task has completed. */ public boolean isDone() { return taskFuture.isDone(); }}