异步编程:JDeferred 2.x 的使用(针对Android)
本庫的地址:https://github.com/jdeferred/jdeferred
依賴方法:
這個庫是流式思想的實踐,類同于rxjava,但更加簡單(當然功能也沒有rxjava強大)
1,核心promise的方法:
// 萬能方法:可以傳入DoneCallback, FailCallback, ProgressCallback, DoneFilter, then() ? // 如下回調 返回 promise (FailFilter, ProgressFilter, DonePipe, FailPipe, ProgressPipe) done() //完成回調 fail() //失敗回調 progress() //進度回調 always() //無論什么結果都會回調2,DeferredObject
Deferred deferred = new DeferredObject(); deferred.resolve("成功了") //需要傳給done的結果 deferred.reject("失敗了")//需要傳給fail的結果 deferred.notify(100)//需要傳給progress的結果 // 實現了promise接口,可以調用promise內的方法3,DeferredManager
//默認的ExecutorService是Executors.newCachedThreadPool(); DeferredManager dm = new DefaultDeferredManager(); DeferredManager dm = new DefaultDeferredManager(executorService); //針對android,AndroidDeferredManager 繼承了DefaultDeferredManager AndroidDeferredManager dm = new AndroidDeferredManager();核心方法:dm.when(params)
params有以下幾種方式:
Callable, Runnable, Future,?
DeferredRunnable, DeferredCallable, DeferredFutureTask,
DeferredAsyncTask
(中三種是上三種的子類,內部持有一個DeferredObject,上三種傳入后也會被封裝為DeferredFutureTask,內部持有一個DeferredObject)
(DeferredFutureTask繼承于FutureTask(實現Runnable\Future,并持有Callable))
(DeferredAsyncTask繼承與AsyncTask,是android特有的,內部持有一個DeferredObject)
4,支持同步和異步
a,同步的使用:
Deferred deferred = new DeferredObject();
Promise promise = deferred.promise();
promise.done(new DoneCallback() {public void onDone(Object result) {...}
}).fail(new FailCallback() {public void onFail(Object rejection) {...}
}).progress(new ProgressCallback() {public void onProgress(Object progress) {...}
}).always(new AlwaysCallback() {public void onAlways(State state, Object result, Object rejection) {...}
});deferred.resolve("done");
deferred.reject("oops");
deferred.notify("100%");
原理:DeferredObject內部持有一個promise,當調用resolve()時,回調promise.done();調用reject()時,回調promise.fail()..以此類推
或者
DeferredManager dm = new DefaultDeferredManager(); Promise p1, p2, p3; // initialize p1, p2, p3 dm.when(p1, p2, p3).done(…).fail(…)原理:DeferredManager內部對promise的返回做了原子性處理,保證了線程安全
b,異步的使用:
三種用法:
第一種:線程中使用deferred
final Deferred deferred = ... Promise promise = deferred.promise(); promise.then(…); Runnable r = new Runnable() {public void run() {while (…) {deferred.notify(myProgress);}deferred.resolve("done");} }第二種:使用DeferredManager 和DeferredRunnable
DeferredManager dm = …; dm.when(new DeferredRunnable<Double>(){public void run() {while (…) {notify(myProgress);}} }).then(…);第三種:使用AndroidDeferredManager(內部封裝了AsyncTask和handler)
AndroidDeferredManager dm = new AndroidDeferredManager(); dm.when(new DeferredAsyncTask<Void, Progress, Result>(){@Overrideprotected Object doInBackgroundSafe(Void... voids) throws Exception {return null;} }).then(...);使用案例:
VUiKit.defer().when(() -> {// 主要操作,比如寫入數據庫等。}).then((v) -> {// 操作完成}).fail((err) -> {// 操作失敗Log.e(TAG,err);}); /** * 簡單封裝一下調用方式 */ public class VUiKit {private static final AndroidDeferredManager gDM = new AndroidDeferredManager();private static final Handler gUiHandler = new Handler(Looper.getMainLooper());public static AndroidDeferredManager defer() {return gDM;}public static int dpToPx(Context context, int dp) {return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,context.getResources().getDisplayMetrics());}public static void post(Runnable r) {gUiHandler.post(r);}public static void postDelayed(long delay, Runnable r) {gUiHandler.postDelayed(r, delay);}public static void sleep(long time) {try {Thread.sleep(time);} catch (InterruptedException e) {e.printStackTrace();}} }贈送源碼:https://github.com/yugu88/MagicWX。
《最完整的Android逆向知識體系》
總結
以上是生活随笔為你收集整理的异步编程:JDeferred 2.x 的使用(针对Android)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android开发万能Utils(工具大
- 下一篇: Charles 抓包 从入门到精通