另一篇文章 使用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
*