5分钟完全理解android handler

Handler机制简介

Handler是android中最重要组成部分之一,Handler机制可以看做是一个消息阻塞队列,APP启动后很快就进入死循环(while循环),不断的读取消息队列中的消息,每个线程最多只有一个消息队列,没有消息时就阻塞,有就立马执行。所有消息排队执行,因为是一个线程,所以同时只能执行一个消息。android的view绘制,事件响应(点击,触摸屏幕等)都是把消息发送到了主线程的消息队列,等待android APP的执行(这点可以通过手动抛出异常查看错误堆栈来验证)。包括自己在主线程new 的handler最终也是把消息插入到了主线程消息队列中。从以上来看android主线程大部分时间是空闲的。当点击屏幕后手机能立马响应也可以看出android主线程大部分时间是空闲的。虽然主线程要处理的事情狠多,很杂,很琐碎(view布局、绘制,事件分发等等),但处理时间都很短暂,可以保证很快处理完毕,然后等待下一个消息的到来。android handler机制简可以实现所有view相关的操作都在主线程进行,从而避免了使用 锁 。具体实现代码 如下。

java工程实现Handler机制代码

下面的代码跟android的handler机制主要原理完全一致,但不依赖android系统。

     
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
     
     
import com.handler.Handler;
import com.handler.Looper;
import com.handler.Message;
public class Main {
public static void main(String[] args) {
new Main().start();
}
private void start(){
//创建该线程唯一的消息队列,线程安全的阻塞队列
Looper.prepare();
onCreate();
//死循环,阻塞式,执行下面代码后主线程就会去获取消息队列里的消息,没有消息时就阻塞,有就执行。执行Looper.loop前即使消息队列里有消息,消息也不会执行,因为主线程还没有去检查消息队列。
Looper.loop();
//下面 的代码通常不会执行,除非手动让主线程消息队列退出。退出主线程消息队列后android的view布局、绘制,事件分发就不执行了,所以android APP也没必要继续执行了,所以android采用了抛出异常的方式结束APP。
System.out.println( "exit........");
throw new RuntimeException( "Main thread loop unexpectedly exited");
}
private void onCreate() {
//
// 下面的操作相当于运行在android的UI线程中
//
final Thread thread = Thread.currentThread();
System.out.println( "main thread=" + thread);
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
//若thread == Thread.currentThread(),则证明已经运行在主线程中了
System.out.println( "current thread is main thread? " + (thread == Thread.currentThread()));
System.out.println(msg);
System.out.println();
}
};
// 测试1 主线程创建handler,子线程使用该handler发送消息
new Thread() {
public void run() {
try { //模拟耗时操作
Thread.sleep( 1000 * 2);
} catch (InterruptedException e) {
}
Message message = new Message();
message.obj = "new Thread" + Thread.currentThread();
message.what = ( int) System.currentTimeMillis();
//在子线程中发送消息
handler.sendMessage(message);
try {
Thread.sleep( 1000 * 2);
} catch (InterruptedException e) {
}
message = new Message();
message.obj = "hanler...waht==1" ;
message.what = 1;
//在子线程中发送消息
handler.sendMessage(message);
message = new Message();
message.obj = "hanler...waht==2" ;
message.what = 2;
//在子线程中发送消息
handler.sendMessage(message);
message = new Message();
message.obj = "hanler...waht==3" ;
message.what = 3;
//在子线程中发送消息
handler.sendMessage(message);
};
}.start();
// 测试2 在thread内部创建handler,结果会抛出异常
new Thread() {
public void run() {
try {
sleep( 1000 * 3);
} catch (InterruptedException e) {
}
/*
* 在线程内部使用默认构造函数创建handler会抛出异常。
* android中也可以在子线程中创建Handler,但要在初始化时传入Looper,
* Looper.getMainLooper()获取到的就是主线程的Looper,所以可以这样创建
*
* new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
//运行在主线程中
}
};
*/
Handler h = new Handler() {
public void handleMessage(Message msg) {
System.out.println( "haneler msg...." + msg);
};
};
Message message = new Message();
message.obj = "handler in new Thread";
message.what = ( int) System.currentTimeMillis();
//在子线程中发送消息
h.sendMessage(message);
};
}.start();
//
// 上面的操作相当于运行在android的UI线程中
//
}
}

运行结果

     
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     
     
main thread=Thread[main,5,main]
current thread is main thread? true
what=18175614 obj=new ThreadThread[Thread-0,5,main]
Exception in thread "Thread-1" java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at com.handler.Handler.<init>(Handler.java:14)
at Main$3$1.<init>(Main.java:103)
at Main$3.run(Main.java:103)
current thread is main thread? true
what=1 obj=hanler...waht==1
current thread is main thread? true
what=2 obj=hanler...waht==2
current thread is main thread? true
what=3 obj=hanler...waht==3

Handler代码

     
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
     
     
package com.handler;
public class Handler {
private MessageQueue messageQueue;
public Handler() {
Looper looper=Looper.myLooper();
if (looper== null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
this.messageQueue=looper.messageQueue;
}
public void sendMessage(Message msg) {
//Looper循环中发现message后,调用message.targer就得到了当前handler,使用taget.handleMessage
//就把消息转发给了发送message时的handler的handleMessage函数
msg.target= this;
messageQueue.enqueueMessage(msg);
}
public void handleMessage(Message msg) {
}
}

Looper代码

     
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
     
     
package com.handler;
public class Looper {
private static final ThreadLocal<Looper> threadLocal= new ThreadLocal<>();
/**
* 存储Message的队列,阻塞式,没有消息则一直等待
*/
final MessageQueue messageQueue;
private Looper() {
messageQueue= new MessageQueue();
}
/**为该线程创建Looper,
* 若该线程已经有Looper了则不需要再次调用prepare
*/
public static void prepare() {
if (threadLocal.get() != null) {
throw new RuntimeException( "Only one Looper may be created per thread");
}
threadLocal.set( new Looper() );
}
public static void loop() {
Looper looper=myLooper();
if (looper == null) {
throw new RuntimeException( "No Looper; Looper.prepare() wasn't called on this thread.");
}
MessageQueue messageQueue=looper.messageQueue;
for(;;){
Message message=messageQueue.next();
message.target.handleMessage(message);
}
}
/**
* 获取当先线程的Looper
* @return
*/
public static Looper myLooper() {
return threadLocal.get();
}
}

MessageQueued代码

     
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
     
     
package com.handler;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class MessageQueue {
private BlockingQueue<Message>blockingQueue= new LinkedBlockingQueue<>();
/**
* 阻塞式,没有消息则一直等待
* @return
*/
public Message next() {
try {
return blockingQueue.take();
} catch (InterruptedException e) {
throw new RuntimeException();
}
}
/**
* 插入到消息队列尾部
* @param message
*/
void enqueueMessage(Message message) {
try {
blockingQueue.put(message);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

ThreadLocal简单实现

ThreadLocal内部原理和下面实现方式不同,但达到的效果是相同的,本篇主要介绍Handler机制,简化了ThreadLocal

     
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
     
     
package com.handler;
import java.util.HashMap;
import java.util.Map;
/**
* ThreadLocal简单实现
* @author Young
*
* @param <T>
*/
public class ThreadLocal<T> {
private Map<Thread,T>map;
public ThreadLocal() {
map= new HashMap<>();
}
public void set(T obj) {
map.put(Thread.currentThread(),obj);
}
public T get() {
return map.get(Thread.currentThread());
}
}

Message代码

     
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
     
     
package com.handler;
public class Message {
Handler target;
public Object obj;
public int what;
@Override
public String toString() {
return "what="+what+ " obj="+obj.toString();
}
}

以上就是android Handler机制原理代码了。

android还提供了HandlerThread,其实是对Handler和Thread的封装。

先看一下HandlerThread使用方式

     
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     
     
Handler myHandler;
new HandlerThread( "Compress-Thread") {
@Override
protected void onLooperPrepared() {
super.onLooperPrepared();
myHandler = new Handler();
myHandler.post( new Runnable(){
@Override
public void run() {
//在HandlerThread线程执行
}
});
}
}.start();
//不要在这使用myHandler发送消息,因为myHandler是在onLooperPrepared中创建的,onLooperPrepared又是运行在HandlerThread线程的,所以刚执行到这时HandlerThread线程可能还没有创建完,onLooperPrepared也就不会执行,myHandler自然是null

每次new HandlerThread都会创建一个新线程,当我们使用myHandler发送消息时消息就会在HandlerThread线程执行。HandlerThread线程内部也是一个死循环,通常不会退出,当通过myHandler为其发送消息时就会从阻塞中醒来执行消息,执行完消息队列里的消息后就又阻塞。HandlerThread可以排队执行消息,保证能按加入消息的先后顺序执行。比如我们需要压缩很多图片时,就可以使用HandlerThread,主线程直接把图片通过myHandler发送到HandlerThread,HandlerThread就可以执行图片压缩,所有压缩任务都按添加顺序依次执行。

来自我的博客

http://blog.csdn.net/qingchunweiliang/article/details/50448365

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值