package com.geolo.android.tea.system;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**线程池工具
* @author geolo*/
public class MyThreadPool extends ThreadGroup{
private static MyThreadPool myThreadPool;
private ThreadPoolExecutor mThreadPoolExecutor;
private static int number = 0;
public MyThreadPool(String name) {
this(Thread.currentThread().getThreadGroup(), name);
}
public MyThreadPool(ThreadGroup parent, String name) {
super(parent, name);
mThreadPoolExecutor = new ThreadPoolExecutor(2, 4, 3,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.DiscardOldestPolicy());
}
public static MyThreadPool getInstance(){
if(myThreadPool==null){
number ++;
myThreadPool = new MyThreadPool(number+"");
}
return myThreadPool;
}
public void Executor(Runnable runnable){
//mThreadPoolExecutor.execute(runnable);
mThreadPoolExecutor.execute(runnable);
}
}
我自己通用的线程池
最新推荐文章于 2025-11-30 18:24:05 发布
本文介绍了一种自定义线程池的实现方法,通过继承ThreadGroup并利用ThreadPoolExecutor进行任务调度。该线程池支持最大线程数、核心线程数及任务队列大小的配置,并采用丢弃最旧任务的拒绝策略。
5万+

被折叠的 条评论
为什么被折叠?



