面试官:“Handler的runWithScissors()了解吗?为什么Google不让开发者用?“

44 阅读2分钟

接下来,我们看看 runWithScissors() 的实现是不是如我们预想一样。

public final boolean runWithScissors(final Runnable r, long timeout) {

if (r == null) {

throw new IllegalArgumentException("runnable must not be null");

}

if (timeout < 0) {

throw new IllegalArgumentException("timeout must be non-negative");

}

if (Looper.myLooper() == mLooper) {

r.run();

return true;

}

BlockingRunnable br = new BlockingRunnable(r);

return br.postAndWait(this, timeout);

}

可以看到,runWithScissors() 接受一个 Runnable,并且可以设置超时时间。

流程也非常简单:

  1. 先简单的对入参进行校验;

  2. 如果当前线程和 Handler 的处理线程一致,则直接运行 run() 方法;

  3. 线程不一致,则通过 BlockingRunnable 包装一下,并执行其 postAndWait() 方法;

那再继续看看 BlockingRunnable 的源码。

private static final class BlockingRunnable implements Runnable {

private final Runnable mTask;

private boolean mDone;

public BlockingRunnable(Runnable task) {

mTask = task;

}

@Override

public void run() {

try {

// 运行在 Handler 线程

mTask.run();

} finally {

synchronized (this) {

mDone = true;

notifyAll();

}

}

}

public boolean postAndWait(Handler handler, long timeout) {

if (!handler.post(this)) {

return false;

}

synchronized (this) {

if (timeout > 0) {

final long expirationTime = SystemClock.uptimeMillis() + timeout;

while (!mDone) {

long delay = expirationTime - SystemClock.uptimeMillis();

if (delay <= 0) {

return false; // timeout

}

try {

wait(delay);

} catch (InterruptedException ex) {

}

}

} else {

while (!mDone) {

try {

wait();

} catch (InterruptedException ex) {

}

}

}

}

return true;

}

}

待执行的任务,会记入 BlockingRunnable 的 mTask,等待后续被调用执行。

postAndWait() 的逻辑也很简单,先通过 handler 尝试将 BlockingRunnable 发出去,之后进入 Synchronized 临界区,尝试 wait() 阻塞。

如果设置了 timeout,则使用 wait(timeout) 进入阻塞,若被超时唤醒,则直接返回 false,表示任务执行失败。

那么现在可以看到 postAndWait() 返回 false 有 2 个场景:

Handler post() 失败,表示 Looper 出问题了;

等待超时,任务还没有执行结束;

除了超时唤醒外,我们还需要在任务执行完后,唤醒当前线程。

回看 BlockingRunnable 的 run() 方法,run() 被 Handler 调度并在其线程执行。在其中调用 mTask.run(),mTask 即我们需要执行的 Runnable 任务。执行结束后,标记 mDone 并通过 notifyAll() 唤醒等待。

任务发起线程,被唤醒后,会判断 mDone,若为 true 则任务执行完成,直接返回 true 退出。

以上就是 runWithScissors() 的完整执行流程。

2.2 Framework 中的使用

runWithScissors() 被标记为 @hide,应用开发一般是用不上的,但是在 Framework 中,却有不少使用场景。

例如比较熟悉的 WMS 启动流程中,分别在 main() 和 initPolicy() 中,通过 runWithScissors() 切换到 “android.display” 和 “android.ui” 线程去做一些初始工作。

private void initPolicy() {

UiThread.getHandler().runWithScissors(new Runnable() {

public void run() {

// 运行在"android.ui"线程

WindowManagerPolicyThread.set(Thread.currentThread(), Looper.myLooper());

mPolicy.init(mContext, WindowManagerService.this, WindowManagerService.this);

}

}, 0);

}

例如上面代码,就是从 “android.display” 线程,通过切换到 “android.ui” 线程去执行任务。

三、runWithScissors() 的问题


看似 runWithScissors() 通过 Synchronized 的等待通知机制,配合 Handler 发送 Runnable 执行阻塞任务,看似没有问题,但依然被 Android 工程师设为 @hide,肯定有不允许使用的原因。

img img img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取