Andorid jar库源码Bolts原理解析
bolts:
作用:
用于链式执行跨线程代码,且传递数据
栗子:
复制代码
task.call(new callable<boolean>() {
@override
public boolean call() throws exception {
return true;
}
}, task.ui_thread_executor);
task.callinbackground(new callable<boolean>() {
@override
public boolean call() throws exception {
return false;
}
});
task.callinbackground(new callable<boolean>() {
@override
public boolean call() throws exception {
return true;
}
}).onsuccess(new continuation<boolean, object>() {
@override
public object then(task<boolean> task) throws exception {
if (task.getresult()) {
return null;
} else {
return new object();
}
}
}, task.background_executor).continuewith(new continuation<object, object>() {
@override
public object then(task<object> task) throws exception {
if (task.getresult() == null) {
toast.maketext(getbasecontext(), "null", toast.length_short).show();
} else {
toast.maketext(getbasecontext(), "not null", toast.length_short).show();
}
return null;
}
}, task.ui_thread_executor);
复制代码
源码解读:
在内部通过维护多中 executorservice 对象,并且通过串联的方式进行调用。
并且通过维护内部变量在,在指定流程处,就是特定的,值,值通过task的对象getresult拿到。
uithread
复制代码
/**
* an {@link java.util.concurrent.executor} that runs tasks on the ui thread.
*/
private static class uithreadexecutor implements executor {
@override
public void execute(runnable command) {
new handler(looper.getmainlooper()).post(command);
}
}
复制代码
backgroundthread
复制代码
private boltsexecutors() {
background = !isandroidruntime()
? java.util.concurrent.executors.newcachedthreadpool()
: androidexecutors.newcachedthreadpool();
scheduled = executors.newsinglethreadscheduledexecutor();
immediate = new immediateexecutor();
}
复制代码
源码:https://github.com/boltsframework/bolts-android
引入:
implementation 'com.parse.bolts:bolts-android:1.2.0'以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Mybaits 源码解析 (五)----- 面试源码系列:Mapper接口底层原理(为什么Mapper不用写实现类就能访问到数据库?)
-
Mybaits 源码解析 (三)----- Mapper接口底层原理(为什么Mapper不用写实现类就能访问到数据库?)
-
Andorid jar库源码Bolts原理解析
-
andorid jar/库源码解析之retrofit2
-
andorid jar/库源码解析之okhttp3
-
andorid jar/库源码解析
-
andorid jar/库源码解析之Dagger/Dagger2
-
andorid jar/库源码解析之zxing
-
andorid jar/库源码解析之okio
-
andorid jar/库源码解析之RxJava2