深入理解Executor
来源:互联网 发布:linux ls 隐藏文件 编辑:程序博客网 时间:2024/05/21 09:08
一、Executor的定义和用途
1.decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.
将任务提交和每个任务怎么运行(包括线程使用的细节、线程的调度)解耦
2.An Executor is normally used instead of explicitly creating threads
使用场景:使用时一般不显式创建线程
例如:
显式线程:
new Thread(new(RunnableTask())).start();
Executor使用:
Executor executor = anExecutor; //需要创建一个Class implements Executor executor.execute(new RunnableTask1()); executor.execute(new RunnableTask2());
一、Executor的使用
1.Executor可以不异步执行,此时,Executor是在调用者的线程中执行此方法
class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); } }2.Executor也可以在新开辟的线程中执行此方法class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } }3.序列化任务(没看懂)Many Executor implementations impose some sort of limitation on how and when tasks are scheduled. The executor below serializes the submission of tasks to a second executor,class SerialExecutor implements Executor { final Queue<Runnable> tasks = new ArrayDeque<Runnable>(); final Executor executor; Runnable active; SerialExecutor(Executor executor) { this.executor = executor; } public synchronized void execute(final Runnable r) { tasks.offer(new Runnable() { public void run() { try { r.run(); } finally { scheduleNext(); } } }); if (active == null) { scheduleNext(); } } protected synchronized void scheduleNext() { if ((active = tasks.poll()) != null) { executor.execute(active); } } }
0 0
- 深入理解Executor
- Executor的理解与使用
- java Executor学习与理解
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- 深度理解Thread Pool, Executor, Callable/Future
- 理解Thread Pool, Executor, Callable/Future
- spark学习-42-Spark的driver理解和executor理解
- 深入理解
- storm源码之理解Storm中Worker、Executor、Task关系
- hduoj-1862-EXCEL排序(结构体排序)
- 正确理解关键词排名与网站权重的关系-成都网站设计
- Python中的OS模块
- App创建快捷方式
- 手机号码合法性判断
- 深入理解Executor
- 2014求职记(二)
- 泰格至尊版软件加密锁注册机
- 机器学习工具包
- Mahout学习之Mahout安装、配置、入门程序测试
- leetcode79——Word Search
- Servlet和JDBC的登陆实现应用
- Matlab中 pdist 函数详解(各种距离的生成)
- hdu 4747 线段树