package com.gxhc.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyThreadPool {
private static volatile ExecutorService threadPool;
private static boolean flag = false;
private MyThreadPool(){
}
public static ExecutorService getInstance(){
if(!flag){
if(threadPool != null){
return threadPool;
}else {
synchronized (ExecutorService.class){
if(threadPool == null){
threadPool = Executors.newFixedThreadPool(100);
flag = true;
}
}
}
}
return threadPool;
}
}
public class Test{
public static void main(String[]args){
for(int i=0;i<1000;i++){
ExecutorService pool = MyThreadPool.getInstance();
pool.summit(new Thread(()->{
System.out.printlIn("66666")
}));
}
}
}
以上pool.summit(new Thread(()->{}));方法是调用了线程的summit方法,其方法是没有返回值的情况。当需要线程池返回值时可以参考另一篇转载文章(https://blog.csdn.net/qq_24448899/article/details/79066717)