New file |
| | |
| | | package com.hdl.sdk.link.common.utils;
|
| | |
|
| | | import android.os.Handler;
|
| | | import android.os.Looper;
|
| | |
|
| | | import java.util.concurrent.ExecutorService;
|
| | | import java.util.concurrent.Executors;
|
| | | import java.util.concurrent.ScheduledExecutorService;
|
| | |
|
| | | /**
|
| | | * Created by Tong on 2021/9/15.
|
| | | */
|
| | | public class ThreadToolUtils {
|
| | |
|
| | | private final Handler uiHandler = new Handler(Looper.getMainLooper());
|
| | |
|
| | | //cpu 最大线程容纳量
|
| | | private final int coreSize = Runtime.getRuntime().availableProcessors() + 1;
|
| | |
|
| | | private ThreadToolUtils() {
|
| | | }
|
| | |
|
| | | private static class SingletonInstance {
|
| | | private static final ThreadToolUtils INSTANCE = new ThreadToolUtils();
|
| | | }
|
| | |
|
| | | public static ThreadToolUtils getInstance() {
|
| | | return SingletonInstance.INSTANCE;
|
| | | }
|
| | |
|
| | |
|
| | | /**
|
| | | * 线程数量固定的线程池
|
| | | */
|
| | | public ExecutorService newFixedThreadPool(int size) {
|
| | | if (size == 0 || coreSize < size) {
|
| | | return Executors.newFixedThreadPool(coreSize);
|
| | | }
|
| | | return Executors.newFixedThreadPool(size);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 定时任务线程池
|
| | | */
|
| | | public ScheduledExecutorService newScheduledThreadPool(int size) {
|
| | | if (size == 0 || coreSize < size) {
|
| | | return Executors.newScheduledThreadPool(coreSize);
|
| | | }
|
| | | return Executors.newScheduledThreadPool(size);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 单一线程
|
| | | */
|
| | | public ExecutorService newSingleThreadPool() {
|
| | | return Executors.newSingleThreadExecutor();
|
| | | }
|
| | |
|
| | |
|
| | | public ExecutorService newCachedThreadPool() {
|
| | | return Executors.newCachedThreadPool();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 切换回主线程
|
| | | */
|
| | | public void runOnUiThread(Runnable run) {
|
| | | uiHandler.post(run);
|
| | | }
|
| | |
|
| | |
|
| | | }
|