从图中可以看出来,接口Executor是框架知识点的引路者,那就从它讲起!
一:关于Executor的源码
/* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package java.util.concurrent; /** * An object that executes submitted {@link Runnable} tasks. This * interface provides a way of decoupling task submission from the * mechanics of how each task will be run, including details of thread * use, scheduling, etc. An <tt>Executor</tt> is normally used * instead of explicitly creating threads. For example, rather than * invoking <tt>new Thread(new(RunnableTask())).start()</tt> for each * of a set of tasks, you might use: * * @since 1.5 * @author Doug Lea */ public interface Executor { /** * Executes the given command at some time in the future. The command * may execute in a new thread, in a pooled thread, or in the calling * thread, at the discretion of the <tt>Executor</tt> implementation. * * @param command the runnable task * @throws RejectedExecutionException if this task cannot be * accepted for execution. * @throws NullPointerException if command is null */ void execute(Runnable command); }
说明:看源码,简单的不得了,就一个execute方法,参数为Runnable类型的对象!学过线程的都应该知道,Thread类就是Runnable的实现类,那execute方法可以传Thread对象。
在这列举一下Runnable的实现类:
- AsyncBoxView.ChildState 用于Java Swing开发
- FutureTask 用于异步计算
- RenderableImageProducer 用于AWT开发图像异步生成
- SwingWorker 用于Java Swing开发
- Thread 用于Java线程开发
- TimerTask 用于计时器任务
其中标红的是需要重点学习的!
二:Executor的几种实现形式
在Executor的源码中,列举了几种实现方式:
1) 执行程序可以在调用者的线程中立即运行已提交的任务
class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); } }
2) 执行程序将为每个任务生成一个新线程去运行
class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } }
3) 执行程序使任务提交与第二个执行程序保持连续,这说明了一个复合执行程序。
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); } } }
参考资料:
JDK API 1.6.0
http://www.iteye.com/topic/366591
相关推荐
001 - Spark框架 - 简介.avi 002 - Spark框架 - Vs Hadoop.avi 003 - Spark框架 - 核心模块 - 介绍.avi 005 - Spark框架 - ...020 - Spark框架 - 核心概念 - Executor & Core & 并行度.avi 023 - SparkCore - 分布式
1. Java多线程学习(一)Java多线程入门 2. Java多线程学习(二)synchronized关键字(1) 3. Java多线程学习(二)synchronized关键字(2) 4. Java多线程学习(三...9. Java多线程学习(八)线程池与Executor 框架
AutoTestFrame1 前言1.1 目的指导初次使用测试框架的测试人员能够快速入门。通过简单的配置,用例代码完成自动的执行测试用例、管理测试流程、收集测试结果、输出日志、报告等。同时也提供了可扩展的执行器、数据...
的 anagram finder 框架。 有关更多上下文,请参阅。 ANAGRAMMER 由三个主要组件组成: FinderExecutor延伸mesos.Executor DefinerExecutor扩展了mesos.Executor RenderingFinder扩展了mesos.Scheduler并使用 ...
Web框架快速入门 28.1.简介 第二十九章. SpringAir - 参考程序 29.1.简介 29.2.架构 29.3.实现 29.3.1.业务层 29.3.2.服务层 29.3.3.Web层 29.4.总结 第三十章. 数据访问快速入门 30.1.简介 第三十一章. 事务管理...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
6.3.1 线程池和Executor 124 6.3.2 Callable和Future 126 6.3.3 ScheduledExecutorService 127 6.4 线程安全的集合和同步器 128 6.4.1 阻塞队列 128 6.4.2 指定阻塞时间 130 6.4.3 同步器 131 6.4.4 Atomic...
(读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加一条,其实大家可以看官方的教程更好些:http://mybatis.github.io/mybatis-3/,而且如果英文不是很好的那就看...
│ 高并发编程第一阶段24讲、线程间通信快速入门,使用wait和notify进行线程间的数据通信.mp4 │ 高并发编程第一阶段25讲、多Produce多Consume之间的通讯导致出现程序假死的原因分析.mp4 │ 高并发编程第一阶段26...
│ 高并发编程第一阶段24讲、线程间通信快速入门,使用wait和notify进行线程间的数据通信.mp4 │ 高并发编程第一阶段25讲、多Produce多Consume之间的通讯导致出现程序假死的原因分析.mp4 │ 高并发编程第一阶段26...