Java多线程系列--“JUC线程池”01之 线程池架构(二)
nable r)
20 // 在将来某个时间执行给定任务。
21 void execute(Runnable command)
22 // 当不再引用此执行程序时,调用 shutdown。
23 protected void finalize()
24 // 返回主动执行任务的近似线程数。
25 int getActiveCount()
26 // 返回已完成执行的近似任务总数。
27 long getCompletedTaskCount()
28 // 返回核心线程数。
29 int getCorePoolSize()
30 // 返回线程保持活动的时间,该时间就是超过核心池大小的线程可以在终止前保持空闲的时间值。
31 long getKeepAliveTime(TimeUnit unit)
32 // 返回曾经同时位于池中的最大线程数。
33 int getLargestPoolSize()
34 // 返回允许的最大线程数。
35 int getMaximumPoolSize()
36 // 返回池中的当前线程数。
37 int getPoolSize()
38 // 返回此执行程序使用的任务队列。
39 BlockingQueue getQueue()
40 // 返回用于未执行任务的当前处理程序。
41 RejectedExecutionHandler getRejectedExecutionHandler()
42 // 返回曾计划执行的近似任务总数。
43 long getTaskCount()
44 // 返回用于创建新线程的线程工厂。
45 ThreadFactory getThreadFactory()
46 // 如果此执行程序已关闭,则返回 true。
47 boolean isShutdown()
48 // 如果关闭后所有任务都已完成,则返回 true。
49 boolean isTerminated()
50 // 如果此执行程序处于在 shutdown 或 shutdownNow 之后正在终止但尚未完全终止的过程中,则返回 true。
51 boolean isTerminating()
52 // 启动所有核心线程,使其处于等待工作的空闲状态。
53 int prestartAllCoreThreads()
54 // 启动核心线程,使其处于等待工作的空闲状态。
55 boolean prestartCoreThread()
56 // 尝试从工作队列移除所有已取消的 Future 任务。
57 void purge()
58 // 从执行程序的内部队列中移除此任务(如果存在),从而如果尚未开始,则其不再运行。
59 boolean remove(Runnable task)
60 // 设置核心线程数。
61 void setCorePoolSize(int corePoolSize)
62 // 设置线程在终止前可以保持空闲的时间限制。
63 void setKeepAliveTime(long time, TimeUnit unit)
64 // 设置允许的最大线程数。
65 void setMaximumPoolSize(int maximumPoolSize)
66 // 设置用于未执行任务的新处理程序。
67 void setRejectedExecutionHandler(RejectedExecutionHandler handler)
68 // 设置用于创建新线程的线程工厂。
69 void setThreadFactory(ThreadFactory threadFactory)
70 // 按过去执行已提交任务的顺序发起一个有序的关闭,但是不接受新任务。
71 void shutdown()
72 // 尝试停止所有的活动执行任务、暂停等待任务的处理,并返回等待执行的任务列表。
73 List shutdownNow()
74 // 当 Executor 已经终止时调用的方法。
75 protected void terminated()
复制代码
5. ScheduledExecutorService
ScheduledExecutorService是一个接口,它继承于于ExecutorService。它相当于提供了"延时"和"周期执行"功能的ExecutorService。
ScheduledExecutorService提供了相应的函数接口,可以安排任务在给定的延迟后执行,也可以让任务周期的执行。
ScheduledExecutorService函数列表
复制代码
1 // 创建并执行在给定延迟后启用的 ScheduledFuture。
2 ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit)
3 // 创建并执行在给定延迟后启用的一次性操作。
4 ScheduledFuture< > schedule(Runnable command, long delay, TimeUnit unit)
5 // 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
6 ScheduledFuture< > scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
7 // 创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
8 ScheduledFuture< > scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
复制代码
6. ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor继承于ThreadPoolExecutor,并且实现了ScheduledExecutorService接口。它相当于提供了"延时"和"周期执行"功能的ScheduledExecutorService。
ScheduledThreadPoolExecutor类似于Timer,但是在高并发程序中,ScheduledThreadPoolExecutor的性能要优于Timer。
ScheduledThreadPoolExecutor函数列表
复制代码
1 // 使用给定核心池大小创建一个新 ScheduledThreadPoolExecutor。
2 ScheduledThreadPoolExecutor(int corePoolSize)
3 // 使用给定初始参数创建一个新 ScheduledThreadPoolExec