当前位置: 首页 > news >正文

Android Handler的runWithScissors手段

Handler的runWithScissors方法是隐藏API,用于执行同步任务(即让另一个线程执行一个任务,当前线程先阻塞,等待该执行完,之后当前线程再继续执行)。源码如下:

BlockingRunnable代码如下:

ok 源码就这些。 当前线程 wait(阻塞),等待handler所关联的线程执行完任务后唤醒当前线程。可以用于执行同步任务。timeOut为超时时间。

隐藏API不方便直接调用。把这段代码复制出来测试下, 测试代码如下:

package com.example.myapplication;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
public class Test {MyHandler handler;public void testRunWithScissors() {Thread thread1 = new Thread(){@Overridepublic void run() {Log.d("zx", "thread1 start , threadID:" + Thread.currentThread().getId());Looper.prepare();handler = new MyHandler(Looper.myLooper());Looper.loop();}};thread1.start();Thread thread2 = new Thread() {@Overridepublic void run() {Log.d("zx", "thread2 start , threadID:" + Thread.currentThread().getId());// 线程2等待线程1执行一个任务, 执行完该任务后,线程2再做其他事。boolean res = handler.runWithScissors2(new Runnable() {@Overridepublic void run() {// 线程1中会执行这个任务Log.d("zx", "current Thread, threadID:" + Thread.currentThread().getId()+ ", task start ...");for (int i = 0; i < 5; i++) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}Log.i("zx", "i = " + i);}Log.i("zx", "task finish");}}, 1000);Log.d("zx", "task finish, result: " + res + ", thread2 continue ...");}};try {Thread.sleep(10); // 防止线程1还没跑起来handler为空} catch (InterruptedException e) {e.printStackTrace();}thread2.start();}public static class MyHandler extends Handler {public MyHandler(Looper looper) {super(looper);}// 同步执行任务。 意思是,阻塞当前线程,等待另一个线程执行完该任务后,当前线程再继续执行. timeout为超时时间。 返回值表示任务执行是否成功public final boolean runWithScissors2(Runnable r, long timeout) { // 不能和隐藏api的方法同名,所以换了方法名。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() == getLooper()) { // 特殊情况,没有切换线程,就在当前线程执行。r.run();return true;}BlockingRunnable br = new BlockingRunnable(r);return br.postAndWait(this, timeout);}private static final class BlockingRunnable implements Runnable {private final Runnable mTask;private boolean mDone;public BlockingRunnable(Runnable task) {mTask = task;}@Overridepublic void run() {try {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;}}}
}

然后在activity里直接测试:

打印:

ok. 结果符合预期。

http://www.jsqmd.com/news/20650/

相关文章:

  • Idea提高制作效率的快捷键最佳学习方式
  • Elasticsearch8容器化部署 - 实践
  • ski 和 db 模块的通信
  • rocky10自己手动换源
  • 完整教程:ImmuCellAI 免疫浸润分析
  • 4.6.2版本来了!快来看看新版本有哪些改动
  • 2025-10-22 ZR-J 模拟赛 赛后总结【ZR】
  • Deepoc具身智能模型:为传统机器人注入“灵魂”,重塑建筑施工现场安全新范式 - 指南
  • [grep] grep stream 2, the error message
  • P5285 [十二省联考 2019] 骗分过样例
  • Liferay Portal与DXP集合提供程序存在授权缺失漏洞分析
  • MapGIS Objects Java计算一条三维线段与一个三角形所在的平面的交点 - 教程
  • layui时间与日期选择器,时间范围查询数据,后端springboot
  • 读书笔记:OpenPBR 规范(2)
  • 轻量级图片信息解析程序
  • 2025.10.23 闲话-全局位运算 max 的解法
  • express 模块学习 - 东方不败-
  • 习题-无限集与选择公理
  • Error: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试及其解决方法
  • 题解:CF2115F1 Gellyfish and Lycoris Radiata (Easy Version)
  • 项目管理软件是不是伪需求?
  • 2025内窥镜/内窥镜电缆线/B超线厂家推荐明秀电子,专业制造品质可靠
  • 2025低烟无卤/UL3302/UL3767/UL4413辐照线厂家推荐明秀电子,专业认证品质保障
  • 2025.10.23考试记录
  • 低代码如何成为业务与IT的沟通桥梁?破解数字化转型中的协作难题
  • 2025铁氟龙/极细铁氟龙/UL系列高温线厂家推荐明秀电子,专业耐用品质保障!
  • LIS 略解
  • 低代码如何引爆全员创新?揭秘技术民主化背后的蒲公英效应
  • 低代码如何重塑IT部门价值?
  • 2025工业冰水机/冷水机厂家推荐东莞市凯诺机械,高效制冷稳定运行