package threadPool;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class MyThreadPool extends ThreadPoolExecutor {
//使用volatile,禁止指令重排,防止出现半初始化的情况发生
private volatile static MyThreadPool instance = null;
//DCL单例
public static MyThreadPool getInstance(){
if(instance != null){
return instance;
}
synchronized (MyThreadPool.class){
if(instance == null){
//处理器核心数
Integer countOfProcessors = Runtime.getRuntime().availableProcessors();
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(1000);//todo 需要估算
ThreadFactory threadFactory = new MyTreadFactory();
RejectedExecutionHandler handler = new MyIgnorePolicy();
instance = new MyThreadPool(countOfProcessors,500,workQueue,threadFactory,handler);
}
}
return instance;
}
/**
*
* @param corePollSize 核心线程 需要估算
* @param maximumPoolSize 当workQueue满了,核心线程又不够用,则开辟新的线程,但不能超过maximumPoolSize,需要估算
* @param workQueue 核心线程用完时,新任务进入等待队列
* @param threadFactory 产生线程的工厂
* @param handler 当任务队列满时,处理新来的任务的策略
*/
private MyThreadPool(int corePollSize,int maximumPoolSize,BlockingQueue workQueue,ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePollSize, maximumPoolSize, 100L, TimeUnit.MILLISECONDS, workQueue, threadFactory, handler);
}
/**
* 自定义线程工厂
*/
private static class MyTreadFactory implements ThreadFactory {
private final AtomicInteger mThreadNum = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
//定义有意义的线程名称,方便出错时回溯
Thread t = new Thread(r, "myThread" + mThreadNum.getAndIncrement());
System.out.println(t.getName() + " has been created,"+instance.toString());
return t;
}
}
/**
* 自定义拒绝策略
*/
private static class MyIgnorePolicy implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
System.err.println( r.toString() + " rejected,"+e.toString());
}
}
//测试
public static void main(String[] args){
List<Future<String>> futureList = new ArrayList<>();
MyThreadPool myThreadPool = MyThreadPool.getInstance();
System.out.println(myThreadPool);
// ExecutorService m1 = Executors.newFixedThreadPool(1);
// ExecutorService m2 = Executors.newCachedThreadPool();
// ExecutorService m3 = Executors.newSingleThreadExecutor();
// ExecutorService m4 = Executors.newScheduledThreadPool(1);
for(Integer i =0;i<2000;i++){
Future<String> future = myThreadPool.submit(()->{
Thread.sleep(1000);
return "Executor:"+myThreadPool.toString();
});
futureList.add(future);
}
futureList.stream().forEach(f ->{
try {
System.out.println(f.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
}
}
DCL(Double Check Lock)单例
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 单例模式是应用最广的模式之一。相信大家都非常熟悉了,什么,不熟悉?你都单例模式单刷了二十年了,还不懂?好吧,不懂的...
- 单例模式可以使得一个类只有一个对象实例,能够减少频繁创建对象的时间和空间开销。意图:保证一个类仅有一个实例,并提供...
- 摘抄《Android源码设计与实例》 DCL 实现单例: 问题: 关键在于instance = new Singl...
- 单例模式是24中设计模式中的最常用、也是最为简单的一种设计模式。java中实现单例模式的方式,大致分为两种: 1、...
- 一:哪出问题了,单例模式double-check 情景: 二:众(wo)所(gang)周(zhi)知(dao),J...