Understanding Android Threading.
这是Android性能模式的第五季的第二个视频,如果有人为我为什么会从第五季开始看呢,因为我只是按照官方的顺序看下去了,那么下面我们开始来看一下这一期的视频,视频地址:
Android Performance Patterns.png
接下来我大概会每周翻译一个视频的内容。
Pop quiz, hotshot - you're got 48 milliseconds of work to do, but only 16 milliseconds per frame to get it done.
你有一个任务需要 48 ms 完成,但每一帧的绘制最好控制在 16 ms 内完成(这么理解对么)。
What do you do?
这句话不是应该翻译为你是做什么的么?
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.
所以来让我们花几分钟来认识一下线程(后面那句不是很会翻译)。
See a thread, by default, does three things.
看一个线程,默认情况下,有三件事情。
线程要做的三件事.pngIt starts,It does some works, and this is that work is done, it terminates.
它的开始,它的做的一些工作和做完任务后它的结束。
Now, by itself, that's not too userful.
这句话大概是不是直说单使用线程可能并不是特别有用,就是那种一个单独任务的那种?
Instead, what you want is a thread that sticks around for a while, so you can feed it packets of work to operate on.
相反,你想让线程长时间不断的运行,那就要不断的给它分配任务。
But to do that. you need a little more scaffolding.
要做的这一点需要一些额外的东西。
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.
首先,线程运行完任务它就 go die 了, 因此我们需要某个循环来保持线程处于活动状态。
Just make sure to put in an exit condition so you can terminate that loop later.
同时也要保证存在一个退出条件,便于结束后可以退出该循环。
In addition, you'll need some sort of queue that the loop can pull blocks of work from to excute on.
此外,我们还需要一个工作队列,可以让循环不断从中拿到任务执行。
And, of course, you'll need some other thread that creates work pactets and pushes them into the queue of execution.
并且,我们还需要一个另外的线程去创建任务,并且把任务发送到队列中。
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.pngThankfully, thougt, Android has a set of classes to do all that for you.
幸运的是,我们不用自己实现图中那一套,因为Android已经事先为我们准备好了这一切。
For example, the Looper class will keep the thread alive and pop work off a queue to excute on.
Looger 就是那个保持线程活跃并且从队列中拿出第一个消息去执行的类。
Looper.pngAnd 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.
任务并不是总会插入到队列的末尾,Handler 类 可以让我们控制把心任务插到哪里的方法,头部,尾部,或者基于延迟时间,等时间到了再执行该任务。
Handler.pngAnd 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 中的任务明确的指定为 intents or runnables or messages,具体还要好看使用场景,谁来发送它们,又是谁来消费它们,所以要看具体的使用场景再决定实用上面三种中的哪一个。
消息队列.pngAnd then the combination of all these things together is called a HandlerThead, which lets this look like this(见下面).
HandlerThead 结合了上面那些东西,因为我们在主线程实用 Handler 时,主线程自带了一个 Looper 而另起的工作线程中想要使用 Handler 需要自己该线程中 Looper 才能运行,HandlerThead 就是自带 Looper 的一个线程,如下图:
第一个this指的是这个图.png HandlerThead.pngPreffy nifty, huh?
很漂亮是不是?因为已经封装好了。
So let's look at how you can use this in your application.
让我们来看看如何让你使用这些在自己的应用中。
When the user launches you app, Anndroid create its own proces.
当我们运行 App 时,Android 会为我们孵化一个进程,如何孵化呢?通过一个叫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.
除此之外,系统会为 App 创建一个执行线程,叫主线程,它的核心就是Handler thread。
手机启动创建进程,自带一个主线程 .pngThis 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 中所有事件,例如:与生命周期相关联的回调函数,来自输入事件的回调,甚至来自其他应用程序的事件。
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.
最重要的是,这些回调会触发其他在主线程上运行的任务,就像是 UI 改变后将创建一个重绘 UI 的任务。
主线程.pngBasically, 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 想要运行的任何代码都会被添加到到工作队列中,然后在主线程上进行服务(我觉得这个服务不是四大组件的服务,只是表达代码被执行的服务)。
我们的代码也在主线程中.pngThe 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.
很多工作都发生在主线程,所以讲更多这样的工作放到其他线程上会很有意义,因为这减轻了主线程的负担,毕竟不要打扰主线程渲染 UI 职责才是正确的做法。
耗时操作放在工作线程,防止UI刷新后掉帧.pngAnd 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 线程模型整体的工作原理,许多人物在线程在间传递,并根据需需进行工作。
So with all this in mind, some of Android's threading classes make a little bit more sense.
考虑到一切(哪一切?主线程少做事,多负责 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 中的每个线程类都是针对特定的线程工作而设计的,也就是它们都有自己最合适的使用常见,所以根据你的需求选择一个正确类是真正重要的事情。
For example, the AsyncTask class is ideal for helping you get work on and off the UI thread in the right way.
例如,AsyncTask 类可以帮助我们以正确的方式处理任务和主线程之间的关系,好像这个上期视频也提到了。
HandlerThreads are great when you need a dedicated thread for callbacks on land on.
当我们需要一个专门的回调线程时,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.
线程池是当我们有很多需要太后处理的任务时不错的选择。
And IntentServices are really ideal for background task or when you need to get intent work off the UI thread.
IntentServices 是一个 Services,非常适用于后台任务,因为 IntentServices 内部实现原理很简单,而且会在完成任务后自动结束掉自己。
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.
你需要根据自己的需求判断哪个更适合自己当前使用。
Now, if you ever want more insight into how your app is leveraging threading, make sure you spend some time getting comfortable with Systrace.
现在如果你想要了解如何在 App 中使用线程,你需要花一些时间来学习一下 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+ 找他们讨论问题...
以上翻译的肯定会有一些不合适的地方,如果你看到了请指正我,谢谢。