[笔记]Executors框架
2016-03-08 20:08
387 查看
框架结构
任务:Runnable和Callable任务的执行:Executor接口->ExecutorService接口
ThreadPoolExecutor
ScheduledThreadPoolExecutor
异步计算的结果:Future接口->FutureTask类
ThreadPoolExecutor
new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,timeunit,runnableTaskQueue,handler)
corePoolSize: 预热到这个线程大小,不再创建新线程,就要放到runnableTaskQueue里面
maximumPoolSize:只有runnableTaskQueue满了,才会创建新线程,最大线程数不超过这个值
runnableTaskQueue: 任务队列
handler:饱和策略
AbortPolicy:直接抛弃
通常使用工厂类Executors创建:
1.FixedThreadPool:满足资源管理的需求,适合负载比较重的服务器
public staticExecutorService newFixedThreadPool(int nThreads) public staticExecutorService newFixedThreadPool(int nThreads,ThreadFactory threadFactory)
源代码:
public static
2.SingleThreadPool: 满足保证顺序的执行每个任务,并且在任意时间点,不会有多个线程是活动的场景
public staticExecutorService newSingleThreadPool()
public staticExecutorService newSingleThreadPool(ThreadFactory threadFactory)
3.CachedThreadPool:大小无界,适用于很多短期异步的小程序,或者是负载较轻的服务器
public staticExecutorService newCachedThreadPool()
public staticExecutorService newCachedThreadPool(ThreadFactory threadFactory)
ScheduledThreadPoolExecutor
使用工厂类Executors创建1.ScheduledThreadPoolExecutor:
public staticExecutorService newScheduledThreadPool(int nThreads)
public staticExecutorService newScheduledThreadPool(int nThreads,ThreadFactory threadFactory)
2.SingleThreadScheduledExecutor:
public staticExecutorService newSingleThreadScheduledExecutor()
public staticExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)
Future接口
当我们把Runnable接口或者Callable接口submit给ThreadPoolExecutor时,会返回一个FutureTask对象,API:<T> Future<T> submit(Callable<T> Task) <T> Future<T> submit(Runnable Task,T result) Future<?> submit(Runnable Task)
Runnable和Callable接口
Executors提供的:public staticCallable<Object> callable(Runnable task)
public static<T>Callable<T> callable(Runnable task,T Result)
相关文章推荐
- 插件管理框架 for Delphi(一)
- 使用CSS框架布局的缺点和优点小结
- 列举PHP的Yii 2框架的开发优势
- Windows窗体的.Net框架绘图技术实现方法
- 浅谈JavaScript 框架分类
- 轻量级javascript 框架Backbone使用指南
- javascript实现框架高度随内容改变的方法
- JS刷新框架外页面七种实现代码
- 超赞的动手创建JavaScript框架的详细教程
- 深入探讨前端框架react
- 简单介绍不用库(框架)自己写ajax
- asp.net4.0框架下验证机制失效的原因及处理办法
- 插件管理框架 for Delphi(二)
- 零基础学习AJAX之AJAX框架
- Ajax 框架学习笔记
- Flex中最好的MVC框架Mate框架
- JavaScript 异步调用框架 (Part 4 - 链式调用)
- JavaScript 异步调用框架 (Part 2 - 用例设计)
- 为什么使用框架 使用框架的优缺点
- JavaScript 异步调用框架 (Part 3 - 代码实现)