线程池的封装和使用(二)
发布日期:2021-05-07 14:11:07 浏览次数:17 分类:技术文章

本文共 4052 字,大约阅读时间需要 13 分钟。

PriorityThreadFactory.java

import java.util.concurrent.ThreadFactory;class PriorityThreadFactory implements ThreadFactory {       private final int mThreadPriority;    public PriorityThreadFactory(int threadPriority) {           mThreadPriority = threadPriority;    }    @Override    public Thread newThread(final Runnable runnable) {           Runnable wrapperRunnable = new Runnable() {               @Override            public void run() {                   try {                       android.os.Process.setThreadPriority(mThreadPriority);                } catch (Throwable t) {                   }                runnable.run();            }        };        return new Thread(wrapperRunnable);    }}

MainThreadExecutor.java

import android.os.Handler;import android.os.Looper;import java.util.concurrent.Executor;class MainThreadExecutor implements Executor {       private final Handler handler = new Handler(Looper.getMainLooper());    @Override    public void execute(Runnable runnable) {           handler.post(runnable);    }}

DefaultExecutorSupplier.java

import java.util.concurrent.Executor;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadFactory;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class DefaultExecutorSupplier {       public static final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();    private final ThreadPoolExecutor mForBackgroundTasks;    private final ThreadPoolExecutor mForLightWeightBackgroundTasks;    private final Executor mMainThreadExecutor;    private static DefaultExecutorSupplier sInstance;    public static DefaultExecutorSupplier getInstance() {           if (sInstance == null) {               synchronized (DefaultExecutorSupplier.class) {                   sInstance = new DefaultExecutorSupplier();            }        }        return sInstance;    }    private DefaultExecutorSupplier() {           ThreadFactory backgroundPriorityThreadFactory = new PriorityThreadFactory(android.os.Process.THREAD_PRIORITY_BACKGROUND);        mForBackgroundTasks = new ThreadPoolExecutor(                NUMBER_OF_CORES * 2,                NUMBER_OF_CORES * 2,                60L,                TimeUnit.SECONDS,                new LinkedBlockingQueue
(), backgroundPriorityThreadFactory ); mForLightWeightBackgroundTasks = new ThreadPoolExecutor( NUMBER_OF_CORES * 2, NUMBER_OF_CORES * 2, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue
(), backgroundPriorityThreadFactory ); mMainThreadExecutor = new MainThreadExecutor(); } public ThreadPoolExecutor forBackgroundTasks() { return mForBackgroundTasks; } public ThreadPoolExecutor forLightWeightBackgroundTasks() { return mForLightWeightBackgroundTasks; } public Executor forMainThreadTasks() { return mMainThreadExecutor; } //demo public void doSomeBackgroundWork() { DefaultExecutorSupplier.getInstance().forBackgroundTasks() .execute(new Runnable() { @Override public void run() { // do some background work here. } }); } //demo public void doSomeLightWeightBackgroundWork() { DefaultExecutorSupplier.getInstance().forLightWeightBackgroundTasks() .execute(new Runnable() { @Override public void run() { // do some light-weight background work here. } }); } //demo public void doSomeMainThreadWork() { DefaultExecutorSupplier.getInstance().forMainThreadTasks() .execute(new Runnable() { @Override public void run() { // do some Main Thread work here. } }); }}
上一篇:Android反射的简单使用
下一篇:Android P按键静音流程

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年03月25日 13时27分05秒