Understanding Android Threading. (Android Performance Patterns Season 5, Ep. 2)

這是Android性能模式的第五季的第二個視頻娘扩,如果有人為我為什么會從第五季開始看呢着茸,因?yàn)槲抑皇前凑展俜降捻樞蚩聪氯チ耍敲聪旅嫖覀冮_始來看一下這一期的視頻琐旁,視頻地址:

https://www.youtube.com/watch?v=0Z5MZ0jL2BM

Android Performance Patterns.png

接下來我大概會每周翻譯一個視頻的內(nèi)容涮阔。

Pop quiz, hotshot - you're got 48 milliseconds of work to do, but only 16 milliseconds per frame to get it done.

你有一個任務(wù)需要 48 ms 完成,但每一幀的繪制最好控制在 16 ms 內(nèi)完成(這么理解對么)灰殴。

What do you do?

這句話不是應(yīng)該翻譯為你是做什么的么敬特?

My name is Colt McAnils.

視頻中的主講人叫 Colt McAnils。

And while threading on Android can help cure your performance woes, it can also end up creating some huge problems, if you don't understand how it's all working under the hood.

而 Android 中的線程可以幫助我們解決性能問題牺陶,如果你不知道線程是如何工作的伟阔,那么它一可能會造成一些巨大的問題。

So let's take a few minutes and mask sure we're all on the some page.

所以來讓我們花幾分鐘來認(rèn)識一下線程(后面那句不是很會翻譯)掰伸。

See a thread, by default, does three things.

看一個線程皱炉,默認(rèn)情況下,有三件事情狮鸭。

線程要做的三件事.png

It starts,It does some works, and this is that work is done, it terminates.

它的開始合搅,它的做的一些工作和做完任務(wù)后它的結(jié)束。

Now, by itself, that's not too userful.

這句話大概是不是直說單使用線程可能并不是特別有用歧蕉,就是那種一個單獨(dú)任務(wù)的那種灾部?

Instead, what you want is a thread that sticks around for a while, so you can feed it packets of work to operate on.

相反,你想讓線程長時間不斷的運(yùn)行惯退,那就要不斷的給它分配任務(wù)赌髓。

But to do that. you need a little more scaffolding.

要做的這一點(diǎn)需要一些額外的東西。

First, since threads die when they run out of work, you need to have some sort of loop running on the thread to keep it alive.

首先催跪,線程運(yùn)行完任務(wù)它就 go die 了锁蠕, 因此我們需要某個循環(huán)來保持線程處于活動狀態(tài)。

Just make sure to put in an exit condition so you can terminate that loop later.

同時也要保證存在一個退出條件叠荠,便于結(jié)束后可以退出該循環(huán)匿沛。

In addition, you'll need some sort of queue that the loop can pull blocks of work from to excute on.

此外扫责,我們還需要一個工作隊(duì)列榛鼎,可以讓循環(huán)不斷從中拿到任務(wù)執(zhí)行。

And, of course, you'll need some other thread that creates work pactets and pushes them into the queue of execution.

并且,我們還需要一個另外的線程去創(chuàng)建任務(wù)者娱,并且把任務(wù)發(fā)送到隊(duì)列中抡笼。

Now if you 've ever tried to write the setup yourself, you know gets a little gnarly to get all that machine reworking incorrectly.

如果你自己編寫一個這種裝置,那么肯定要花一些精力的黄鳍,大概是這個意思推姻?后面的我不是很會翻譯。

看著像不像Looper.png

Thankfully, thougt, Android has a set of classes to do all that for you.

幸運(yùn)的是框沟,我們不用自己實(shí)現(xiàn)圖中那一套藏古,因?yàn)锳ndroid已經(jīng)事先為我們準(zhǔn)備好了這一切。

For example, the Looper class will keep the thread alive and pop work off a queue to excute on.

Looger 就是那個保持線程活躍并且從隊(duì)列中拿出第一個消息去執(zhí)行的類忍燥。

Looper.png

And rather than always inserting work at the end of that queue, the Handler class gives you the control to push work at the head, the tail, or set a time - based delay that'll keep some work for being processed until that time has passed.

任務(wù)并不是總會插入到隊(duì)列的末尾拧晕,Handler 類 可以讓我們控制把心任務(wù)插到哪里的方法,頭部梅垄,尾部厂捞,或者基于延遲時間,等時間到了再執(zhí)行該任務(wù)队丝。

Handler.png

And don't forget the units of work in Android are explicitly defined as intents or runnables or messages, depending on who's issuing them and who's consuming them.

不過別忘了將 Android 中的任務(wù)明確的指定為 intents or runnables or messages靡馁,具體還要好看使用場景,誰來發(fā)送它們机久,又是誰來消費(fèi)它們臭墨,所以要看具體的使用場景再決定實(shí)用上面三種中的哪一個。

消息隊(duì)列.png

And then the combination of all these things together is called a HandlerThead, which lets this look like this(見下面).

HandlerThead 結(jié)合了上面那些東西膘盖,因?yàn)槲覀冊谥骶€程實(shí)用 Handler 時裙犹,主線程自帶了一個 Looper 而另起的工作線程中想要使用 Handler 需要自己該線程中 Looper 才能運(yùn)行,HandlerThead 就是自帶 Looper 的一個線程衔憨,如下圖:

第一個this指的是這個圖.png
HandlerThead.png

Preffy nifty, huh?

很漂亮是不是叶圃?因?yàn)橐呀?jīng)封裝好了。

So let's look at how you can use this in your application.

讓我們來看看如何讓你使用這些在自己的應(yīng)用中践图。

When the user launches you app, Anndroid create its own proces.

當(dāng)我們運(yùn)行 App 時掺冠,Android 會為我們孵化一個進(jìn)程,如何孵化呢码党?通過一個叫Zygote 孵化器德崭。

Alongside with this, them system creates a thread of execution for you application call the Main thead, which, at its core, is just a Handler thread.

除此之外,系統(tǒng)會為 App 創(chuàng)建一個執(zhí)行線程揖盘,叫主線程眉厨,它的核心就是Handler thread。

手機(jī)啟動創(chuàng)建進(jìn)程兽狭,自帶一個主線程 .png

This Main thread handles processing of events from all over your app, for example, callbacks associated with life cycle information or callbacks from input events, or even events that coming from other application.

主線程會處理 App 中所有事件憾股,例如:與生命周期相關(guān)聯(lián)的回調(diào)函數(shù)鹿蜀,來自輸入事件的回調(diào),甚至來自其他應(yīng)用程序的事件服球。

And most important is that these callbacks can trigger other work that runs on the thread, too, like make a change to the UI will create work packets that allow the UI to be redrawn.

最重要的是茴恰,這些回調(diào)會觸發(fā)其他在主線程上運(yùn)行的任務(wù),就像是 UI 改變后將創(chuàng)建一個重繪 UI 的任務(wù)斩熊。

主線程.png

Basically, this mean that any block of code to your app wants to run has to be pushed into a work queue and then serviced on the Main thread.

基本上往枣,這也意味著 App 想要運(yùn)行的任何代碼都會被添加到到工作隊(duì)列中,然后在主線程上進(jìn)行服務(wù)(我覺得這個服務(wù)不是四大組件的服務(wù)粉渠,只是表達(dá)代碼被執(zhí)行的服務(wù))分冈。

我們的代碼也在主線程中.png

The takeaway here is that with so much work happening on the Main tread, it makes a lot of sense to offload longer work to other threads, as to not disturb the UI system from its rendering duties.

很多工作都發(fā)生在主線程,所以講更多這樣的工作放到其他線程上會很有意義霸株,因?yàn)檫@減輕了主線程的負(fù)擔(dān)丈秩,畢竟不要打擾主線程渲染 UI 職責(zé)才是正確的做法。

耗時操作放在工作線程淳衙,防止UI刷新后掉幀.png

And this is how the entirety of Android's threading model works, lot of packages of work being passed around between threads and worked on as needed.

這就是 Android 線程模型整體的工作原理蘑秽,許多人物在線程在間傳遞,并根據(jù)需需進(jìn)行工作箫攀。

So with all this in mind, some of Android's threading classes make a little bit more sense.

考慮到一切(哪一切肠牲?主線程少做事,多負(fù)責(zé) UI 么靴跛?)缀雳,Android 中的一些線程類很有意義。

See, each threaded class is instended for a specific type of threading work, so picking the right one of your needs is really important.

Android 中的每個線程類都是針對特定的線程工作而設(shè)計(jì)的梢睛,也就是它們都有自己最合適的使用常見肥印,所以根據(jù)你的需求選擇一個正確類是真正重要的事情。

For example, the AsyncTask class is ideal for helping you get work on and off the UI thread in the right way.

例如绝葡,AsyncTask 類可以幫助我們以正確的方式處理任務(wù)和主線程之間的關(guān)系深碱,好像這個上期視頻也提到了。

HandlerThreads are great when you need a dedicated thread for callbacks on land on.

當(dāng)我們需要一個專門的回調(diào)線程時藏畅,HandlerThreads 是非常棒的敷硅。

And ThreadPools work best when you could break your work up into really small packets and then toss them to bunch of waiting threads.

線程池是當(dāng)我們有很多需要太后處理的任務(wù)時不錯的選擇。

And IntentServices are really ideal for background task or when you need to get intent work off the UI thread.

IntentServices 是一個 Services愉阎,非常適用于后臺任務(wù)绞蹦,因?yàn)?IntentServices 內(nèi)部實(shí)現(xiàn)原理很簡單,而且會在完成任務(wù)后自動結(jié)束掉自己榜旦。

And like everything else there's not a silver bullet here.

這句話大概意思是說和其他的一樣幽七,這里并沒有最合適的方法。

But knowing which primitive is best for what stiuation, can save you a lot of headaches.

你需要根據(jù)自己的需求判斷哪個更適合自己當(dāng)前使用溅呢。

Now, if you ever want more insight into how your app is leveraging threading, make sure you spend some time getting comfortable with Systrace.

現(xiàn)在如果你想要了解如何在 App 中使用線程澡屡,你需要花一些時間來學(xué)習(xí)一下 Systrace猿挚,這一個工具。

It's a fancy tool that'll school you on how all that mumbo jumbo is working underneath the hood.

這句話要如何翻譯...

And if you're looking to get school more, make sure you check out the rest of the Android Performance Patterns videos.

人家手動打廣告挪蹭,歡迎來 YouTube 上觀看 Android Performance Patterns videos亭饵。

....以下略休偶,歡迎來Google+ 找他們討論問題...

以上翻譯的肯定會有一些不合適的地方梁厉,如果你看到了請指正我,謝謝踏兜。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末词顾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子碱妆,更是在濱河造成了極大的恐慌肉盹,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疹尾,死亡現(xiàn)場離奇詭異上忍,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)纳本,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門窍蓝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人繁成,你說我怎么就攤上這事吓笙。” “怎么了巾腕?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵面睛,是天一觀的道長。 經(jīng)常有香客問我尊搬,道長叁鉴,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任佛寿,我火速辦了婚禮亲茅,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘狗准。我一直安慰自己克锣,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布腔长。 她就那樣靜靜地躺著袭祟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪捞附。 梳的紋絲不亂的頭發(fā)上巾乳,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天您没,我揣著相機(jī)與錄音,去河邊找鬼胆绊。 笑死氨鹏,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的压状。 我是一名探鬼主播仆抵,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼种冬!你這毒婦竟也來了镣丑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤娱两,失蹤者是張志新(化名)和其女友劉穎莺匠,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體十兢,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡趣竣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了旱物。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片遥缕。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖异袄,靈堂內(nèi)的尸體忽然破棺而出通砍,到底是詐尸還是另有隱情,我是刑警寧澤烤蜕,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布封孙,位于F島的核電站,受9級特大地震影響讽营,放射性物質(zhì)發(fā)生泄漏虎忌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一橱鹏、第九天 我趴在偏房一處隱蔽的房頂上張望膜蠢。 院中可真熱鬧,春花似錦莉兰、人聲如沸挑围。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杉辙。三九已至,卻和暖如春捶朵,著一層夾襖步出監(jiān)牢的瞬間蜘矢,已是汗流浹背狂男。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留品腹,地道東北人岖食。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像舞吭,于是被迫代替她去往敵國和親泡垃。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

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