AndroidExecutors.class
用来构建在android中使用的线程池
public static ExecutorService newCachedThreadPool() { ThreadPoolExecutor executor = new ThreadPoolExecutor( CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); allowCoreThreadTimeout(executor, true); return executor; }这个似乎和AsyncTask中的一样
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { ThreadPoolExecutor executor = new ThreadPoolExecutor( CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); allowCoreThreadTimeout(executor, true); return executor; }提供自定义的线程工厂
public static Executor uiThread() { return INSTANCE.uiThread; }
private static class UIThreadExecutor implements Executor { @Override public void execute(Runnable command) { new Handler(Looper.getMainLooper()).post(command); } }构建主线程的执行器
BoltsExecutors.class
用来构建后台任务的线程池,单例模式,在构造器中初始化:
private BoltsExecutors() { background = !isAndroidRuntime() ? java.util.concurrent.Executors.newCachedThreadPool() : AndroidExecutors.newCachedThreadPool(); scheduled = Executors.newSingleThreadScheduledExecutor(); immediate = new ImmediateExecutor(); }background:后台的任务线程池使用AndroidExecutors中的
scheduled:后台单线程的可定时的线程池
immediate:每提交一个任务,executionDepth加1,并在主线程执行,但是提交的 command太多使executionDepth来不及decrementDepth以至于executionDepth超过15后,每次判断如果executionDepth已经有15个任务,这时候进来的任务就放到background的线程池中执行
CancellationTokenSource.class
任务的控制类:用来关闭任务和判断任务是否已经关闭,里面维护了一个CancellationToken对象
public boolean isCancellationRequested() { return token.isCancellationRequested(); }通过token判断任务是否已经取消
public void cancel() { token.tryCancel(); }通过token取消任务
public CancellationToken getToken() { return token; }获得token对象
CancellationToken.class
任务的取消类,供CancellationTokenSource来调用
Task<TResult>.class
主要方法的调用类
内部类TaskCompletionSource.class
任务结果的控制类:有三种结果:
任务被取消了调用:setCancelled()
任务获得正常结果:setResult(TResult result)
任务出异常了:setError(Exception error)
这三个方法又调用:
public boolean trySetCancelled() { synchronized (lock) { if (complete) { return false; } complete = true; cancelled = true; lock.notifyAll(); runContinuations(); return true; } }
public boolean trySetResult(TResult result) { synchronized (lock) { if (complete) { return false; } complete = true; Task.this.result = result; lock.notifyAll(); runContinuations(); return true; } }
public boolean trySetError(Exception error) { synchronized (lock) { if (complete) { return false; } complete = true; Task.this.error = error; lock.notifyAll(); runContinuations(); return true; } }这三个方法都回循环遍历continuations集合中的Continuation<TResult, ?>对象,并调用continuation.then(this);方法。
1.
Task<Integer> task = Task.callInBackground(new Callable<Integer>() { public Integer call() throws Exception { Thread.sleep(100); return 5; } }).continueWith(new Continuation<Integer, Integer>() { public Integer then(Task<Integer> task) { // 这里的task已经执行完了 return null; } });在后台线程池中执行call()任务中的代码,后台执行完之后通过continueWith()中的Continuation<Integer, Integer>()对象返回,Continuation对象的then方法默认在INSTANCE.immediate( 主线程)中执行。这个模式就类似于AsyncTask,call()方法类似doInBackground(),then()方法类似onPostExecute().
2.
final CancellationTokenSource cts = new CancellationTokenSource(); cts.cancel(); Task.callInBackground(new Callable<Integer>() { public Integer call() throws Exception { Thread.sleep(100); return 5; } }, cts.getToken()).continueWith(new Continuation<Integer, Void>() { public Void then(Task<Integer> task) { return null; } });实例化一个CancellationTokenSource对象,传入他的cts.getToken()对象,这样在onStop()或者onDestory()中就可以手动使用cts.cancel()来取消当前任务。
3.
Task.delay(1000).continueWith(new Continuation<Void, Void>() { public Void then(Task<Void> task) { return null; } });将在1000后执行一个result为null的任务,任务complete后调用Continuation中的then方法
4.
final ArrayList<Task<Integer>> tasks = new ArrayList<Task<Integer>>(); tasks.add(Task.callInBackground(new Callable<Integer>() { public Integer call() throws Exception { Thread.sleep(100); return 5; } })); tasks.add(Task.callInBackground(new Callable<Integer>() { public Integer call() throws Exception { Thread.sleep(100); return 5; } })); tasks.add(Task.callInBackground(new Callable<Integer>() { public Integer call() throws Exception { Thread.sleep(100); return 5; } })); Task.whenAnyResult(tasks).continueWith(new Continuation<Task<Integer>, Void>() { @Override public Void then(Task<Task<Integer>> task) throws Exception { return null; } });获得所有任务集合中第一个完成任务的result