Bolts-Android github地址:
https://github.com/BoltsFramework/Bolts-Android
原理比较简单,不需要长篇大论解释,所以写在了代码中的注释里
先看简单使用:
//task简单使用的两个方法call和continueWith
Task.call(new Callable<Object>() {
@Override
public Object call() throws Exception {
return null; //在这个方法中返回需要的结果
}
}, Task.BACKGROUND_EXECUTOR//指定call方法运行的线程,可以指定在主线程
).continueWith(new Continuation<Object, Object>() {
@Override
public Object then(Task<Object> task) throws Exception {
//刚才返回的结果可以从task中获取,然后进行处理
return null;
}
}//可以指定then方法运行的线程,可以指定在主线程,不写的话默认有ThreadLocal);
原理逻辑记录:
public static <TResult> Task<TResult> call(
final Callable<TResult>callable, //Android接口类,类中返回一个call方法
Executor executor, //参数用来选择是否在线程
final CancellationToken ct //用来取消task ) {
final bolts.TaskCompletionSource<TResult> tcs = new bolts.TaskCompletionSource<>();
executor.execute(new Runnable() {
@Override
public void run() {
if (ct != null && ct.isCancellationRequested()) {
tcs.setCancelled();
return;
}
try {
tcs.setResult(callable.call());//接下来看这个,方法中设置当前task
} catch (CancellationException e) {
tcs.setCancelled();
} catch (Exception e) {
if (DEBUG) {
LogUtils.logError(TAG, e.getMessage(), e);
}
tcs.setError(e);
}
}
});
return tcs.getTask();//返回当前task
}
call方法中tcs.setResult(callable.call());会调用到下面这个方法
boolean trySetResult(TResult result) {
synchronized (lock) {
if (complete) {
return false;
}
complete = true;
Task.this.result = result; //设置当前task
lock.notifyAll();
runContinuations();
return true;
}
}
call方法中的主体逻辑完成,之后继续continueWith方法,这个方法中调用了completeImmediately(),可以直接看最后completeImmediately()中传入的参数和做的事情
下面是方法实现:
public <TContinuationResult> Task<TContinuationResult> continueWith(
final Continuation<TResult, TContinuationResult> continuation, //任务完成之后调用,接口类中实现then方法
final Executor executor,//可以指定是否ui线程
final CancellationToken ct//任务取消) {
boolean completed;
final bolts.TaskCompletionSource<TContinuationResult> tcs = new bolts.TaskCompletionSource<>();
synchronized (lock) {
completed = this.isCompleted();
if (!completed) {
this.continuations.add(new Continuation<TResult, Void>() {
@Override
public Void then(Task<TResult> task) {
completeImmediately(tcs, continuation, task, executor, ct);
return null;
}
});
}
}
if (completed) {
completeImmediately(tcs, continuation, this, executor, ct);//第三个参数this把当前task传进去
}
return tcs.getTask();
}
private static <TContinuationResult, TResult> void completeImmediately(
final bolts.TaskCompletionSource<TContinuationResult> tcs,
final Continuation<TResult, TContinuationResult> continuation, final Task<TResult> task,
Executor executor,//任务在哪个线程最终在这个方法中确定
final CancellationToken ct) {
executor.execute(new Runnable() {
@Override
public void run() {
if (ct != null && ct.isCancellationRequested()) {
tcs.setCancelled();
return;
}
try {
TContinuationResult result = continuation.then(task);//continueWith实现的then方法在这儿进行调用,task也在这里传入
tcs.setResult(result);
} catch (CancellationException e) {
tcs.setCancelled();
} catch (Exception e) {
if (DEBUG) {
LogUtils.logError(TAG, e.getMessage(), e);
}
tcs.setError(e);
}
}
});
}
这是一个总体流程,即使有很多其他的附加操作,但结果最终也可以在continueWith方法中拿到,所以我们了解到call怎么怎么把任务给到continueWith就可以
Task这个其实就是一个链式调用,对线程进行了一个封装,写法更简单