Android ThreadUtil 线程公共类,判断是否在主线程/ 子线程执行 相关操作
比如,一个加载网络图片的的方法,需要在子线程中执行。
/** * 加载网络图片 */ 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

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android 视频播放器 VideoView 的使用,播放本地视频 和 网络 视频
1、布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="app.com.myapp...
- 下一篇
Android github 快速实现多人协作
前言:最近要做github多人协作,也就是多人开发。搜索了一些资料,千篇一律,而且操作麻烦。今天就整理一下,github多人协作的简单实现方法。 下面的教程不会出现:公钥、组织、team、pull request 1、首先小张在github上创建一个仓库,比如叫做:GlideDemo 2、 小张开始邀请小王 创建仓库后,然后开始添加 小王了。 注意在第三步的时候,要输入小王的github用户名。 3、小王接收小张的邀请 小王在github登录自己的账户,登录完成后,将在屏幕的右上角看到一个铃铛,双击铃铛。 小王在完成接受邀请后,就可以看到小张的项目仓库了。并且可以push代码到仓库。 4、小张查看所有的协作伙伴。
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2全家桶,快速入门学习开发网站教程
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS8编译安装MySQL8.0.19
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池