一个实用的线程辅助类

另一篇文章 使用Ntrip协议连接CORS服务器获取差分数据-Java 中使用了这个线程辅助类,有人问到,这里把它放上来方便大家参考。import android.os.Handler;import android.os.Looper;import android.text.TextUtils;import android.util.Log;import androidx.annotation.IntRange;import androidx.annotation.NonNull;impo
摘要由CSDN通过智能技术生成

另一篇文章 使用Ntrip协议连接CORS服务器获取差分数据-Java 中使用了这个线程辅助类,有人问到,这里把它放上来方便大家参考。

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 线程辅助类
 */
public class ThreadUtils {
   
    private static final String TAG = ThreadUtils.class.getSimpleName();
    private static final Handler HANDLER = new Handler(Looper.getMainLooper());
    private static final String DEFAULT_PREFIX_OF_TYPE_SINGLE = "Single";
    private static final String DEFAULT_PREFIX_OF_TYPE_CACHED = "Cache";
    private static final String DEFAULT_PREFIX_OF_TYPE_IO = "Io";
    private static final String DEFAULT_PREFIX_OF_TYPE_CPU = "Cpu";
    private static final String DEFAULT_PREFIX_OF_TYPE_SCHEDULE = "Schedule";
    private static final String DEFAULT_PREFIX_OF_TYPE_FIXED = "Fixed";

    private static final byte TYPE_SINGLE = -1;
    private static final byte TYPE_CACHED = -2;
    private static final byte TYPE_IO = -4;
    private static final byte TYPE_CPU = -8;
    private static final byte TYPE_SCHEDULE = -12;

    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();

    /**
     * @return TYPE_SINGLE
     */
    public static byte getTypeSingle() {
   
        return TYPE_SINGLE;
    }

    /**
     * @return TYPE_CACHED
     */
    public static byte getTypeCached() {
   
        return TYPE_CACHED;
    }

    /**
     * @return TYPE_IO
     */
    public static byte getTypeIo() {
   
        return TYPE_IO;
    }

    /**
     * @return TYPE_CPU
     */
    public static byte getTypeCpu() {
   
        return TYPE_CPU;
    }

    /**
     * @return TYPE_SCHEDULE
     */
    public static byte getTypeSchedule() {
   
        return TYPE_SCHEDULE;
    }

    /**
     * @return CPU_COUNT
     */
    public static int getCpuCount() {
   
        return CPU_COUNT;
    }

    /**
     * Return whether the thread is the main thread.
     *
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isMainThread() {
   
        return Looper.myLooper() == Looper.getMainLooper();
    }

    /**
     * 获取Handler
     *
     * @return Handler
     */
    public static Handler getMainHandler() {
   
        return HANDLER;
    }

    /**
     * 在主线程运行
     *
     * @param runnable Runnable
     */
    public static void runOnUiThread(final Runnable runnable) {
   
        if (runnable == null) {
   
            return;
        }
        if (isMainThread()) {
   
            runnable.run();
        } else {
   
            HANDLER.post(runnable);
        }
    }

    /**
     * 延迟运行
     *
     * @param runnable Runnable
     * @param delay    延迟时间
     * @param timeUnit 时间单位
     */
    public static void runOnUiThreadDelayed(final Runnable runnable, long delay, TimeUnit timeUnit) {
   
        if (runnable == null) {
   
            return;
        }
        HANDLER.postDelayed(runnable, timeUnit.toMillis(delay));
    }

    /**
     * 移除任务
     *
     * @param runnable Runnable
     */
    public static void removeCallbacks(Runnable runnable) {
   
        if (runnable == null) {
   
            return;
        }
        HANDLER.removeCallbacks(runnable);
    }

    /**
     * Return a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue, using the provided
     * ThreadFactory to create new threads when needed.
     *
     * @param size The size of thread in the pool.
     * @return a fixed thread pool
     */
    public static ExecutorService getFixedPool(@IntRange(from = 1) final int size) {
   
        return getFixedPool("", size);
    }

    /**
     * Return a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue, using the provided
     * ThreadFactory to create new threads when needed.
     *
     * @param tag  TAG
     * @param size The size of thread in the pool.
     * @return a fixed thread pool
     */
    public static ExecutorService getFixedPool(String tag, @IntRange(from = 1) final int size) {
   
        return getPoolByTypeAndPriority(tag, size);
    }

    /**
     * Return a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue, using the provided
     * 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值