Java 多线程编程-并发编程
对于初学者来说,多线程就是 new 一个 Thread(),然后设置一个 Runnable,调用 Thread.start()方法启动线程。可是这种方式可能存在以下问题
- 子线程不可控制,不可取消(可以自行设置标志位,结束run方法),不可返回结果
- 创建新的线程,并且启动线程,需要抢占资源,可能会超过线程数,同时空闲的线程,没有被重新利用。
为此,这里会介绍两个内容点
- 并发编程基础:Callable 相关内容
- 多线程框架: Executor 相关内容
其中 Executor 的相关类图如下:
细节的介绍,可以看后面的文章。
并发基础类之任务类
Callable 接口
Callable 接口代码如下:
public interface Callable<V> {
/**执行方法,其中V表示返回的结果
*/
V call() throws Exception;
}
和 Runnable 类似,只不过提供了一个 call()方法,可以返回执行的结果,其泛型参数 表示结果类型。
Future 接口
Future 的代码如下:
public interface Future<V> {
/**
* Attempts to cancel execution of this task. This attempt will
* fail if the task has already completed, has already been cancelled,
* or could not be cancelled for some other reason. If successful,
* and this task has not started when {@code cancel} is called,
* this task should never run. If the task has already started,
* then the {@code mayInterruptIfRunning} parameter determines
* whether the thread executing this task should be interrupted in
* an attempt to stop the task.
* 取消任务,如果任务已经完成,已经被取消或者不可被取消,则取消失败。boolean 值参数表示是否可以取消 * 执行中的任务。
*/
boolean cancel(boolean mayInterruptIfRunning);
/**
* Returns {@code true} if this task was cancelled before it completed
* normally. 任务是否已经被取消
*/
boolean isCancelled();
/**
* Returns {@code true} if this task completed. 任务是否已经被完成
*/
boolean isDone();
/**
* Waits if necessary for the computation to complete, and then
* retrieves its result. 获取任务结果,如果任务没有完成,get 方法会阻塞
*
*/
V get() throws InterruptedException, ExecutionException;
/**
* Waits if necessary for at most the given time for the computation
* to complete, and then retrieves its result, if available.
* 等同于 get()只不过,声明了最长阻塞时间,不会无限阻塞
*/
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
总的来说, Future 接口, 提供了以下能力
- 获取任务结果(可阻塞)
- 取消任务,获取任务的简单状态(是否完成,是否被取消等)
这个是异步回调的基础接口,需要好好理解,但是实际上,我们并不会直接用它,而是实现它的子类 FutureTask ,具体的介绍,往后看。
FutureTask 类
FutureTask 类实现 RunnableFuture 接口,而 RunnableFuture接口的实现如下:
public interface RunnableFuture<V> extends Runnable, Future<V> {
void run();
}
从这个角度看,我们可以知道 RunableFuture 实现的规范如下:
- run() 方法,子任务执行的方法
- get() 方法,获取子任务执行结果的能力
- 从 Future 结果继承的,取消子任务,获取子任务简单状态的方法
而 FutureTask 是 RunnableFuture 的具体实现类,根据官方的描述, FutureTask 是可取消的异步操作,提供了 Future 接口的具体实现,实现了开始和取消一个操作的,查询操作是否完成,获取操作结果的方法。它可以包裹一个 Callable 或者 Runnable 可以被 Executor 执行和调度。
我们来看下关键代码
public class FutureTask<V> implements RunnableFuture<V> {
/**
* 表示该任务的执行状态,只可能为以下四种情况
* Possible state transitions:
* NEW -> COMPLETING -> NORMAL 正常完成的流程 new -> completing -> normal
* NEW -> COMPLETING -> EXCEPTIONAL 异常退出的流程 new -> completing -> exceptional
* NEW -> CANCELLED 取消的流程 new -> cancelled
* NEW -> INTERRUPTING -> INTERRUPTED 被中断的流程 new -> interrupting -> interrupted
*/
private volatile int state;
private static final int NEW = 0;
private static final int COMPLETING = 1;
private static final int NORMAL = 2;
private static final int EXCEPTIONAL = 3;
private static final int CANCELLED = 4;
private static final int INTERRUPTING = 5;
private static final int INTERRUPTED = 6;
/** The underlying callable; nulled out after running */
private Callable<V> callable;
/** The result to return or exception to throw from get() */
private Object outcome; // non-volatile, protected by state reads/writes
/** The thread running the callable; CASed during run() 工作线程,用于执行 Callable */
private volatile Thread runner;
/** Treiber stack of waiting threads */
private volatile WaitNode waiters;
// 两个构造方法是一样的,如果传递的是 Runnable 则会被封装成 Callable
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
}
构造方法
构造方法可以传递一个 Callable 或者 Runnable,但是实际上传递 Runnable 也好,最终还是通过 Excutors 工具类,将 Runnable 封装成 Callable,这里的话,会对 state 进行赋值,赋值为 new。
run()方法
public void run() {
if (state != NEW ||
!U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
首先会判断任务的 State,如果 State 不为 new 则直接返回,然后执行 Callable 类型的类变量 callable 的call()方法,依旧构造方法里面传入的 Callable 对象。途中会捕获异常,如果发生异常,则将 result 置为null,并且设置 Exception,最后如果执行执行,则将 call()方法返回的结果,通过set()方法传递给outcome变量,最终通过get()等方法,获取的就是这个 outcome 泛型对象的值。
get()方法
get()方法用于获取任务执行的结果,如果状态还处于未完成的话,则改方法进行阻塞。
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}
这里的关键方法是调用了 awaitDone() 方法,顾名思义,就是一个阻塞方法,其代码如下:
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
long startTime = 0L; // Special value 0L means not yet parked
WaitNode q = null;
boolean queued = false;
for (;;) {//进入一个无限循环中
int s = state;
if (s > COMPLETING) {//判断任务是否被完成,取消,或者中断
if (q != null)
q.thread = null;
return s;
}
else if (s == COMPLETING)//如果任务执行中,则 yeid 线程,yeid 不同于 sleep 和waite,仅仅
//在线程资源紧张的情况下,会暂停线程
// We may have already promised (via isDone) that we are done
// so never return empty-handed or throw InterruptedException
Thread.yield();
else if (Thread.interrupted()) {
removeWaiter(q);
throw new InterruptedException();
}
else if (q == null) {
if (timed && nanos <= 0L)
return s;
q = new WaitNode();//对 q 进行初始化,仅仅在 timed 传参为 true 的时候
}
else if (!queued)//加入队列,并且将 q.next 赋值给 wauters
queued = U.compareAndSwapObject(this, WAITERS,
q.next = waiters, q);
else if (timed) {//以下代码是计算阻塞时间的代码
final long parkNanos;
if (startTime == 0L) { // first time
startTime = System.nanoTime();
if (startTime == 0L)
startTime = 1L;
parkNanos = nanos;
} else {
long elapsed = System.nanoTime() - startTime;
if (elapsed >= nanos) {
removeWaiter(q);
return state;
}
parkNanos = nanos - elapsed;
}
// nanoTime may be slow; recheck before parking
if (state < COMPLETING)//LockSupport 是一个用来创建锁和其它同步类的基本线程元素类
LockSupport.parkNanos(this, parkNanos);//该线程会阻塞特定的事件
}
else
LockSupport.park(this);
}
}
waitDone()方法就是将当前线程加入等待队列(WaitNode 持有当前线程),然后调用 LockSupport的park()方法,将线程阻塞,等待执行完成或者异常。那么这里的等待,又是在哪里被唤醒的呢?在 run()方法里,调用了 set()方法,最终调用了 finishCompletion()方法。
public void run() {
..................
if (ran)
set(result);
.................
}
protected void set(V v) {
if (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
outcome = v;
U.putOrderedInt(this, STATE, NORMAL); // final state
finishCompletion();
}
}
/**
* Removes and signals all waiting threads, invokes done(), and
* nulls out callable.
*/
private void finishCompletion() {
// assert state > COMPLETING;
for (WaitNode q; (q = waiters) != null;) {
if (U.compareAndSwapObject(this, WAITERS, q, null)) {
for (;;) {
Thread t = q.thread;
if (t != null) {
q.thread = null;
LockSupport.unpark(t);//这里对进行 unPark(),接触线程阻塞
}
WaitNode next = q.next;
if (next == null)
break;
q.next = null; // unlink to help gc
q = next;
}
break;
}
}
done();
callable = null; // to reduce footprint
}
FutureTask 中的同步操作,并没有使用锁机制,而是使用了 LockSupport 阻塞和唤起线程,锁操作的话,会额外的耗时。
总的来说,FuterTask 提供了,任务执行的方法,获取任务结果的方法,以及取消的机制,能够实现同步编程的基本操作。
并发基础类之线程相关类
ThreadFactory 接口
总是通过硬编码 new 一个 Thread是不优雅的,所以 java 提供了一个接口,用于实现new Thread这样一个操作,这样的话,你就可以生产出一些你需要的特殊 Thrad,例如达成某种任务的 Thread,高优先级的 Thread 等等。
public interface ThreadFactory {
Thread newThread(Runnable r);
}
总结:工厂模式,往往是为了减少业务耦合。
ThreadGroup 类
ThreadGroup 类,这个类主要是方便线程群的管理,统一设置线程的一些属性。
例如从 setMaxPriority() 方法说起,这个方法统一设置了线程群里面的优先级:
public final void setMaxPriority(int pri) {
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
// Android changed: Clamp to MIN_PRIORITY, MAX_PRIORITY.
// if (pri < Thread.MIN_PRIORITY || pri > Thread.MAX_PRIORITY) {
// return;
// }
if (pri < Thread.MIN_PRIORITY) {
pri = Thread.MIN_PRIORITY;
}
if (pri > Thread.MAX_PRIORITY) {
pri = Thread.MAX_PRIORITY;
}
maxPriority = (parent != null) ? Math.min(pri, parent.maxPriority) : pri;
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].setMaxPriority(pri);
}
}
这个方法主要是遍历类Thread 数组变量 ngroups,然后逐一给线程设置优先级。
总结:ThreadGroup 类,提供了线程组这样一个概念,这个组里面拥有的是一些属性相同的线程。至于实际用途,视开发场景而言。
Executors.DefaultThreadFactory 类
创建一个 ThreadPoolExecutor 如果不传递 ThreadFactory 的话,则会使用 DefaultThreadFactory。
private static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
构造方法里面会通过 Thread.currentThread.getThreadGroup()创建一个 ThreadGroup,然后在 newThread() 方法里面,会给新加入的 Thread,设置 ThreadGroup,这样通过,这个 ThreadFactory 产生的 Thread,就具有一些统一的属性了。
题外话:ThreadGroup 里面使用了数组作为存储的数据结构,看起来好像不太好扩展,而且从数组不可变的角度来考虑,似乎无法理解这一点。
并发基础类之调度器
为什么需要调度器,我们考虑一些情况,如果一个应用进程里面,野蛮的 new 一些 Thread()出来,并且调用了线程的 start(),从操作系统角度考虑,这样肯定会造成资源竞争的,会带来以下一些缺点
- 资源竞争,重要的线程,可能被低优先级线程阻塞。
- 资源浪费,闲置的线程,无法被回收利用。
- 从 Android 的角度考虑, liunx 限制了最大线程数目,超过线程数目,会导致 OOM。
所以,需要线程调度器这样一个管理者的角色。Java从 jdk 1.5 开始在 java.util.concurrent 包里面提供了一系列相关的接口和类,实现这个能力。
先看下类图结构。
- Executor接口 最基本的接口,实现了线程池的 excute()行为
- ExecutorService接口 继承了 Executor 接口,实现了 shutdown(),submit()方法,扩展了停止任务,提交任务的能力
- AbstractExecutorService抽象类 实现了 ExecutorService 接口里面的大部分方法
- ThreadPoolExecutor 类 继承 AbstractExecutorService ,实现了线程池
- ScheduledExecutorService接口 继承ExecutorService接口,提供了周期调度的能力
- ScheduledThreadPoolExecutor类,周期调度的线程池
- Executos 类,线程池的静态工厂方法,提供了四种便捷的线程池
Executor 接口
最基础的是 Executor 接口
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 {@code Executor} implementation.
*
*/
void execute(Runnable command);
}
这个接口类,提供了 excute()方法,可以执行提交一个任务,也就是 执行 Runnable 对象的 run()方法。
ExecutorService 接口
继承 Executor 接口,实现了一些拓展,较为基本的线程池接接口。
public interface ExecutorService extends Executor {
/**停止一次之前提交的任务
*/
void shutdown();
/**
* 停止所以正在执行的列表
* @return list of tasks that never commenced execution
*/
List<Runnable> shutdownNow();
/**
* Returns {@code true} if this executor has been shut down.
*
* @return {@code true} if this executor has been shut down
*/
boolean isShutdown();
/**
* 是否所以任务都被停止了,如果是,则返回 true
* @return {@code true} if all tasks have completed following shut down
*/
boolean isTerminated();
/**
* Blocks until all tasks have completed execution after a shutdown
* request, or the timeout occurs, or the current thread is
* interrupted, whichever happens first.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return {@code true} if this executor terminated and
* {@code false} if the timeout elapsed before termination
* @throws InterruptedException if interrupted while waiting
*/
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
/**
* Submits a value-returning task for execution and returns a
* Future representing the pending results of the task. The
* Future's {@code get} method will return the task's result upon
* successful completion.
* 提交一个 Callable,返回值为 Future,Future 的get()方法会在任务成功完成后,返回结果
* <p>
* If you would like to immediately block waiting
* for a task, you can use constructions of the form
* {@code result = exec.submit(aCallable).get();}
*
* <p>Note: The {@link Executors} class includes a set of methods
* that can convert some other common closure-like objects,
* for example, {@link java.security.PrivilegedAction} to
* {@link Callable} form so they can be submitted.
*
* @param task the task to submit
* @param <T> the type of the task's result
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
<T> Future<T> submit(Callable<T> task);
/**
* Submits a Runnable task for execution and returns a Future
* representing that task. The Future's {@code get} method will
* return the given result upon successful completion.
*
* @param task the task to submit
* @param result the result to return
* @param <T> the type of the result
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
<T> Future<T> submit(Runnable task, T result);
/**
* Submits a Runnable task for execution and returns a Future
* representing that task. The Future's {@code get} method will
* return {@code null} upon <em>successful</em> completion.
*
* @param task the task to submit
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
Future<?> submit(Runnable task);
/**
* Executes the given tasks, returning a list of Futures holding
* their status and results when all complete.
* {@link Future#isDone} is {@code true} for each
* element of the returned list.
* Note that a <em>completed</em> task could have
* terminated either normally or by throwing an exception.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param <T> the type of the values returned from the tasks
* @return a list of Futures representing the tasks, in the same
* sequential order as produced by the iterator for the
* given task list, each of which has completed
* @throws InterruptedException if interrupted while waiting, in
* which case unfinished tasks are cancelled
* @throws NullPointerException if tasks or any of its elements are {@code null}
* @throws RejectedExecutionException if any task cannot be
* scheduled for execution
*/
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
/**
* Executes the given tasks, returning a list of Futures holding
* their status and results
* when all complete or the timeout expires, whichever happens first.
* {@link Future#isDone} is {@code true} for each
* element of the returned list.
* Upon return, tasks that have not completed are cancelled.
* Note that a <em>completed</em> task could have
* terminated either normally or by throwing an exception.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @param <T> the type of the values returned from the tasks
* @return a list of Futures representing the tasks, in the same
* sequential order as produced by the iterator for the
* given task list. If the operation did not time out,
* each task will have completed. If it did time out, some
* of these tasks will not have completed.
* @throws InterruptedException if interrupted while waiting, in
* which case unfinished tasks are cancelled
* @throws NullPointerException if tasks, any of its elements, or
* unit are {@code null}
* @throws RejectedExecutionException if any task cannot be scheduled
* for execution
*/
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException;
/**
* Executes the given tasks, returning the result
* of one that has completed successfully (i.e., without throwing
* an exception), if any do. Upon normal or exceptional return,
* tasks that have not completed are cancelled.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param <T> the type of the values returned from the tasks
* @return the result returned by one of the tasks
* @throws InterruptedException if interrupted while waiting
* @throws NullPointerException if tasks or any element task
* subject to execution is {@code null}
* @throws IllegalArgumentException if tasks is empty
* @throws ExecutionException if no task successfully completes
* @throws RejectedExecutionException if tasks cannot be scheduled
* for execution
*/
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException;
/**
* Executes the given tasks, returning the result
* of one that has completed successfully (i.e., without throwing
* an exception), if any do before the given timeout elapses.
* Upon normal or exceptional return, tasks that have not
* completed are cancelled.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @param <T> the type of the values returned from the tasks
* @return the result returned by one of the tasks
* @throws InterruptedException if interrupted while waiting
* @throws NullPointerException if tasks, or unit, or any element
* task subject to execution is {@code null}
* @throws TimeoutException if the given timeout elapses before
* any task successfully completes
* @throws ExecutionException if no task successfully completes
* @throws RejectedExecutionException if tasks cannot be scheduled
* for execution
*/
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
void shutdown()
停止任务,但是不会等待之前提交任务完成之后,再停止
List showdownNow()
停止只在执行和等待执行的任务,并且返回等待执行的任务列表
Future submit(Callable task)
提交一个 Callable 类型的任务,返回一个可获取结果的 Future 对象,Future.get()方法会返回结果,直到任务完成。
Future
ScheduledExecutorService 接口
这个接口赋予了定时调度的能力,符合一些频率性的开发场景,让开发者轻松实现周期性的任务。
public interface ScheduledExecutorService extends ExecutorService {
/**
* Creates and executes a one-shot action that becomes enabled
* after the given delay.
*
* @param command the task to execute
* @param delay the time from now to delay execution
* @param unit the time unit of the delay parameter
* @return a ScheduledFuture representing pending completion of
* the task and whose {@code get()} method will return
* {@code null} upon completion
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if command is null
*/
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);
/**
* Creates and executes a ScheduledFuture that becomes enabled after the
* given delay.
*
* @param callable the function to execute
* @param delay the time from now to delay execution
* @param unit the time unit of the delay parameter
* @param <V> the type of the callable's result
* @return a ScheduledFuture that can be used to extract result or cancel
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if callable is null
*/
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay, TimeUnit unit);
/**
* Creates and executes a periodic action that becomes enabled first
* after the given initial delay, and subsequently with the given
* period; that is, executions will commence after
* {@code initialDelay}, then {@code initialDelay + period}, then
* {@code initialDelay + 2 * period}, and so on.
*
* <p>The sequence of task executions continues indefinitely until
* one of the following exceptional completions occur:
* <ul>
* <li>The task is {@linkplain Future#cancel explicitly cancelled}
* via the returned future.
* <li>The executor terminates, also resulting in task cancellation.
* <li>An execution of the task throws an exception. In this case
* calling {@link Future#get() get} on the returned future will
* throw {@link ExecutionException}.
* </ul>
* Subsequent executions are suppressed. Subsequent calls to
* {@link Future#isDone isDone()} on the returned future will
* return {@code true}.
*
* <p>If any execution of this task takes longer than its period, then
* subsequent executions may start late, but will not concurrently
* execute.
*
* @param command the task to execute
* @param initialDelay the time to delay first execution
* @param period the period between successive executions
* @param unit the time unit of the initialDelay and period parameters
* @return a ScheduledFuture representing pending completion of
* the series of repeated tasks. The future's {@link
* Future#get() get()} method will never return normally,
* and will throw an exception upon task cancellation or
* abnormal termination of a task execution.
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if command is null
* @throws IllegalArgumentException if period less than or equal to zero
*/
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
/**
* Creates and executes a periodic action that becomes enabled first
* after the given initial delay, and subsequently with the
* given delay between the termination of one execution and the
* commencement of the next.
*
* <p>The sequence of task executions continues indefinitely until
* one of the following exceptional completions occur:
* <ul>
* <li>The task is {@linkplain Future#cancel explicitly cancelled}
* via the returned future.
* <li>The executor terminates, also resulting in task cancellation.
* <li>An execution of the task throws an exception. In this case
* calling {@link Future#get() get} on the returned future will
* throw {@link ExecutionException}.
* </ul>
* Subsequent executions are suppressed. Subsequent calls to
* {@link Future#isDone isDone()} on the returned future will
* return {@code true}.
*
* @param command the task to execute
* @param initialDelay the time to delay first execution
* @param delay the delay between the termination of one
* execution and the commencement of the next
* @param unit the time unit of the initialDelay and delay parameters
* @return a ScheduledFuture representing pending completion of
* the series of repeated tasks. The future's {@link
* Future#get() get()} method will never return normally,
* and will throw an exception upon task cancellation or
* abnormal termination of a task execution.
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if command is null
* @throws IllegalArgumentException if delay less than or equal to zero
*/
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
}
上面还是贴上了官方 API 的解释,下面对每个方法进行译解。
public ScheduledFuture
AbstractExecutorService 抽象类
AbstractExecutorService 是 ExecutorService 的实现类,实现了 invoke(),submit()等方法,具体代码如下:
首先看下 submit()方法,这里实现了三个重载的 submit()方法,介绍一个足以说明:
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTask<T>(callable);
}
首先调用 newTaskFor()方法,将 Callable 包裹成为 RunnableFuture,然后调用 excute()方法,执行任务,由于 excute() 还是空的,所以这里交由子类的 excute() 去实现。
再来看 invoke()方法:
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException {
if (tasks == null)
throw new NullPointerException();
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());
try {
for (Callable<T> t : tasks) {
RunnableFuture<T> f = newTaskFor(t);
futures.add(f);
execute(f);
}
for (int i = 0, size = futures.size(); i < size; i++) {
Future<T> f = futures.get(i);
if (!f.isDone()) {
try { f.get(); }
catch (CancellationException ignore) {}
catch (ExecutionException ignore) {}
}
}
return futures;
} catch (Throwable t) {
cancelAll(futures);
throw t;
}
}
这里的代码也比较简单,也就是遍历传入的 Callable 列表,然后为每个 Callable 创建一个 RunnableFuture,然后执行这些 RunnableFuture,最后通过RunnableFuture.get()方法尝试获得结果,最终返回整个 RunnableFuture 列表。
最后一个方法是 cancleAll()方法:
private static <T> void cancelAll(ArrayList<Future<T>> futures, int j) {
for (int size = futures.size(); j < size; j++)
futures.get(j).cancel(true);
}
其实就是执行所以 Future 的 cancel()方法。
总结:AbstractExecutorService 抽象类,实现了简单的任务提交,取消,调度的逻辑。
ThreadPoolExecutor 类
ThreadPoolExecutor 类,线程池实现类,关于这个类,有必要翻译下官方 API 的介绍
public class ThreadPoolExecutor extends AbstractExecutorService {
* <p>Thread pools address two different problems: they usually
* provide improved performance when executing large numbers of
* asynchronous tasks, due to reduced per-task invocation overhead,
* and they provide a means of bounding and managing the resources,
* including threads, consumed when executing a collection of tasks.
* Each {@code ThreadPoolExecutor} also maintains some basic
* statistics, such as the number of completed tasks.
线程池关注两种问题:1.通过减少每个任务的调度,提高了执行大量异步任务的性能。2.提供资源限定和管理,包括线程在内的资源。
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
}
然后,我们需要了解一些概念
corePoolSize 和 maximumPoolSize
如果 excute() 提交了新的任务,但是当前线程数量小于 corePoolSize,那么会创建一个新的线程来处理请求,即便存在空闲线程。如果超过 corePoolSize 数量但是少于 maximumPoolSize ,仅仅队列是满的情况下,才会创建新的线程。也就是说, corePoolSize 是最佳线程容量,而且 maximumPoolSize 则是阀值。
创建新的线程
你可以通过 ThreadFactory 创建线程,默认的话通过 Executors.defaultThreadFactory() 创建一个线程工厂,这样你就可以通过 ThreadGroup 定制你需要的 Thread 属性了。
Keep-alive Time
如果线程池中的数目超过了 corePoolSize 数目,那么一些闲置的线程如果闲置时间超过了 Keep-Alive Time 的话,则这些闲置的线程会被停止。
线程队列
首先线程队列的入队情况可能是以下情形:
a.低于 corePoolSize 数目的线程,Executor 会新创建一个线程,而不是将空闲线程加入队列中
b.多于 corePoolSize 数目但是少于 maximumPoolSize 数目的线程,则会选择将空闲线程如队,而不是创建 一个新的线程
c.如果线程入队失败,并且由于超过 maximumPoolSize 无法创建新的线程来响应 request,则这个 Task 会被拒绝
与此同时,Java 提供了三种入队策略:
a.直接提交:例如使用 SynchronousQueue 作为队列数据结构,会把 task 直接提交给 Thread,也就意味者,队列里没有空闲的线程,会直接创建一个新的 Thread。
b.无界队列队列:例如使用 LinkedBlockingQueue ,使用无届队列的话,将不再使用 maximumPoolSize 作为界限,但是,corePoolSize 数目的 Thread 没有空闲状态,那么这个任务会被阻塞(能创建新的 Thread 响应该任务,但是不会被执行),这种场景适用于,各个子任务相对独立的情况下。
c. 有界队列:例如使用 ArrayBlockingQueue ,使用有界队列可以保证当前执行的最佳性能,但是反之,这种队列是最难控制和协调的,
Hook Methods
这个类提供了 beforeExecute(Thread,Runnable) 和 afterExcute(Runnable,Throwable)方法用于回调,在执行Task之前和执行Task之后回调。
RejectedExecutionHandler(饱和策略)
所谓饱和策略,就是任务提交失败之后,需要怎么处理这个 Task;在 concurrent 包里面提供了四种策略;介绍如下:
a. ThreadPoolExecutor.AbortPolicy 如果reject 情况发生,则抛出一个 RejectedExecution,默认是该策略
b. ThreadPoolExecutor.CallerRunsPolicy 在reject 情况发生时,如果 Executor 没有 shutdown,则直接在调用者的线程里面执行这个 Task;反之则该 Task会被丢弃。
c. ThreadPoolExecutor.DiscardOldestPolicy 在 reject 情况发生时,如果 Executor 没有 shutdown,会先放弃队列最后一个 Task,然后让新的 Task 入队。反之则,新的 Task 会被丢弃。
d. ThreadPoolExecutor.DiscardPolicy 在 reject 情况发送下,直接丢弃该 Task
总结:预定义的四个 RejectedExecutionHandler,可以通过构造方法传入,也可以自定义类。但是在使用 CallerRunsPolicy 需要注意,由于会在调用者线程直接执行 Runnable 的 run() 方法,我们需要特别注意 UI 主线程的问题。但是实际上来说,线程池已经满了,这会即便是该任务顺利执行,对用户的角度来说,该 Task 可能也不是有用的了,所以,最好的是依赖好的排队策略和线程管理策略,而不是依赖这种补救方法。
ThreadPoolExecutor.Worker
在 ThreadPoolExecutor 有个内部类
内部类 Worker
AtomicInteger Int 类型变量 ct1
先看代码如下:
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
ThreadPoolExecutor 中使用了 AtomicInteger 类型的变量 ct1 有以下两个作用
- 低三十二位保存 worker 线程数目
- 高三位保存 runState,也就是线程池的运行状态
线程池的运行状态可能是下面几个值:
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
同时线程池的状态变化也是有规律的,可能是以下集中变换情况:
*
* RUNNING: Accept new tasks and process queued tasks 可接受新任务,并且处理队列在运行
* SHUTDOWN: Don't accept new tasks, but process queued tasks 不接受新任务,处理队列仍在运行
* STOP: Don't accept new tasks, don't process queued tasks, 既不接受新任务,处理队列也停止了
* and interrupt in-progress tasks
* TIDYING: All tasks have terminated, workerCount is zero,所以任务暂停
* the thread transitioning to state TIDYING
* will run the terminated() hook method
* TERMINATED: terminated() has completed terminated() 方法被调用
* RUNNING -> SHUTDOWN
* On invocation of shutdown(), perhaps implicitly in finalize()
* (RUNNING or SHUTDOWN) -> STOP
* On invocation of shutdownNow()
* SHUTDOWN -> TIDYING
* When both queue and pool are empty
* STOP -> TIDYING
* When pool is empty
* TIDYING -> TERMINATED
* When the terminated() hook method has completed
构造方法
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
//...... 由于篇幅问题,省略了另外两个重载的构造函数
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
在构造方法里面,我们需要注意几个问题;
- 默认的 ThreadFactory 通过 Executors.DefaulThreadFactory() 获得,上面已经介绍过了。
- 默认的 RejectedExecutionHandler 也就是使用 AbortPolicy 策略的 Handler,具体看下源码。
- 提供的数据结构,会决定线程池的的容量和排队策略
Excute() 方法
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
execute() 方法里面主要执行以下操作
- 在活动线程数目小于 corePoolSize 的情况下,创建新的 Thread 响应新的 Task。
- 在活动线程数目大于等于 corePoolSize 的情况下,先加入到任务队列中,如果 Task 成功入队,需要进行 double-check 查看查看执行 Task 的 Thread 是否可用,如果不可用,考虑创建新的 Thread。
- 如果任务队列已经满了,则启动一个新的 Thread,如果失败,则拒绝任务。
然后看代码
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {//活动线程数,小于 corePoolSize
if (addWorker(command, true))//添加新 Thread,ture 参数表示进行 double-check 检查线程数目
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
关于 addWorker() 方法,主要完成以下操作:
- 检查线程池转态是否合法,检查 Task 是否为空,检测 Task 队列是否为空
- 如果第一步检查通过,则原子增加线程数目
这里是详细的解析
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);//获取线程池的状态
// Check if queue empty only if necessary.
// 这里进行的操作是,检测线程池状态是否合法,检测 Task Runnable 是否空,检查工作队列是否为空
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;//返回 fasle 退出该循环
for (;;) {
int wc = workerCountOf(c);//取出线程池线程数目
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))//double-check 线程池数目
return false;
if (compareAndIncrementWorkerCount(c))//增加 workCunt
break retry;//跳出循环
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs)
continue retry;//如果线程状态发生改变,则重试一次
// else CAS failed due to workerCount change; retry inner loop
}
}
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// Recheck while holding lock.
// Back out on ThreadFactory failure or if
// shut down before lock acquired.
int rs = runStateOf(ctl.get());
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
workers.add(w);//加入到 workers HasSet 中,workers 保存了所以的线程
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;//刷新 largestPoolSize
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
t.start();//最终调用的是 Worker.run()也就是 ThreadPoolExecutor.runWorker()
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
在 addWorker() 方法里面调用了 Worker.run()方法,实际上调用的是 ThreadPoolExecutor.runWorker() 方法,那么 runWorker() 方法里面主要执行的操作如下:
-
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();//获取当前线程
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
关于 getTask() 则负责从工作队列中获取 Task,关键的代码解析如下:
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
// 检查线程池的状态以及任务队列是否为空
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
// Are workers subject to culling? 判断是否需要设置等待阻塞时间
//如果设置了允许等待或者线程数目大于 corePoolSize 则会被设置为等待,工作队列会延迟出队
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();//这里决定了直接出队或者等待一定的时间
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
好了这里为止 excute() 方法的执行流程基本清晰了。
线程池的管理
线程池帮助我们管理线程,那么线程池生命周期本身,就是需要我们自己进行管理的。作为 Android 开发者, Activity 或者 Fragment 都有着严格的生命周期,
思考下面几个问题
- 如果我们需要暂停或者恢复线程池里面的工作线程
- 如果我们需要完全销毁线程池里面的所以线程
- 线程池的生命周期,是否应该跟随 Activity
严格意义上, 线程池的生命周期应该不是跟随 Activity 的,因为它和 Activity 一般不存在引用关系,反而如果线程池中还存在存活的线程,那么这个线程池对象就不会被 GC 回收,但是从业务角度考虑,这个线程池的生命周期可能已经结束了,因为用户已经退出了 Activity,所以这里要做区分,如果是跟随 Activity 生命周期的线程池,应该及时取消所有任务,并且回收资源;如果不跟随 Activity ,则需要取消关联的任务;
取消所有任务的方法如下:
List shutdownNow()
停止所有正在执行的活动任务,停止等待的任务,并且返回等待执行的任务列表
void shutdown()
关闭线程池,并且不在接受新的任务,但是阻塞队列里面的任务不会受影响
两者存在区别,从源码角度理解:
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(SHUTDOWN);//转态设置为 SHUTDOWN
interruptIdleWorkers();//中断闲置的线程
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();//保证执行中的任务会被执行
}
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(STOP);//设置状态为 STOP
interruptWorkers();//中断所以线程
tasks = drainQueue();//返回还没被执行的任务
} finally {
mainLock.unlock();
}
tryTerminate();
return tasks;
}
从这个角度我们可以知道,该调用那种 shutdown() 方法来处理线程池的关闭。
ThreadPoolExecutor 使用教程
以在 AsyncTask 中的源码教程,这里编写一个简易版本的 AsyncTask 作为 ThreadPoolExecutor 的使用案例。首先了解我们需要达到的目的:
- 线程池是怎么被创建的,应该关注这个线程池那些参数
- 线程池的入队策略,调度策略,新线程的创建策略
这里写了一个最简单的例子,定时两秒更新 TextView 上的数字,从 0 更新到 100,代码如下:
private void doAsyncTask() {
asyncTask = new AutoCountAsyncTask();
asyncTask.execute("hahah");
}
private void updateProgress(int progress) {
if (autoCountTv != null) {
autoCountTv.setText("进度" + progress + "%");
}
}
public class AutoCountAsyncTask extends AsyncTask<String, Integer, Integer> {
@Override
protected Integer doInBackground(String... params) {
int initalCount = 0;
try {
for (initalCount = 0; initalCount < 100; initalCount++) {
Thread.sleep(1000 * 2);
publishProgress(initalCount);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return initalCount;
}
@Override
protected void onProgressUpdate(Integer... values) {
updateProgress(values[0]);
}
@Override
protected void onPreExecute() {
updateProgress(0);
}
@Override
protected void onPostExecute(Integer integer) {
updateProgress(integer);
}
}
在 AsyncTask 的 excute() 方法里面,就是执行这个任务的开始,实际上,调用的是以下:
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
}
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) {
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
throw new IllegalStateException("Cannot execute task:"
+ " the task is already running.");
case FINISHED:
throw new IllegalStateException("Cannot execute task:"
+ " the task has already been executed "
+ "(a task can be executed only once)");
}
}
mStatus = Status.RUNNING;
onPreExecute();
mWorker.mParams = params;
exec.execute(mFuture);
return this;
}
这里使用了一个 sDefaultExecutor,他是一个串行的调度器,它的用途是不断的把新加入的 Task 插入到双端队列末尾,然后取出双端队列第一个 Task,这里是串行的。
private static class SerialExecutor implements Executor {
final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
Runnable mActive;
public synchronized void execute(final Runnable r) {
mTasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (mActive == null) {
scheduleNext();
}
}
protected synchronized void scheduleNext() {
if ((mActive = mTasks.poll()) != null) {
THREAD_POOL_EXECUTOR.execute(mActive);
}
}
}
然而,真正执行这些 Runable 或者 FutureTask 的的是在 SerialExecutor 的scheduleNext() 方法中,如下:
protected synchronized void scheduleNext() {
if ((mActive = mTasks.poll()) != null) {
THREAD_POOL_EXECUTOR.execute(mActive);//线程池执行 excute() 方法
}
}
而这个 THREAD_POOL_EXECUTOR 的赋值是在 AsyncTask 的 static 代码块里面,如下:
static {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
sPoolWorkQueue, sThreadFactory);
threadPoolExecutor.allowCoreThreadTimeOut(true);
THREAD_POOL_EXECUTOR = threadPoolExecutor;
}
所以最终在 AsyncTask 中执行 Task 的线程池的特点是:
- corePoolSize 是 2 , CPU 数目减1 和 4中最小值,两个数值中的最大值
- maximumPoolSize 是Cpu 数目的两倍加上1
- 超时时间是 30 秒
- 工作队列是 LinkedBlockingQueue,属于无界队列,初始容量是 128,当然超过了这个容量,是无法入队的,即便 LinkedBlockingQueue 是链表结构。
所以我们可以看到,线程池在 AsyncTask 中使用,也是比较简单的。