android開發(fā) 之 Bolts-Android

目錄

多線程編程對比: iOS與Android

android的多線程編程有

  • Thread

  • Handler

  • AsyncTask

  • IntentService

android多線程詳細(xì)可以參考android學(xué)習(xí) 之 Service

iOS的多線程編程有

  • NSthread

  • GCD

  • NSOperationQueue

iOS多線程詳細(xì)可以參考Objective-C學(xué)習(xí) 之 GCD / iOS最佳實踐 之 優(yōu)先使用NSOperationQueue而不是GCD / iOS開發(fā) 之 Queue和Thread

對比兩個平臺的多線程編程, android還是稍遜一些

  • Handler和IntentService的多線程編程, 開銷有些大, 而GCD配合Block, 簡潔好用并且功能也很強(qiáng)大

  • AsyncTask處理消息隊列和多線程同步, 是比較費勁的, 而NSOperationQueue, 仍然是簡單好用五顆星

那有沒有一種方案可以讓android變得和iOS一樣簡單好用呢?

答案就是今天的主角BoltsFramework/Bolts-Android

Bolts-Android

Bolts-Android由Parse"榮譽(yù)"出品, 為什么說是"榮譽(yù)"呢?

因為作為Baas的鼻祖, Parse已經(jīng)成功地死在了沙灘上, 但是被Facebook關(guān)門之后, 它仍然發(fā)揮了余熱, Parse的很多項目都在Github Parse上開源了

廢話不多說, 直接看例子吧

Tasks

想要在當(dāng)前線程執(zhí)行Task, 就是這么簡單

Task.call(new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
        XLog.d(Thread.currentThread().getName() + " " + "call in current thread");
        return true;
    }
});

如果想在后臺線程執(zhí)行Task

Task.callInBackground(new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
        XLog.d(Thread.currentThread().getName() + " " + "call in background thread");
        return true;
    }
});

如果想在UI主線程執(zhí)行Task

Task.call(new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
        XLog.d(Thread.currentThread().getName() + " " + "call in main thread");
        return true;
    }
}, Task.UI_THREAD_EXECUTOR);

如果想要延時執(zhí)行Task

Task.delay(2000).continueWith(new Continuation<Void, Void>() {
    @Override
    public Void then(Task<Void> task) throws Exception {
        XLog.d(Thread.currentThread().getName() + " " + "call in main thread (after delay 2 seconds)");
        return null;
    }
});

是不是簡單到讓人無法相信? 其實這里的Task和iOS中的GCD編程是類似的(詳細(xì)可以參考Objective-C學(xué)習(xí) 之 GCD)

Chaining Tasks

熱完身, 我們來看下Bolts-Android的函數(shù)式編程(關(guān)于函數(shù)式編程詳細(xì)可以參考談?wù)労瘮?shù)式響應(yīng)式編程(Functional Reactive Programming))

Task.call(new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
        XLog.d(Thread.currentThread().getName() + " calling");
        return true;
    }
}, Task.UI_THREAD_EXECUTOR).continueWith(new Continuation<Boolean, String>() {
    @Override
    public String then(Task<Boolean> task) throws Exception {
        Thread.sleep(2000);
        XLog.d(Thread.currentThread().getName() + " onSuccess " + task.getResult());

        return "hello bolts";
    }
}, Task.BACKGROUND_EXECUTOR).continueWith(new Continuation<String, Void>() {
    @Override
    public Void then(Task<String> task) throws Exception {
        XLog.d(Thread.currentThread().getName() + " continueWith " + task.getResult());
        return null;
    }
}, Task.UI_THREAD_EXECUTOR);

打印結(jié)果如下

main calling
pool-2-thread-1 onSuccess true
main continueWith hello bolts

通過函數(shù)式的鏈?zhǔn)骄幊? 輕松地實現(xiàn)了Task的同步和多線程的切換(怎么忽然就想到了RxJava了呢? 看來下期可以來個Bolts-Android大戰(zhàn)RxJava的專題)

Group Tasks

Group Task是我取得名字哈, 所謂Group Tasks, 就是指多個Task組成的一組Tasks

Task.call(new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
        XLog.d(Thread.currentThread().getName() + " calling");
        return true;
    }
}, Task.UI_THREAD_EXECUTOR).continueWith(new Continuation<Boolean, List<Task<Void>>>() {
    @Override
    public List<Task<Void>> then(Task<Boolean> task) throws Exception {
        XLog.d(Thread.currentThread().getName() + " tasks start");
        List<Task<Void>> tasks = new ArrayList<Task<Void>>();
        tasks.add(asyncOperation1());
        tasks.add(asyncOperation2());
        Task.whenAll(tasks).waitForCompletion();
        XLog.d(Thread.currentThread().getName() + " tasks end");
        return tasks;
    }
}, Task.BACKGROUND_EXECUTOR).continueWith(new Continuation<List<Task<Void>>, Void>() {
    @Override
    public Void then(Task<List<Task<Void>>> task) throws Exception {
        XLog.d(Thread.currentThread().getName() + " done");
        return null;
    }
}, Task.UI_THREAD_EXECUTOR);

其中asyncOperation1和asyncOperation2的定義如下

private Task<Void> asyncOperation1() {
    return Task.callInBackground(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            XLog.i("asyncOperation1 start");

            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                XLog.e(e.getMessage());
            }

            XLog.i("asyncOperation1 end");
            return null;
        }
    });
}

private Task<Void> asyncOperation2() {
    return Task.callInBackground(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            XLog.i("asyncOperation2 start");

            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                XLog.e(e.getMessage());
            }

            XLog.i("asyncOperation2 end");
            return null;
        }
    });
}

后面的continueWith會等asyncOperation1和asyncOperation2都執(zhí)行完成后再執(zhí)行

打印結(jié)果如下

main calling
pool-2-thread-1 tasks start
asyncOperation1 start
asyncOperation2 start
asyncOperation1 end
asyncOperation2 end
pool-2-thread-1 tasks end
main done

有沒有小試牛刀的感覺? 我們接著來看下面的用法

Tasks in Parallel

final Executor PARALLEL_EXECUTOR = Executors.newCachedThreadPool();

Task.call(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                XLog.i(Thread.currentThread().getName() + " i = " + i);
            }
        }
        return null;
    }
}, PARALLEL_EXECUTOR);

Task.call(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 1) {
                XLog.i(Thread.currentThread().getName() + " i = " + i);
            }
        }
        return null;
    }
}, PARALLEL_EXECUTOR);

打印結(jié)果如下

pool-2-thread-1 i = 0
pool-2-thread-2 i = 50
pool-2-thread-1 i = 1
pool-2-thread-2 i = 51
pool-2-thread-1 i = 2
pool-2-thread-2 i = 52
pool-2-thread-1 i = 3
pool-2-thread-2 i = 53
pool-2-thread-1 i = 4
pool-2-thread-2 i = 54
pool-2-thread-1 i = 5
pool-2-thread-1 i = 6
pool-2-thread-2 i = 55
pool-2-thread-2 i = 56
pool-2-thread-1 i = 7
pool-2-thread-1 i = 8
pool-2-thread-2 i = 57
pool-2-thread-2 i = 58
pool-2-thread-1 i = 9
pool-2-thread-2 i = 59
pool-2-thread-1 i = 10
pool-2-thread-2 i = 60
pool-2-thread-1 i = 11
pool-2-thread-2 i = 61
pool-2-thread-1 i = 12
pool-2-thread-2 i = 62
pool-2-thread-1 i = 13
pool-2-thread-2 i = 63
pool-2-thread-1 i = 14
pool-2-thread-2 i = 64
pool-2-thread-1 i = 15
pool-2-thread-2 i = 65
pool-2-thread-1 i = 16
pool-2-thread-2 i = 66
pool-2-thread-1 i = 17
pool-2-thread-2 i = 67
pool-2-thread-1 i = 18
pool-2-thread-1 i = 19
pool-2-thread-2 i = 68
pool-2-thread-1 i = 20
pool-2-thread-2 i = 69
pool-2-thread-1 i = 21
pool-2-thread-2 i = 70
pool-2-thread-1 i = 22
pool-2-thread-2 i = 71
pool-2-thread-1 i = 23
pool-2-thread-2 i = 72
pool-2-thread-1 i = 24
pool-2-thread-2 i = 73
pool-2-thread-2 i = 74
pool-2-thread-1 i = 25
pool-2-thread-2 i = 75
pool-2-thread-1 i = 26
pool-2-thread-2 i = 76
pool-2-thread-1 i = 27
pool-2-thread-2 i = 77
pool-2-thread-1 i = 28
pool-2-thread-2 i = 78
pool-2-thread-1 i = 29
pool-2-thread-2 i = 79
pool-2-thread-1 i = 30
pool-2-thread-1 i = 31
pool-2-thread-2 i = 80
pool-2-thread-1 i = 32
pool-2-thread-2 i = 81
pool-2-thread-1 i = 33
pool-2-thread-2 i = 82
pool-2-thread-1 i = 34
pool-2-thread-2 i = 83
pool-2-thread-2 i = 84
pool-2-thread-1 i = 35
pool-2-thread-2 i = 85
pool-2-thread-1 i = 36
pool-2-thread-2 i = 86
pool-2-thread-1 i = 37
pool-2-thread-2 i = 87
pool-2-thread-1 i = 38
pool-2-thread-1 i = 39
pool-2-thread-2 i = 88
pool-2-thread-1 i = 40
pool-2-thread-2 i = 89
pool-2-thread-1 i = 41
pool-2-thread-2 i = 90
pool-2-thread-1 i = 42
pool-2-thread-2 i = 91
pool-2-thread-1 i = 43
pool-2-thread-2 i = 92
pool-2-thread-1 i = 44
pool-2-thread-2 i = 93
pool-2-thread-1 i = 45
pool-2-thread-2 i = 94
pool-2-thread-1 i = 46
pool-2-thread-2 i = 95
pool-2-thread-1 i = 47
pool-2-thread-2 i = 96
pool-2-thread-1 i = 48
pool-2-thread-2 i = 97
pool-2-thread-1 i = 49
pool-2-thread-2 i = 98
pool-2-thread-2 i = 99

打印結(jié)果有兩個發(fā)現(xiàn)

  • 線程池有兩個線程: pool-2-thread-1和pool-2-thread-2

  • 線程池里的線程是并行執(zhí)行的: 數(shù)字都是

Tasks in Serial

那有沒有方法可以讓線程池中只有一個線程, 所有Tasks按照queue的方式FIFO依次執(zhí)行呢? 辦法如下

final Executor SERIAL_EXECUTOR = Executors.newSingleThreadExecutor();

Task.call(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
        for (int i = 0; i < 50; i++) {
            XLog.i(Thread.currentThread().getName() + " i = " + i);
        }
        return null;
    }
}, SERIAL_EXECUTOR);

call(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
        for (int i = 50; i < 100; i++) {
            XLog.i(Thread.currentThread().getName() + " i = " + i);
        }
        return null;
    }
}, SERIAL_EXECUTOR);

打印結(jié)果如下

pool-2-thread-1 i = 0
pool-2-thread-1 i = 1
pool-2-thread-1 i = 2
pool-2-thread-1 i = 3
pool-2-thread-1 i = 4
pool-2-thread-1 i = 5
pool-2-thread-1 i = 6
pool-2-thread-1 i = 7
pool-2-thread-1 i = 8
pool-2-thread-1 i = 9
pool-2-thread-1 i = 10
pool-2-thread-1 i = 11
pool-2-thread-1 i = 12
pool-2-thread-1 i = 13
pool-2-thread-1 i = 14
pool-2-thread-1 i = 15
pool-2-thread-1 i = 16
pool-2-thread-1 i = 17
pool-2-thread-1 i = 18
pool-2-thread-1 i = 19
pool-2-thread-1 i = 20
pool-2-thread-1 i = 21
pool-2-thread-1 i = 22
pool-2-thread-1 i = 23
pool-2-thread-1 i = 24
pool-2-thread-1 i = 25
pool-2-thread-1 i = 26
pool-2-thread-1 i = 27
pool-2-thread-1 i = 28
pool-2-thread-1 i = 29
pool-2-thread-1 i = 30
pool-2-thread-1 i = 31
pool-2-thread-1 i = 32
pool-2-thread-1 i = 33
pool-2-thread-1 i = 34
pool-2-thread-1 i = 35
pool-2-thread-1 i = 36
pool-2-thread-1 i = 37
pool-2-thread-1 i = 38
pool-2-thread-1 i = 39
pool-2-thread-1 i = 40
pool-2-thread-1 i = 41
pool-2-thread-1 i = 42
pool-2-thread-1 i = 43
pool-2-thread-1 i = 44
pool-2-thread-1 i = 45
pool-2-thread-1 i = 46
pool-2-thread-1 i = 47
pool-2-thread-1 i = 48
pool-2-thread-1 i = 49
pool-2-thread-1 i = 50
pool-2-thread-1 i = 51
pool-2-thread-1 i = 52
pool-2-thread-1 i = 53
pool-2-thread-1 i = 54
pool-2-thread-1 i = 55
pool-2-thread-1 i = 56
pool-2-thread-1 i = 57
pool-2-thread-1 i = 58
pool-2-thread-1 i = 59
pool-2-thread-1 i = 60
pool-2-thread-1 i = 61
pool-2-thread-1 i = 62
pool-2-thread-1 i = 63
pool-2-thread-1 i = 64
pool-2-thread-1 i = 65
pool-2-thread-1 i = 66
pool-2-thread-1 i = 67
pool-2-thread-1 i = 68
pool-2-thread-1 i = 69
pool-2-thread-1 i = 70
pool-2-thread-1 i = 71
pool-2-thread-1 i = 72
pool-2-thread-1 i = 73
pool-2-thread-1 i = 74
pool-2-thread-1 i = 75
pool-2-thread-1 i = 76
pool-2-thread-1 i = 77
pool-2-thread-1 i = 78
pool-2-thread-1 i = 79
pool-2-thread-1 i = 80
pool-2-thread-1 i = 81
pool-2-thread-1 i = 82
pool-2-thread-1 i = 83
pool-2-thread-1 i = 84
pool-2-thread-1 i = 85
pool-2-thread-1 i = 86
pool-2-thread-1 i = 87
pool-2-thread-1 i = 88
pool-2-thread-1 i = 89
pool-2-thread-1 i = 90
pool-2-thread-1 i = 91
pool-2-thread-1 i = 92
pool-2-thread-1 i = 93
pool-2-thread-1 i = 94
pool-2-thread-1 i = 95
pool-2-thread-1 i = 96
pool-2-thread-1 i = 97
pool-2-thread-1 i = 98
pool-2-thread-1 i = 99

Error Handling

看完上面的內(nèi)容, 想必各位從此都是android多線程的"磚家"了吧, 不過請稍等片刻, 還有一個重要的問題沒有解釋

Task.call(new Callable<String>() {
    @Override
    public String call() throws Exception {
        Thread.sleep(2000);

        if (1 < 10) {
            XLog.d(Thread.currentThread().getName() + " RuntimeException");
            throw new RuntimeException("There was an error.");
        }
        return "hello bolts";
    }
}, Task.BACKGROUND_EXECUTOR).onSuccess(new Continuation<String, Void>() {
    @Override
    public Void then(Task<String> task) throws Exception {
        XLog.d(Thread.currentThread().getName() + " onSuccess");
        return null;
    }
}, Task.UI_THREAD_EXECUTOR).continueWith(new Continuation<Void, Void>() {
    @Override
    public Void then(Task<Void> task) throws Exception {
        if (task.isFaulted()) {
            XLog.e(Thread.currentThread().getName() + " error");
            return null;
        }
        XLog.d(Thread.currentThread().getName() + " done");
        return null;
    }
}, Task.UI_THREAD_EXECUTOR);

請問這里的打印結(jié)果是如何呢?

  • A: onSuccess

  • B: error

  • C: done

  • D: onSuccess & done

正確答案是B

小結(jié)

android多線程的方案還有諸如: yigit/android-priority-jobqueue, 不過經(jīng)過實測, 和Bolts-Android比較起來, android-priority-jobqueue還是"略重量級"了一些, 如果你有更好的方案和實現(xiàn), 歡迎留言和分享

參考

更多文章, 請支持我的個人博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末帘靡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子满葛,更是在濱河造成了極大的恐慌叔磷,老刑警劉巖愁拭,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡东帅,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門球拦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來靠闭,“玉大人帐我,你說我怎么就攤上這事±颍” “怎么了拦键?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長檩淋。 經(jīng)常有香客問我芬为,道長,這世上最難降的妖魔是什么蟀悦? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任媚朦,我火速辦了婚禮,結(jié)果婚禮上熬芜,老公的妹妹穿的比我還像新娘莲镣。我一直安慰自己,他們只是感情好涎拉,可當(dāng)我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布瑞侮。 她就那樣靜靜地躺著,像睡著了一般鼓拧。 火紅的嫁衣襯著肌膚如雪半火。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天季俩,我揣著相機(jī)與錄音钮糖,去河邊找鬼。 笑死酌住,一個胖子當(dāng)著我的面吹牛店归,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播酪我,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼消痛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了都哭?” 一聲冷哼從身側(cè)響起秩伞,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎欺矫,沒想到半個月后纱新,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡穆趴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年脸爱,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片未妹。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡阅羹,死狀恐怖勺疼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捏鱼,我是刑警寧澤执庐,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站导梆,受9級特大地震影響轨淌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜看尼,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一递鹉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧藏斩,春花似錦掂墓、人聲如沸嚎卫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至兆览,卻和暖如春屈溉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背抬探。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工子巾, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人小压。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓线梗,卻偏偏與公主長得像,于是被迫代替她去往敵國和親怠益。 傳聞我的和親對象是個殘疾皇子仪搔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,129評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件溉痢、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評論 4 62
  • 本文章轉(zhuǎn)載于搜狗測試 敏捷測試逐漸成為行業(yè)趨勢,優(yōu)勢在于快速迭代憋他,所以在這個過程中孩饼,測試不能成為短板,測試工作也就...
    夜境閱讀 10,290評論 0 1
  • 貽笑大方: 差齡畸婚遭眾噴竹挡, 男氓女賤貽笑人镀娶, 一朝揚(yáng)灰妃子淚, 空嘆夢碎看墅暈揪罕。 二八佳人空自誤梯码, 八二老朽辱自...
    云之憾閱讀 209評論 0 0
  • 今天跟周先生一起拾掇棗子宝泵,一起分出大小,一起打包轩娶。 今天和周先生約定儿奶,以后睡前一起看書,交流心得鳄抒。 家里有人和自己...
    大老爺?shù)牟焕蠒?/span>閱讀 198評論 0 1