[Android] 任意时刻从子线程切换到主线程的实现

========================================================
作者:qiujuer
博客:blog.csdn.net/qiujuer
网站:www.qiujuer.net
开源库:Genius-Android

转载请注明出处:http://blog.csdn.net/qiujuer/article/details/41599383
========================================================

引入

在Android开发中常常会遇到网络请求数据库数据准备等一些耗时的操作;而这些操作是不允许在主线程中进行的。因为这样会堵塞主线程导致程序出现未响应情况。

所以只能另起一个子线程进行这些耗时的操作,完成后再显示到界面。众所周知,界面等控件操作只能在主线程中完成;所以不可避免的需要从子线程切换到主线程

方法

对于这样的情况在Android 中比较常见的是使用AsynTask类或者 Handler来进行线程切换;而其中AsynTask是官方封装的类,较为简单,效率也比较可以,但是并不适合所有的情况,至少我使用了一两次后就再也没有使用了。使用 Handler可以说是最万能的方式,其原理是消息循环,在主线程中建立Handler 变量时,就会启动Handler消息循环,一个个的处理消息队列中的任务。但是其也有棘手的时候;其棘手的地方就是麻烦。

每次都需要去建立一个 Handler 类,然后使用voidhandleMessage(Messagemsg) 方法把消息取出来进行界面操作,而其中还要遇到参数的传递等问题,说起来真的是挺麻烦的。

想法

既然有着这么多的问题,但是又有其的优势,我们何不自行封装一次呢?

这里我梳理一下思路:

  1. 还是使用 Handler进行线程切换
  2. 在子线程中能通过简单的调用就切换到主线程进行工作
  3. 在子线程切换到主线程时,子线程进入阻塞直到主线程执行完成(知道为什么有这样的需求么?)
  4. 一定要保证其效率
  5. 主线程的执行要有时间限制,不能执行太长时间导致主线程阻塞

我能想到的就是这些;观众老爷们咋样?可否还有需求?

说干就干,梳理一下实现方法

  • 使用Handler 实现,既然这样那么主方法当然就是采用继承Handler 来实现
  • 而要简单同时又要能随时进入方法 那么对外采用静态方法是个不错的选择
  • 而要保证效率的话,那就不能让Handler 的消息队列过于太多,但是又要满足能随时调用,那么采用外部 Queue
  • 更具情况有阻塞与不阻塞子线程两种情况,那么采用两个 Queue吧,分开来好一点
  • 要保证不能长时间在主线程执行那么对于队列的执行一定要有时间限制加一个时间变量吧
  • 当然最后考虑了一下,既然要简单那么传入参数采用Runnable 是很爽的

万事俱备,只欠东风了;好了进入下一环节。

CodeTime

首先我们建立一个ToolKit类:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class ToolKit {  
  2.     /** 
  3.      * Asynchronously 
  4.      * 
  5.      * @param runnable Runnable Interface 
  6.      */  
  7.     public static void runOnMainThreadAsync(Runnable runnable) {  
  8.     }  
  9.   
  10.     /** 
  11.      * Synchronously 
  12.      * 
  13.      * @param runnable Runnable Interface 
  14.      */  
  15.     public static void runOnMainThreadSync(Runnable runnable) {  
  16.     }  
  17. }  

两个对外的方法简单来说就是这样了;但是其功能实现就需要使用继承Handler了。

建立类HandlerPoster,继承自Handler:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. final class HandlerPoster extends Handler {  
  2.     private final int ASYNC = 0x1;  
  3.     private final int SYNC = 0x2;  
  4.     private final Queue<Runnable> asyncPool;  
  5.     private final Queue<SyncPost> syncPool;  
  6.     private final int maxMillisInsideHandleMessage;  
  7.     private boolean asyncActive;  
  8.     private boolean syncActive;  
  9.   
  10.     HandlerPoster(Looper looper, int maxMillisInsideHandleMessage) {  
  11.         super(looper);  
  12.         this.maxMillisInsideHandleMessage = maxMillisInsideHandleMessage;  
  13.         asyncPool = new LinkedList<>();  
  14.         syncPool = new LinkedList<>();  
  15.     }  
  16.   
  17.     void dispose() {  
  18.         this.removeCallbacksAndMessages(null);  
  19.         this.asyncPool.clear();  
  20.         this.syncPool.clear();  
  21.     }  
  22.   
  23.     void async(Runnable runnable) {  
  24.         synchronized (asyncPool) {  
  25.             asyncPool.offer(runnable);  
  26.             if (!asyncActive) {  
  27.                 asyncActive = true;  
  28.                 if (!sendMessage(obtainMessage(ASYNC))) {  
  29.                     throw new GeniusException("Could not send handler message");  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.     void sync(SyncPost post) {  
  36.         synchronized (syncPool) {  
  37.             syncPool.offer(post);  
  38.             if (!syncActive) {  
  39.                 syncActive = true;  
  40.                 if (!sendMessage(obtainMessage(SYNC))) {  
  41.                     throw new GeniusException("Could not send handler message");  
  42.                 }  
  43.             }  
  44.         }  
  45.     }  
  46.   
  47.     @Override  
  48.     public void handleMessage(Message msg) {  
  49.         if (msg.what == ASYNC) {  
  50.             boolean rescheduled = false;  
  51.             try {  
  52.                 long started = SystemClock.uptimeMillis();  
  53.                 while (true) {  
  54.                     Runnable runnable = asyncPool.poll();  
  55.                     if (runnable == null) {  
  56.                         synchronized (asyncPool) {  
  57.                             // Check again, this time in synchronized  
  58.                             runnable = asyncPool.poll();  
  59.                             if (runnable == null) {  
  60.                                 asyncActive = false;  
  61.                                 return;  
  62.                             }  
  63.                         }  
  64.                     }  
  65.                     runnable.run();  
  66.                     long timeInMethod = SystemClock.uptimeMillis() - started;  
  67.                     if (timeInMethod >= maxMillisInsideHandleMessage) {  
  68.                         if (!sendMessage(obtainMessage(ASYNC))) {  
  69.                             throw new GeniusException("Could not send handler message");  
  70.                         }  
  71.                         rescheduled = true;  
  72.                         return;  
  73.                     }  
  74.                 }  
  75.             } finally {  
  76.                 asyncActive = rescheduled;  
  77.             }  
  78.         } else if (msg.what == SYNC) {  
  79.             boolean rescheduled = false;  
  80.             try {  
  81.                 long started = SystemClock.uptimeMillis();  
  82.                 while (true) {  
  83.                     SyncPost post = syncPool.poll();  
  84.                     if (post == null) {  
  85.                         synchronized (syncPool) {  
  86.                             // Check again, this time in synchronized  
  87.                             post = syncPool.poll();  
  88.                             if (post == null) {  
  89.                                 syncActive = false;  
  90.                                 return;  
  91.                             }  
  92.                         }  
  93.                     }  
  94.                     post.run();  
  95.                     long timeInMethod = SystemClock.uptimeMillis() - started;  
  96.                     if (timeInMethod >= maxMillisInsideHandleMessage) {  
  97.                         if (!sendMessage(obtainMessage(SYNC))) {  
  98.                             throw new GeniusException("Could not send handler message");  
  99.                         }  
  100.                         rescheduled = true;  
  101.                         return;  
  102.                     }  
  103.                 }  
  104.             } finally {  
  105.                 syncActive = rescheduled;  
  106.             }  
  107.         } else super.handleMessage(msg);  
  108.     }  
  109. }  

下面来说说这个我花了很大时间弄出来的类。

类的变量部分:

两个标识,两个队列,两个执行状态,一个时间限制;很好理解吧?标识为了区别分别是处理那个队列使用;队列当然是装着任务了;执行状态是为了避免重复发送消息导致消息队列过多;时间限制这个最好理解了。

下面来说说方法部分:

构造函数HandlerPoster(Looper_looper,int_maxMillisInsideHandleMessage)

传入两个参数,分别是 Looper,用于初始化到主线程,后面的是时间限制;然后初始化了两个队列。

销毁函数void_dispose()首先去除掉没有处理的消息,然后清空队列。

添加异步执行方法void_async(Runnable_runnable):

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void async(Runnable runnable) {  
  2.     synchronized (asyncPool) {  
  3.         asyncPool.offer(runnable);  
  4.         if (!asyncActive) {  
  5.             asyncActive = true;  
  6.             if (!sendMessage(obtainMessage(ASYNC))) {  
  7.                 throw new GeniusException("Could not send handler message");  
  8.             }  
  9.         }  
  10.     }  
  11. }  

可以看见进入方法后第一件事儿就是进入同步状态,然后调用asyncPool.offer(runnable);把任务写入到队列。

之后判断当前是否处于异步任务执行中,如果不是:立刻改变状态,然后发送一个消息给当前Handler,当然不要忘记了传入标识。

当然为了效率其消息的构造也是通过obtainMessage(ASYNC)方法来完成,为的就是不过多建立新的Message,尽量使用当前队列中空闲的消息。

添加同步执行方法void_sync(SyncPost_post)

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void sync(SyncPost post) {  
  2.     synchronized (syncPool) {  
  3.         syncPool.offer(post);  
  4.         if (!syncActive) {  
  5.             syncActive = true;  
  6.             if (!sendMessage(obtainMessage(SYNC))) {  
  7.                 throw new GeniusException("Could not send handler message");  
  8.             }  
  9.         }  
  10.     }  
  11. }  

可以看到,这里传入的并不是Runnable 而是SyncPost这是为了同步而对Runnable进行了一次封装后的类;后面介绍。

同样是进入同步,添加,判断,发送消息。

任务执行者@Override_void_handleMessage(Message_msg):

这里是复写的Handler的消息处理方法,当当前Handler消息队列中有消息的时候将会按照顺序一个个的调用该方法。

分段来看:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. if (msg.what == ASYNC) {  
  2.     boolean rescheduled = false;  
  3.     try {  
  4.         long started = SystemClock.uptimeMillis();  
  5.         while (true) {  
  6.             Runnable runnable = asyncPool.poll();  
  7.             if (runnable == null) {  
  8.                 synchronized (asyncPool) {  
  9.                     // Check again, this time in synchronized  
  10.                     runnable = asyncPool.poll();  
  11.                     if (runnable == null) {  
  12.                         asyncActive = false;  
  13.                         return;  
  14.                     }  
  15.                 }  
  16.             }  
  17.             runnable.run();  
  18.             long timeInMethod = SystemClock.uptimeMillis() - started;  
  19.             if (timeInMethod >= maxMillisInsideHandleMessage) {  
  20.                 if (!sendMessage(obtainMessage(ASYNC))) {  
  21.                     throw new GeniusException("Could not send handler message");  
  22.                 }  
  23.                 rescheduled = true;  
  24.                 return;  
  25.             }  
  26.         }  
  27.     } finally {  
  28.         asyncActive = rescheduled;  
  29.     }  
  30. }  

进入后首先判断是否是进行异步处理的消息,如果是那么进入该位置。

进入后我们进行了try_finally有一个变量long_started用于标识开始时间。

当执行一个任务后就判断一次如果超过了每次占用主线程的时间限制,那么不管队列中的任务是否执行完成都退出,同时发起一个新的消息到Handler循环队列。

while部分,我们从队列取出一个任务,采用Poll方法;判断是否为空,如果为空进入队列同步块;然后再取一次,再次判断。

如果恰巧在进入同步队列之前有新的任务来了,那么第二次取到的当然就不是 NULL也就会继续执行下去。反之,如果还是为空;那么重置当前队列的状态为false同时跳出循环。

下面来看第二部分:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. else if (msg.what == SYNC) {  
  2.             boolean rescheduled = false;  
  3.             try {  
  4.                 long started = SystemClock.uptimeMillis();  
  5.                 while (true) {  
  6.                     SyncPost post = syncPool.poll();  
  7.                     if (post == null) {  
  8.                         synchronized (syncPool) {  
  9.                             // Check again, this time in synchronized  
  10.                             post = syncPool.poll();  
  11.                             if (post == null) {  
  12.                                 syncActive = false;  
  13.                                 return;  
  14.                             }  
  15.                         }  
  16.                     }  
  17.                     post.run();  
  18.                     long timeInMethod = SystemClock.uptimeMillis() - started;  
  19.                     if (timeInMethod >= maxMillisInsideHandleMessage) {  
  20.                         if (!sendMessage(obtainMessage(SYNC))) {  
  21.                             throw new GeniusException("Could not send handler message");  
  22.                         }  
  23.                         rescheduled = true;  
  24.                         return;  
  25.                     }  
  26.                 }  
  27.             } finally {  
  28.                 syncActive = rescheduled;  
  29.             }  
  30.         } else super.handleMessage(msg);  

首先还是判断,如果是同步任务消息就进入,如果还是不是 那么只有调用super.handleMessage(msg);了。

从上面的处理部分可以看出来其处理的过程与第一部分可以说是完全一样的。

只不过是从不同队列取出不同的类SyncPost,然后判断执行,以及发送不同标识的消息;可以说如果懂了第一部分,这部分是毫无营养的。

这里就有问题了,既然方法操作流程一样,那么同步与异步是在哪里进行区分的?

这里就要看看SyncPost了:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. final class SyncPost {  
  2.     boolean end = false;  
  3.     Runnable runnable;  
  4.   
  5.     SyncPost(Runnable runnable) {  
  6.         this.runnable = runnable;  
  7.     }  
  8.   
  9.     public void run() {  
  10.         synchronized (this) {  
  11.             runnable.run();  
  12.             end = true;  
  13.             try {  
  14.                 this.notifyAll();  
  15.             } catch (Exception e) {  
  16.                 e.printStackTrace();  
  17.             }  
  18.         }  
  19.     }  
  20.   
  21.     public void waitRun() {  
  22.         if (!end) {  
  23.             synchronized (this) {  
  24.                 if (!end) {  
  25.                     try {  
  26.                         this.wait();  
  27.                     } catch (InterruptedException e) {  
  28.                         e.printStackTrace();  
  29.                     }  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34. }  

首先看看SyncPost的构造函数:

是不是传入一个Runnable接口?所以说是对Runnable 的简单封装。

可以看见其public_void_run()方法:

在该方法中我们进入了同步块,然后调用Runnable接口的run方法。同时在执行完成后将其中的一个状态变量进行了改变boolean_end=true;

然后调用this.notifyAll();通知等待的部分可以继续了,当然有这样的情况;假如在进入该同步块的时候子线程还未执行到this.wait();部分呢?所以我们为此准备了endtry

然后看看public_void_waitRun()方法:

在这个中,我们首先判断状态,如果状态已经变了,那么证明子线程执行到此处时,主线程以及执行了void_run()。

所以也就不用进入同步块进行等待了,不然那还不等死啊?反之就进入进行等待直到主线程调用this.notifyAll();


激情部分

马上进入到完成部分了,组建都完善了那么该进行最后的组装了。

回到类classToolKit

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class ToolKit {  
  2.     private static HandlerPoster mainPoster = null;  
  3.   
  4.     private static HandlerPoster getMainPoster() {  
  5.         if (mainPoster == null) {  
  6.             synchronized (ToolKit.class) {  
  7.                 if (mainPoster == null) {  
  8.                     mainPoster = new HandlerPoster(Looper.getMainLooper(), 20);  
  9.                 }  
  10.             }  
  11.         }  
  12.         return mainPoster;  
  13.     }  
  14.   
  15.     /** 
  16.      * Asynchronously 
  17.      * The child thread asynchronous run relative to the main thread, 
  18.      * not blocking the child thread 
  19.      * 
  20.      * @param runnable Runnable Interface 
  21.      */  
  22.     public static void runOnMainThreadAsync(Runnable runnable) {  
  23.         if (Looper.myLooper() == Looper.getMainLooper()) {  
  24.             runnable.run();  
  25.             return;  
  26.         }  
  27.         getMainPoster().async(runnable);  
  28.     }  
  29.   
  30.     /** 
  31.      * Synchronously 
  32.      * The child thread relative thread synchronization operation, 
  33.      * blocking the child thread, 
  34.      * thread for the main thread to complete 
  35.      * 
  36.      * @param runnable Runnable Interface 
  37.      */  
  38.     public static void runOnMainThreadSync(Runnable runnable) {  
  39.         if (Looper.myLooper() == Looper.getMainLooper()) {  
  40.             runnable.run();  
  41.             return;  
  42.         }  
  43.         SyncPost poster = new SyncPost(runnable);  
  44.         getMainPoster().sync(poster);  
  45.         poster.waitRun();  
  46.     }  
  47.   
  48.     public static void dispose() {  
  49.         if (mainPoster != null) {  
  50.             mainPoster.dispose();  
  51.             mainPoster = null;  
  52.         }  
  53.     }  
  54. }  

其中就一个静态变量HandlerPoster

然后一个初始化部分HandlerPoster_getMainPoster()这里采用同步的方式进行初始化,用于适应多线程同时调用情况;当然在初始化的时候我们传入了

mainPoster=newHandlerPoster(Looper.getMainLooper(),20); 这里就决定了是在主线程执行的HandlerPoster,同时指定主线程单次运行时间为20毫秒。

在方法void_runOnMainThreadAsync(Runnable_runnable)中:

首先判断调用该方法的是否是主线程,如果是那还弄到队列中执行干嘛?直接执行啊;如果是子线程就调用getMainPoster().async(runnable);追加到队列中执行。

而在方法void_runOnMainThreadSync(Runnable_runnable)中:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static void runOnMainThreadSync(Runnable runnable) {  
  2.     if (Looper.myLooper() == Looper.getMainLooper()) {  
  3.         runnable.run();  
  4.         return;  
  5.     }  
  6.     SyncPost poster = new SyncPost(runnable);  
  7.     getMainPoster().sync(poster);  
  8.     poster.waitRun();  
  9. }  

同样是线程判断,然后进行封装,然后丢进队列中等待执行,而在该方法中调用poster.waitRun();进行等待;直到主线程执行了SyncPost类的run方法。
最后当然留下了一个销毁方法;妈妈说要学会清理不留垃圾:void_dispose()

OK,完成了

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // "Runnable" 类实现其中 "run()" 方法  
  2. // "run()" 运行在主线程中,可在其中进行界面操作  
  3. // 同步进入主线程,等待主线程处理完成后继续执行子线程  
  4. ToolKit.runOnMainThreadSync(Runnable runnable);  
  5. // 异步进入主线程,无需等待  
  6. ToolKit.runOnMainThreadAsync(Runnable runnable);  

对外就是这么两个方法,简单便捷啊;大伙试试吧;一个字

代码:代码变更,所以名称变了

UIKit.java
UIKitHandlerPoster.java
UIKitSyncPost.java



开源项目:

Genius-Android

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值