陕西省城乡住房建设部网站,中国建设监理协会化工监理分会网站,乐山网站制作设计公司,网站建设 深圳转载请注明出处#xff1a;http://www.cnblogs.com/skywang12345/p/3509903.html 线程池架构图 线程池的架构图如下#xff1a; 1. Executor 它是执行者接口#xff0c;它是来执行任务的。准确的说#xff0c;Executor提供了execute()接口来执行已提交的 Runna…转载请注明出处http://www.cnblogs.com/skywang12345/p/3509903.html 线程池架构图 线程池的架构图如下 1. Executor 它是执行者接口它是来执行任务的。准确的说Executor提供了execute()接口来执行已提交的 Runnable 任务的对象。Executor存在的目的是提供一种将任务提交与任务如何运行分离开来的机制。它只包含一个函数接口 void execute(Runnable command) 2. ExecutorService ExecutorService继承于Executor。它是执行者服务接口它是为执行者接口Executor服务而存在的准确的话ExecutorService提供了将任务提交给执行者的接口(submit方法)让执行者执行任务(invokeAll, invokeAny方法)的接口等等。 ExecutorService的函数列表 1 // 请求关闭、发生超时或者当前线程中断无论哪一个首先发生之后都将导致阻塞直到所有任务完成执行。2 boolean awaitTermination(long timeout, TimeUnit unit)3 // 执行给定的任务当所有任务完成时返回保持任务状态和结果的 Future 列表。4 T ListFutureT invokeAll(Collection? extends CallableT tasks)5 // 执行给定的任务当所有任务完成或超时期满时无论哪个首先发生返回保持任务状态和结果的 Future 列表。6 T ListFutureT invokeAll(Collection? extends CallableT tasks, long timeout, TimeUnit unit)7 // 执行给定的任务如果某个任务已成功完成也就是未抛出异常则返回其结果。8 T T invokeAny(Collection? extends CallableT tasks)9 // 执行给定的任务如果在给定的超时期满前某个任务已成功完成也就是未抛出异常则返回其结果。
10 T T invokeAny(Collection? extends CallableT tasks, long timeout, TimeUnit unit)
11 // 如果此执行程序已关闭则返回 true。
12 boolean isShutdown()
13 // 如果关闭后所有任务都已完成则返回 true。
14 boolean isTerminated()
15 // 启动一次顺序关闭执行以前提交的任务但不接受新任务。
16 void shutdown()
17 // 试图停止所有正在执行的活动任务暂停处理正在等待的任务并返回等待执行的任务列表。
18 ListRunnable shutdownNow()
19 // 提交一个返回值的任务用于执行返回一个表示任务的未决结果的 Future。
20 T FutureT submit(CallableT task)
21 // 提交一个 Runnable 任务用于执行并返回一个表示该任务的 Future。
22 Future? submit(Runnable task)
23 // 提交一个 Runnable 任务用于执行并返回一个表示该任务的 Future。
24 T FutureT submit(Runnable task, T result) 3. AbstractExecutorService AbstractExecutorService是一个抽象类它实现了ExecutorService接口。AbstractExecutorService存在的目的是为ExecutorService中的函数接口提供了默认实现。 AbstractExecutorService函数列表由于它的函数列表和ExecutorService一样这里就不再重复列举了。 4. ThreadPoolExecutor ThreadPoolExecutor就是大名鼎鼎的线程池。它继承于AbstractExecutorService抽象类。 ThreadPoolExecutor函数列表 1 // 用给定的初始参数和默认的线程工厂及被拒绝的执行处理程序创建新的 ThreadPoolExecutor。2 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueueRunnable workQueue)3 // 用给定的初始参数和默认的线程工厂创建新的 ThreadPoolExecutor。4 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueueRunnable workQueue, RejectedExecutionHandler handler)5 // 用给定的初始参数和默认被拒绝的执行处理程序创建新的 ThreadPoolExecutor。6 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueueRunnable workQueue, ThreadFactory threadFactory)7 // 用给定的初始参数创建新的 ThreadPoolExecutor。8 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueueRunnable workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)9
10 // 基于完成执行给定 Runnable 所调用的方法。
11 protected void afterExecute(Runnable r, Throwable t)
12 // 如果在保持活动时间内没有任务到达新任务到达时正在替换如果需要则设置控制核心线程是超时还是终止的策略。
13 void allowCoreThreadTimeOut(boolean value)
14 // 如果此池允许核心线程超时和终止如果在 keepAlive 时间内没有任务到达新任务到达时正在替换如果需要则返回 true。
15 boolean allowsCoreThreadTimeOut()
16 // 请求关闭、发生超时或者当前线程中断无论哪一个首先发生之后都将导致阻塞直到所有任务完成执行。
17 boolean awaitTermination(long timeout, TimeUnit unit)
18 // 在执行给定线程中的给定 Runnable 之前调用的方法。
19 protected void beforeExecute(Thread t, Runnable 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 BlockingQueueRunnable 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 ListRunnable shutdownNow()
74 // 当 Executor 已经终止时调用的方法。
75 protected void terminated() 5. ScheduledExecutorService ScheduledExecutorService是一个接口它继承于于ExecutorService。它相当于提供了延时和周期执行功能的ExecutorService。ScheduledExecutorService提供了相应的函数接口可以安排任务在给定的延迟后执行也可以让任务周期的执行。 ScheduledExecutorService函数列表 1 // 创建并执行在给定延迟后启用的 ScheduledFuture。
2 V ScheduledFutureV schedule(CallableV callable, long delay, TimeUnit unit)
3 // 创建并执行在给定延迟后启用的一次性操作。
4 ScheduledFuture? schedule(Runnable command, long delay, TimeUnit unit)
5 // 创建并执行一个在给定初始延迟后首次启用的定期操作后续操作具有给定的周期也就是将在 initialDelay 后开始执行然后在 initialDelayperiod 后执行接着在 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 // 使用给定初始参数创建一个新 ScheduledThreadPoolExecutor。4 ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler)5 // 使用给定的初始参数创建一个新 ScheduledThreadPoolExecutor。6 ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory)7 // 使用给定初始参数创建一个新 ScheduledThreadPoolExecutor。8 ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)9
10 // 修改或替换用于执行 callable 的任务。
11 protected V RunnableScheduledFutureV decorateTask(CallableV callable, RunnableScheduledFutureV task)
12 // 修改或替换用于执行 runnable 的任务。
13 protected V RunnableScheduledFutureV decorateTask(Runnable runnable, RunnableScheduledFutureV task)
14 // 使用所要求的零延迟执行命令。
15 void execute(Runnable command)
16 // 获取有关在此执行程序已 shutdown 的情况下、是否继续执行现有定期任务的策略。
17 boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()
18 // 获取有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
19 boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()
20 // 返回此执行程序使用的任务队列。
21 BlockingQueueRunnable getQueue()
22 // 从执行程序的内部队列中移除此任务如果存在从而如果尚未开始则其不再运行。
23 boolean remove(Runnable task)
24 // 创建并执行在给定延迟后启用的 ScheduledFuture。
25 V ScheduledFutureV schedule(CallableV callable, long delay, TimeUnit unit)
26 // 创建并执行在给定延迟后启用的一次性操作。
27 ScheduledFuture? schedule(Runnable command, long delay, TimeUnit unit)
28 // 创建并执行一个在给定初始延迟后首次启用的定期操作后续操作具有给定的周期也就是将在 initialDelay 后开始执行然后在 initialDelayperiod 后执行接着在 initialDelay 2 * period 后执行依此类推。
29 ScheduledFuture? scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
30 // 创建并执行一个在给定初始延迟后首次启用的定期操作随后在每一次执行终止和下一次执行开始之间都存在给定的延迟。
31 ScheduledFuture? scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
32 // 设置有关在此执行程序已 shutdown 的情况下是否继续执行现有定期任务的策略。
33 void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)
34 // 设置有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
35 void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)
36 // 在以前已提交任务的执行中发起一个有序的关闭但是不接受新任务。
37 void shutdown()
38 // 尝试停止所有正在执行的任务、暂停等待任务的处理并返回等待执行的任务列表。
39 ListRunnable shutdownNow()
40 // 提交一个返回值的任务用于执行返回一个表示任务的未决结果的 Future。
41 T FutureT submit(CallableT task)
42 // 提交一个 Runnable 任务用于执行并返回一个表示该任务的 Future。
43 Future? submit(Runnable task)
44 // 提交一个 Runnable 任务用于执行并返回一个表示该任务的 Future。
45 T FutureT submit(Runnable task, T result) 7. Executors Executors是个静态工厂类。它通过静态工厂方法返回ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 等类的对象。 Executors函数列表 1 // 返回 Callable 对象调用它时可运行给定特权的操作并返回其结果。2 static CallableObject callable(PrivilegedAction? action)3 // 返回 Callable 对象调用它时可运行给定特权的异常操作并返回其结果。4 static CallableObject callable(PrivilegedExceptionAction? action)5 // 返回 Callable 对象调用它时可运行给定的任务并返回 null。6 static CallableObject callable(Runnable task)7 // 返回 Callable 对象调用它时可运行给定的任务并返回给定的结果。8 static T CallableT callable(Runnable task, T result)9 // 返回用于创建新线程的默认线程工厂。
10 static ThreadFactory defaultThreadFactory()
11 // 创建一个可根据需要创建新线程的线程池但是在以前构造的线程可用时将重用它们。
12 static ExecutorService newCachedThreadPool()
13 // 创建一个可根据需要创建新线程的线程池但是在以前构造的线程可用时将重用它们并在需要时使用提供的 ThreadFactory 创建新线程。
14 static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)
15 // 创建一个可重用固定线程数的线程池以共享的无界队列方式来运行这些线程。
16 static ExecutorService newFixedThreadPool(int nThreads)
17 // 创建一个可重用固定线程数的线程池以共享的无界队列方式来运行这些线程在需要时使用提供的 ThreadFactory 创建新线程。
18 static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)
19 // 创建一个线程池它可安排在给定延迟后运行命令或者定期地执行。
20 static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
21 // 创建一个线程池它可安排在给定延迟后运行命令或者定期地执行。
22 static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)
23 // 创建一个使用单个 worker 线程的 Executor以无界队列方式来运行该线程。
24 static ExecutorService newSingleThreadExecutor()
25 // 创建一个使用单个 worker 线程的 Executor以无界队列方式来运行该线程并在需要时使用提供的 ThreadFactory 创建新线程。
26 static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)
27 // 创建一个单线程执行程序它可安排在给定延迟后运行命令或者定期地执行。
28 static ScheduledExecutorService newSingleThreadScheduledExecutor()
29 // 创建一个单线程执行程序它可安排在给定延迟后运行命令或者定期地执行。
30 static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)
31 // 返回 Callable 对象调用它时可在当前的访问控制上下文中执行给定的 callable 对象。
32 static T CallableT privilegedCallable(CallableT callable)
33 // 返回 Callable 对象调用它时可在当前的访问控制上下文中使用当前上下文类加载器作为上下文类加载器来执行给定的 callable 对象。
34 static T CallableT privilegedCallableUsingCurrentClassLoader(CallableT callable)
35 // 返回用于创建新线程的线程工厂这些新线程与当前线程具有相同的权限。
36 static ThreadFactory privilegedThreadFactory()
37 // 返回一个将所有已定义的 ExecutorService 方法委托给指定执行程序的对象但是使用强制转换可能无法访问其他方法。
38 static ExecutorService unconfigurableExecutorService(ExecutorService executor)
39 // 返回一个将所有已定义的 ExecutorService 方法委托给指定执行程序的对象但是使用强制转换可能无法访问其他方法。
40 static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) 线程池示例 下面通过示例来对线程池的使用做简单演示。 1 import java.util.concurrent.Executors;2 import java.util.concurrent.ExecutorService;3 4 public class ThreadPoolDemo1 {5 6 public static void main(String[] args) {7 // 创建一个可重用固定线程数的线程池8 ExecutorService pool Executors.newFixedThreadPool(2);9 // 创建实现了Runnable接口对象Thread对象当然也实现了Runnable接口
10 Thread ta new MyThread();
11 Thread tb new MyThread();
12 Thread tc new MyThread();
13 Thread td new MyThread();
14 Thread te new MyThread();
15 // 将线程放入池中进行执行
16 pool.execute(ta);
17 pool.execute(tb);
18 pool.execute(tc);
19 pool.execute(td);
20 pool.execute(te);
21 // 关闭线程池
22 pool.shutdown();
23 }
24 }
25
26 class MyThread extends Thread {
27
28 Override
29 public void run() {
30 System.out.println(Thread.currentThread().getName() is running.);
31 }
32 } 运行结果 pool-1-thread-1 is running.
pool-1-thread-2 is running.
pool-1-thread-1 is running.
pool-1-thread-2 is running.
pool-1-thread-1 is running. 结果说明主线程中创建了线程池pool线程池的容量是2。即线程池中最多能同时运行2个线程。紧接着将ta,tb,tc,td,te这3个线程添加到线程池中运行。最后通过shutdown()关闭线程池。 转载于:https://www.cnblogs.com/kexianting/p/8549795.html