星空网 > 软件开发 > 操作系统

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

 

     

 




原标题:Android ThreadUtil 线程公共类,判断是否在主线程/ 子线程执行 相关操作

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

非洲海运整箱:https://www.goluckyvip.com/tag/97370.html
波兰到海运:https://www.goluckyvip.com/tag/97371.html
海运到摩洛哥:https://www.goluckyvip.com/tag/97372.html
至法国海运:https://www.goluckyvip.com/tag/97373.html
海运费卡拉奇:https://www.goluckyvip.com/tag/97374.html
到越南海运:https://www.goluckyvip.com/tag/97375.html
洛阳市涧西区有啥好玩的地方 洛阳涧西区附近景点:https://www.vstour.cn/a/408256.html
九月初新疆旅游服装搭配(新疆游玩必备衣服清单):https://www.vstour.cn/a/408257.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流