Android ThreadUtil 线程公共类,判断是否在主线程/ 子线程执行 相关操作
2016-06-15 16:42
507 查看
前言:通常,我们写的公共的模块给别人用,但是这个模块又必须在特定的线程中执行。
比如,一个加载网络图片的的方法,需要在子线程中执行。
但是其他的同事在使用的时候,可能一不小心就在主线程中执行了 loadImage() 方法。这样就势必造成了界面卡顿。
为了避免这种情况,我们需要一个线程判断的工具 ThreadUtil 来帮助我们处理。
当前线程是主线程,抛出异常,不去加载
当前线程是子线程,继续执行,完成加载
然后我们把 loadImage() 修改一下,就成了
可以看到在 loadImage() 方法中多了一句: ThreadUtil.assertBackgroundThread();
在 assertBackgroundThread() 方法里,判断如果不是子线程就直接抛出 "YOu must call this method on a background thread"
正确的调用应该是:在子线程中调用 loadImage() ,比如:
总结:
ThreadUitl 是参考图片加载框架Glide写的 .
ThreadUtil.assertBackgroundThread(); 要求在子线程中执行
ThreadUtil.assertMainThread() ; 要求在主线程运行
代码示例已上传到 github: https://github.com/zyj1609wz/ZUtils
比如,一个加载网络图片的的方法,需要在子线程中执行。
/** * 加载网络图片 */ private void loadImage() { try { //用延时3秒操作来模拟网络操作 Thread.sleep( 3000 ); } catch (InterruptedException e) { e.printStackTrace(); } }
但是其他的同事在使用的时候,可能一不小心就在主线程中执行了 loadImage() 方法。这样就势必造成了界面卡顿。
为了避免这种情况,我们需要一个线程判断的工具 ThreadUtil 来帮助我们处理。
当前线程是主线程,抛出异常,不去加载
当前线程是子线程,继续执行,完成加载
package com.app; import android.os.Looper; /** * Created by ${zyj} on 2016/6/7. */ public class ThreadUtil { /** * Throws an {@link java.lang.IllegalArgumentException} if called on a thread other than the main thread. */ public static void assertMainThread() { if (!isOnMainThread()) { throw new IllegalArgumentException("You must call this method on the main thread"); } } /** * Throws an {@link java.lang.IllegalArgumentException} if called on the main thread. */ public static void assertBackgroundThread() { if (!isOnBackgroundThread()) { throw new IllegalArgumentException("YOu must call this method on a background thread"); } } /** * Returns {@code true} if called on the main thread, {@code false} otherwise. */ public static boolean isOnMainThread() { return Looper.myLooper() == Looper.getMainLooper(); } /** * Returns {@code true} if called on the main thread, {@code false} otherwise. */ public static boolean isOnBackgroundThread() { return !isOnMainThread(); } }
然后我们把 loadImage() 修改一下,就成了
/** * 加载网络图片 */ private void loadImage() { //判断是否在子线程。 子线程:继续执行 主线程:抛出异常 ThreadUtil.assertBackgroundThread(); try { //用延时3秒操作来模拟网络操作 Thread.sleep( 3000 ); } catch (InterruptedException e) { e.printStackTrace(); } }
可以看到在 loadImage() 方法中多了一句: ThreadUtil.assertBackgroundThread();
在 assertBackgroundThread() 方法里,判断如果不是子线程就直接抛出 "YOu must call this method on a background thread"
正确的调用应该是:在子线程中调用 loadImage() ,比如:
new Thread(new Runnable() { @Override public void run() { loadImage(); } }).start();
总结:
ThreadUitl 是参考图片加载框架Glide写的 .
ThreadUtil.assertBackgroundThread(); 要求在子线程中执行
ThreadUtil.assertMainThread() ; 要求在主线程运行
代码示例已上传到 github: https://github.com/zyj1609wz/ZUtils
相关文章推荐
- Ubuntu 16.04 下载和编译android 6 源码
- android更新LOGO后无法显示新LOGO的原因
- 使用TabLayout和ViewPager实现左右滑动选项卡
- Android中dp和px之间进行转换
- android学习6#--自定义View之颜色与角度弧度
- Android之Service(六):使用IntentService
- android sdk下载
- Html5调用android 本地的方法
- android 编辑框获取软键盘后 将底部控件顶上来解决办法
- Android拖动小球跟随手指移动Demo
- Android Studio Gradle 之基础配置与介绍
- Android中的动画
- 四种Activity的启动方式
- Robotium
- Android启动屏全屏显示
- Android 自定义View(二) 自定义属性
- android性能1-内存、CPU获取
- Android滚动选取金额
- Android Annotations
- 重温观察者模式:Android里很常用的模式之一