java.util.concurrent.ThreadPoolExecutor是一个ExecutorService,它使用可能的几个池化线程之一执行每个提交的任务,通常使用Executors工厂方法配置.它还提供了各种实用程序方法来检查当前线程统计信息并控制它们.
ThreadPoolExecutor方法
Sr.No. | 方法&说明 |
---|---|
1 | protected void afterExecute(Runnable r,Throwable t) 完成给定Runnable的执行后调用的方法. |
2 | void allowCoreThreadTimeOut(boolean value) 设置控制核心线程是否可以超时并在保持活动时间内没有任务到达时终止的策略,在新任务到达时根据需要进行替换. |
3 | boolean allowsCoreThreadTimeOut() 如果此池允许核心线程超时并在keepAlive时间内没有任务到达时终止,则返回true,如果需要则更新任务到达. |
4 | booleanawaitTermination(long timeout, TimeUnit unit) 阻止所有任务完成d在关闭请求之后执行,或者发生超时,或者当前线程被中断,以先发生者为准. |
5 | protected void beforeExecute(Thread t,Runnable r) 在给定线程中执行给定Runnable之前调用的方法. |
6 | void execute(Runnable command) 执行给定的任务将来的某个时间. |
7 | protected void finalize() 当不再引用此执行程序并且没有线程时,调用shutdown. |
8 | int getActiveCount() 返回正在执行任务的大致线程数. |
9 | long getCom pletedTaskCount() 返回已完成执行的大致任务总数. |
10 | int getCorePoolSize() 返回核心线程数. |
11 | long getKeepAliveTime(TimeUnit unit) 返回线程保持活动时间,是超过核心池大小的线程在终止之前保持空闲的时间量. |
12 | int getLargestPoolSize() 返回最大的曾经同时进入池中的线程数. |
13 | int getMaximumPoolSize() 返回允许的最大线程数. |
14 | 在t getPoolSize() 返回池中当前的线程数. |
15 | BlockingQueue 返回此执行程序使用的任务队列. |
15 | RejectedExecutionHandler getRejectedExecutionHandler() 返回不可执行任务的当前处理程序. |
16 | long getTaskCount() 返回已安排执行的大致任务总数. |
17 | ThreadFactory getThreadFactory() 返回用于创建新线程的线程工厂. |
18 | 布尔值isShutdown() 如果此执行程序具有,则返回true已被关闭. |
19 | boolean isTerminated() 如果关闭后所有任务都已完成,则返回true. |
20 | boolean isTerminating () 如果此执行程序在shutdown()或shutdownNow()之后终止但尚未完全终止,则返回true. |
21 | int prestartAllCoreThreads() 启动所有核心线程,导致它们无所事事地等待工作. |
22 | boolean prestartCoreThread() 启动一个核心线程,导致它等待工作. |
23 | void purge() 试图从中删除工作队列所有已取消的Future任务. |
24 | boolean remove(Runnable task) 从执行程序的内部队列中删除此任务(如果存在),因此如果没有,则导致它不能运行已经开始. |
25 | void setCorePoolSize(int corePoolSize) 设置核心线程数. |
26 | void setKeepAliveTime(long time, TimeUnit unit) 设置线程在终止之前可以保持空闲的时间限制. |
27 | void setMaximumPoolSize(int maximumPoolSize) 设置允许的最大线程数. |
28 | void setRejectedExecutionHandler(RejectedExecutionHandler handler) 设置一个不可执行任务的新处理程序. |
29 | void setThreadFactory(ThreadFactory threadFactory) 设置用于创建新线程的线程工厂. |
30 | void shutdown() 启动有序关闭,其中执行先前提交的任务,但不接受任何新任务. |
31 | List 尝试停止所有正在执行的任务,暂停等待任务的处理,并返回等待执行的任务列表. |
32 | protected void terminated() Executor终止时调用的方法. |
33 | String toString() 返回标识此池及其状态的字符串,包括运行状态和估计的工作人员和任务计数的指示. |
示例
以下TestThread程序显示了基于线程的环境中ThreadPoolExecutor接口的使用.
import java.util.concurrent.Executors;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class TestThread { public static void main(final String[] arguments) throws InterruptedException { ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool(); //Stats before tasks execution System.out.println("Largest executions: " + executor.getLargestPoolSize()); System.out.println("Maximum allowed threads: " + executor.getMaximumPoolSize()); System.out.println("Current threads in pool: " + executor.getPoolSize()); System.out.println("Currently executing threads: " + executor.getActiveCount()); System.out.println("Total number of threads(ever scheduled): " + executor.getTaskCount()); executor.submit(new Task()); executor.submit(new Task()); //Stats after tasks execution System.out.println("Core threads: " + executor.getCorePoolSize()); System.out.println("Largest executions: " + executor.getLargestPoolSize()); System.out.println("Maximum allowed threads: " + executor.getMaximumPoolSize()); System.out.println("Current threads in pool: " + executor.getPoolSize()); System.out.println("Currently executing threads: " + executor.getActiveCount()); System.out.println("Total number of threads(ever scheduled): " + executor.getTaskCount()); executor.shutdown(); } static class Task implements Runnable { public void run() { try { Long duration = (long) (Math.random() * 5); System.out.println("Running Task! Thread Name: " + Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(duration); System.out.println("Task Completed! Thread Name: " + Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } }}
这将产生以下结果.
输出
Largest executions: 0Maximum allowed threads: 2147483647Current threads in pool: 0Currently executing threads: 0Total number of threads(ever scheduled): 0Core threads: 0Largest executions: 2Maximum allowed threads: 2147483647Current threads in pool: 2Currently executing threads: 2Total number of threads(ever scheduled): 2Running Task! Thread Name: pool-1-thread-2Running Task! Thread Name: pool-1-thread-1Task Completed! Thread Name: pool-1-thread-1Task Completed! Thread Name: pool-1-thread-2