未登录,请登录后再发表信息
最新评论 (0)
播放视频

安卓性能模式第五季——理解安卓线程

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

安卓性能模式
Android Performance Patterns
突击测验 同学们——你有耗时48毫秒的工作
Pop quiz, hotshot– you’ve got48 milliseconds of work to do,
但每帧只有16毫秒的时间来做任务
but only 16 millisecondsper frame to get it done.
你该怎么办?
What do you do?
我叫Colt McAnlis
My name is Colt McAnlis.
虽然安卓里的线程能帮你改善性能问题
And while threading on Androidcan help cure your performance woes,
但如果你不明白底层是如何工作的
it can also end upcreating some huge problems,
它也能搞出一些大麻烦
if you don’t understand howit’s all working under the hood.
所以让我们花几分钟
So let’s take a few minutesand make sure we’re all
了解一下背景知识
on the same page. See,
一个线程默认会做三件事
a thread, by default,does three things.
它会先开始
It starts.
做一些工作 工作做完
It does some works, andthis is that work is done,
它会终止
it terminates. Now,
仅仅这样的话 线程并不是很有用
by itself,that’s not too useful. Instead,
你希望的是线程能停留一段时间
what you want isa thread that sticks around
让你能把工作打包
for a while, so you can feed it packets
给线程去操作
of work to operate on.
想要实现这个 你需要写一个框架
But to do that, you needa little more scaffolding. First,
首先因为线程会在工作做完时终止
since threads diewhen they run out of work,
你需要某种
you need to have some sort
让线程保持存活的
of loop running on the thread
循环语句
to keep it alive.
不过要记得写一个退出循环的条件
Just make sure to put in an exit condition
之后用来终止循环
so you can terminate that loop later.
另外你还需要某种队列
In addition, you’llneed some sort of queue
让循环能从中提取任务块来执行
that the loop can pull blocks of work from to execute on. And,
当然你还需要一些其他线程
of course, you’llneed some other thread
能生成任务块 推入队列中等待执行
that creates work packets and pushes them into the queue for execution. Now,
如果你曾经自己尝试写过这种框架
if you’ve ever triedto write the setup yourself,
你会知道 让这些代码没有错误 不是件容易的事
you know gets a little gnarly to get all of that machine reworking incorrectly. Thankfully,
欣慰的是
though, Android has a set
安卓给我们准备好了这些类
of classes to do all that for you.
例如 Looper类
For example, theLooper class will
能保持线程存活并将工作从队列取出执行
keep the thread alive and pop work off a queue to execute on.
Handler类不仅能将任务
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.
别忘了安卓中任务的基本单位
And don’t forget theunits of work in Android
被明确地定义为Intents或Runables
are explicitly definedas intents or runables
或Messages 这取决于谁在生产
or messages, dependingon who’s issuing them
谁在消费
and who’s consuming them.
把这些所有的组合起来
And then the combination
被叫做Handler线程
of all these things together is called a HandlerThread,
实现了前面提到的流程
which lets this look like this.
[掌声]
[CHEERING]
挺漂亮的 对吧?
Pretty nifty, huh?
我们来看下如何在你的应用中使用它们
So let’s look at how you can use this in your application.
当用户启动你的app时 安卓会创建一个Linux进程
When the user launches your app, Android creates its own Linux process.
另外
Alongside with this,
系统会为你的应用创建一个线程
the system creates a thread of execution for your application
即主线程
called the Main thread,which, at its core,
它的核心就是一个Handler线程
is just a Handler thread.
这个主线程会处理从
This Main thread handlesprocessing of events
你的app产生的所有事件
from all over yourapp, for example,
例如与生命周期相关的一些回调
callbacks associated with lifecycle information or callbacks
还有输入产生的回调 甚至是来自其他应用的事件
from input events, or even events that are coming from other applications.
最重要的是这些回调函数会触发
And most important is thatthese callbacks can trigger
其他工作在主线程上运行
other work thatruns on the thread, too,
例如 改变UI
like makinga change to the UI
这会创建一个任务块允许UI进行重绘
will create work packets thatallow the UI to be redrawn. Basically,
这表示应用中的任何一段代码
this means thatany block of code to your app
想要运行的话 都要被推入任务队列
wants to run has to bepushed into a work queue
然后在主线程运行
and then servicedon the Main thread.
因此要注意的是 有如此多的任务
The takeaway here is thatwith so much work happening
都要在主线程执行 将冗长的任务
on the Main thread, itmakes a lot of sense
放到其它线程上就显得很有必要
to offload longerwork to other threads,
这样就不会打扰UI系统执行其渲染任务
as to not disturb the UI systemfrom its rendering duties.
这也是整个Android线程模型的原理
And this is how the entiretyof Android’s threading model works,
许许多多的任务块
lots of packages ofwork being passed around
在线程中按需传递
between threads andworked on as needed.
记住 不同的Android线程类
So with all this in mind, someof Android’s threading classes
有不同的意义
make a little bit more sense. See,
不同线程类被设计为处理
each threadedclass is intended
不同类型的工作
for a specific typeof threading work,
所以选择对你需要的类是很重要的
so picking the right one foryour needs is really important.
例如AsyncTask类
For example, theAsyncTask class is
更适用于处理
ideal for helping you getwork on and off the UI
UI任务
thread in the right way.
HandlerThread更适用于处理回调方法
HandlerThreads are great when you need a dedicated thread for callbacks to land on.
ThreadPool类能最好地帮助你
And ThreadPools work best whenyou could break your work up
把工作分为很多小块并
into really small packetsand then toss them
把它们扔到一组等待线程中
to a bunch of waiting threads.
IntentService很适合用来处理后台任务
And IntentServices are reallyideal for background task
或是把intent工作从UI线程中移除
or when you need to get intent work off the UI thread.
就像其他所有事 这里并没有万精油的选择
And like everythingelse, there’s not a silver bullet here.
知道那种情形下适合用哪个
But knowing which primitiveis best for what situation,
能免去你很多烦恼
can save you a lot of headaches. Now,
如果你想知道更多
if you ever want more insight
应用该如何
into how your app is
使用线程 你可以花时间
leveraging threading, make sureyou spend some time getting
学习一下Systrace
comfortable with Systrace.
一个很不错的工具
It’s a fancy toolthat’ll school you
告诉你这些神秘的线程在底层是如何工作的
on how all that mumbo jumbo isworking underneath the hood.
如果你想学到更多
And if you’re looking to get schooled more,
你可以观看本系列的
make sure you check out the rest of the Android Performance
其他视频
Patterns videos.
别忘了加入我们的谷歌+社区
And don’t forget to join ourGoogle+ community for more tips
了解更多关于线程的小技巧
and tricks on threading.
好了 保持镇静 改善你的代码 并记住
So keep calm, profile yourcode, and always remember
性能是王道
perf matters.

发表评论

译制信息
视频概述

安卓的线程是怎么回事呢

听录译者

收集自网络

翻译译者

真皮沙发

审核员

审核员1024

视频来源

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

相关推荐