天天看点

线程通讯:HandlerAndroid进程(Process)与线程(Thread)Handler

Android进程(Process)与线程(Thread)

当一个Android应用程序组件启动时候,如果此时这个程序的其他组件没有正在运行,那么系统会为这个程序以单一线程的形式启动一个新的

Linux进程

。默认情况下,同一应用程序下的所有组件都运行再相同的

进程

线程

(一般称为程序的“主”线程)中。如果一个应用组件启动但这个应用的进程已经存在了(因为这个应用的其他组件已经在之前启动了),那么这个组件将会在这个进程中启动,同时在这个应用的主线程里面执行。然而,你也可以让你的应用里面的组件运行在 不同的进程里面,也可以为任何进程添加额外的线程。

进程(Process)

默认情况下,同一程序的所有组件都运行在相同的进程里面,大多数的应用都是这样的。然而,如果你发现你需要让你的程序里面的某个组件运行在特定的进程里面,你可以在

manifest

文件里面设置。

线程(Thread)

当一个应用启动的时候,系统会为它创建一个线程,称为“主线程”。这个线程很重要。它负责处理调度事件到相关的 user interface widgets,包括绘制事件。你的应用也是在这个线程里面与来自Android UI toolkit (包括来自 android.widget 和 android.view 包的组件)的组件进行交互。因此,这个主线程有时候也被称为

UI 线程

Android的UI 线程不是线程安全的。所以你不能在一个

worker线程

操作你的UI——你必须在UI线程上对你的UI进行操作。这有两条简单的关于Android单线程模型的规则:

  • 不要阻塞 UI 线程
  • 不要在非UI线程里访问 Android UI toolkit

所以,我们需要在worker线程中进行诸如

访问网络数据

查询数据库

等这样的耗时操作。那么,访问到的网络数据结果和数据库查询结果如何传递到UI线程呢?Android系统为我们提供了

Handler

AsyncTask

等工具来解决主线程与worker线程的交互问题。

Handler

了解Handler

原汁原味:

A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it – from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler:

1. to schedule messages and runnables to be executed as some point in the future;

2. to enqueue an action to be performed on a different thread than your own.

Scheduling messages is accomplished with the post,

postAtTime(Runnable, long)

,

postDelayed

,

sendEmptyMessage

,

sendMessage

,

sendMessageAtTime

, and

sendMessageDelayed

methods. The

post

versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the

sendMessage

versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler’s

handleMessage

method (requiring that you implement a subclass of Handler).

When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same

post

or

sendMessage

methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler’s message queue and processed when appropriate.

剖析Handler

关于Handler的实现机制,网络上有很多个版本。大家看过之后都应该能了解其所以然。但我个人更喜欢用图表来说明问题,直观啊!

Handler的类关系图:

线程通讯:HandlerAndroid进程(Process)与线程(Thread)Handler

相关类说明

  • Handler:发送和处理

    Message

  • MessageQueue:负责持有Looper的

    Message链表

    。Handler发送的Message并非直接被添加到MessageQueue中,而是追加在Message链表的后面,通过

    next()

    方法提供链表中的Message给Looper使用;
  • Message : 包含对Message的描述以及可以携带任意的数据,并提供将这些信息发送给Handler的方法

    sendToTarget()

    。另,负责链接Message持有的Handler发送的其他Message。
  • Looper:与Thread协同工作,与Thread是一对一关系。它负责从MessageQueue里取出Message,并调用Message持有的Handler实例的

    dispatchMessage(message)

    方法进行Message分发。Looper会一直执行下去,直到MessageQueue再也不能提供Message。

使用Handler

替代Handler

AsyncTask

替代AsyncTask

RxJava

继续阅读