安卓开发:线程管理工具类

/**
 * author:tachechen
 * data:2023/11/24
 */
public class ThreadManager {
    //设备cpu核数
    private final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();
    //线程池最大线程数
    private final int MAXIMUM_POOL_SIZE = 16;
    //空闲线程存活时间
    private static final int KEEP_ALIVE_SECONDS = 3;
    private final Handler mUiHandler;
    private final Executor ioExecutor;
    private final Executor cpuExecutor;
    private final Executor miscExecutor;

    private static class Holder {
        public static ThreadManager instance = new ThreadManager();
    }

    public static ThreadManager getInstance() {
        return Holder.instance;
    }

    private ThreadManager() {
        mUiHandler = new Handler(Looper.getMainLooper());
        ioExecutor = new ThreadPoolExecutor(MAXIMUM_POOL_SIZE,MAXIMUM_POOL_SIZE,KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(),ioThreadFactory);
        cpuExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,CORE_POOL_SIZE,KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(),cpuThreadFactory);
        miscExecutor = Executors.newFixedThreadPool(3,miscThreadFactory);
    }

    //io型线程设置
    private static final ThreadFactory ioThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r,"IO #" +mCount.getAndIncrement());
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            return thread;
        }
    };

    //cpu计算型线程设置
    private static final ThreadFactory cpuThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r,"CPU #" +mCount.getAndIncrement());
            Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
            return thread;
        }
    };

    //io与cpu混合型
    private static final ThreadFactory miscThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r,"MISC #" +mCount.getAndIncrement());
            Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
            return thread;
        }
    };

    /**
     * 切换到主线程
     */
    public void runOnUiThread(Runnable runnable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            runnable.run();
        } else {
            mUiHandler.post(runnable);
        }
    }

    /**
     * 开启io子线程
     */
    public void startIoExecutor(Runnable runnable) {
        ioExecutor.execute(runnable);
    }

    /**
     * 开启cpu计算子线程
     */
    public void startCpuExecutor(Runnable runnable) {
        cpuExecutor.execute(runnable);
    }

    /**
     * 开启混合操作子线程
     */
    public void startMiscExecutor(Runnable runnable) {
        miscExecutor.execute(runnable);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值