Android部分-5:Handler

1.子线程一定不能更新UI吗?

  • 子线程更新UI的检测异常是在ViewRootImpl的checkThread方法,而ViewRootImpl是在onResume方法之后创建的,所以在Android的onResume生命周期之前是可以在子线程更新UI。
  • SurfaceView

2.给我说说Handler的原理。

Message(单向链表) MessageQueue(延时,线程的阻塞和唤醒) Looper ThreadLocal

3.Handler导致的内存泄露你是如何解决的?

静态类和弱引用(WeakReference)

4.如何使用Handler让子线程和子线程通信?

HandlerThread 或者 调用Looper.prepare()和Looper.loop()创建子线程的Looper对象;

5.你能给我说说Handler的设计原理?

见Question 2

6.HandlerThread是什么 & 原理 & 使用场景?

子线程之间通信。重写run方法实现创建Looper轮询。(Looper.prepare() 和 Looper.loop())

7.IdleHandler是什么?

消息轮询空闲时间执行的消息处理任务。

1
2
3
4
5
6
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
@Override
public boolean queueIdle() {
return false;
}
});
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
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
return null;
}

int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
...
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf(TAG, "IdleHandler threw exception", t);
}

if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}

// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;

// While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}

8.一个线程能否创建多个Handler,Handler和Looper之间的对应关系?

一个Looper,一个MessageQueue,多个Handler

9.为什么Android系统不建议子线程访问UI?

UI控件不是线程安全的,子线程访问UI会造成不可预期的状态

  • 为什么不使用加锁机制

    加锁会很大程度上降低UI的访问效率。

10.Looper死循环为什么不会导致应用卡死?

Activity的执行过程本就应该在死循环中运行,便于接受生命周期切换的消息。在消息队列为空的情况下,该线程会进入休眠状态,CPU也会释放相应资源。

11.使用Handler的postDealy后消息队列有什么变化?

线程会阻塞设置的延迟时间,在此时间段内如果有新的消息被发出且时间小于上次消息执行时间,消息将会被唤醒。

12.可以在子线程直接new一个Handler出来吗?

可以,但是需要调用Handler带有Looper实例的构造方法。

13.Message对象创建的方式有哪些 & 区别?

Mesage.obtain() 从Messsage对象池中获取。

1
2
3
4
5
6
7
8
9
10
11
12
13
public static Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
}
return new Message();
}

14.ANR和Handler存在什么联系吗?

使用异步任务消息处理机制可以避免ANR异常产生

15.子线程的Looper和主线程的Looper有什么区别?

属于各自线程的Looper循环

16.说说Handler为什么不能进行跨进程通信?

Handler用于可以共享内存的同一进程

17.Handler的消息延时是如何实现的?

线程的阻塞和唤醒

18.什么是消息屏障?查看详情

通过设置同步屏障,可以使Handler优先处理异步任务。

19.假设主线程new了Handler A和Handler B以及Handler C,现在有个子线程,在子线程中通过Handler C发送了一条消息,那么Handler A和Handler B能接收到吗?为什么?

不可以,Message的target参数指定了接受消息的Handler实例

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×