JDK1.5 Excutor 与ThreadFactory

Excutor 源码解读:

/**
* 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 {@code Executor} is normally used * instead of explicitly creating threads. For example, rather than * invoking {@code new Thread(new(RunnableTask())).start()} for each * of a set of tasks, you might use: *
 Excutor 是一个可以执行提交过来实现Runnable 接口的对象,这个接口提供了一种将任务的提交以及
 这个任务如何去执行进行了解耦; 包含了线程线程的使用细节,调度等;
 我们通常使用Excutor 而不是显式的创建线程,比如说,我们不会调用对每一个任务进行new Thread (Runnabe ()).start()
 方法;而是使用如下:
* <pre> * Executor executor = <em>anExecutor</em>; * executor.execute(new RunnableTask1()); * executor.execute(new RunnableTask2()); * ... * </pre> 创建一个执行器Excutor
调用执行器的execte方法
* However, the {
@code Executor} interface does not strictly * require that execution be asynchronous. In the simplest case, an * executor can run the submitted task immediately in the caller's * thread: 然而,Excutor执行器并不是严格的要求执行是异步的,一个简单的情况,
一个执行器在调用者线程中会立刻运行提交的任务,不理解看下面例子: * <pre> {
@code * class DirectExecutor implements Executor { * public void execute(Runnable r) { * r.run(); * } * }}</pre> 运行了.run 方法,并不会启用一个新的线程,.start 才会启动一个新的线程,就是这个意思 * More typically, tasks are executed in some thread other * than the caller's thread. The executor below spawns a new thread * for each task. * * <pre> {@code * class ThreadPerTaskExecutor implements Executor { * public void execute(Runnable r) { * new Thread(r).start(); * } * }}</pre> * * Many {@code 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, * illustrating a composite executor. * * <pre> {@code * 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); * } * } * }}</pre> * * The {@code Executor} implementations provided in this package * implement {@link ExecutorService}, which is a more extensive * interface. The {@link ThreadPoolExecutor} class provides an * extensible thread pool implementation. The {@link Executors} class * provides convenient factory methods for these Executors. ************很重要了***************
在这个package 中Excutor 的实现提供了 关于Excutor 接口的扩展ExcutorService
ThreadPoolExcutor 类提供了可扩展的线程池的实现, Excutors 提供了 方便的工厂方法(常用)
* <p>Memory consistency effects: Actions in a thread prior to * submitting a {
@code Runnable} object to an {@code Executor} * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> * its execution begins, perhaps in another thread. *

ThreadFactory 源码解读“”
/**
* An object that creates new threads on demand. Using thread factories * removes hardwiring of calls to {@link Thread#Thread(Runnable) new Thread}, * enabling applications to use special thread subclasses, priorities, etc.
ThreadFactory 是一个按需要创建线程的对象,使用线程工厂消除了使用
Thread(Runnable) new Thread 创建线程的硬连接 能够使应用程序使用使用特殊的线程子类,优先级等;下面是一个简单的例子:
* <p> * The simplest implementation of this interface is just: * <pre> 
{
@code * class SimpleThreadFactory implements ThreadFactory
{ * public Thread newThread(Runnable r) {
* return new Thread(r); * } * }}
</pre>

猜你喜欢

转载自www.cnblogs.com/iscys/p/9693799.html