标签:null get volatile lin 开启 核心 线程池 throws thread
1. ThreadPoolUtils
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ThreadPoolUtils { private static volatile ThreadPoolExecutor pool = null; public static ThreadPoolExecutor getThreadPool() { if (pool == null) { synchronized (ThreadPoolUtils.class) { if (pool == null) { pool = createPool(10, 100); } } } return pool; } /** * 创建线程池 * * @param corePoolSize 核心线程数量 * @param maxThreadSize 最大线程数 * @return */ private static ThreadPoolExecutor createPool(int corePoolSize, int maxThreadSize) { return new ThreadPoolExecutor(corePoolSize, maxThreadSize, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); } }
2. 使用线程池创建线程
ThreadPoolExecutor threadPool = ThreadPoolUtils.getThreadPool(); threadPool.submit(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return sendEmail("保险产品审核表",emails,"未审核的产品",is); } });
3. threadPool.submit()是可以获取返回结果的,call方法里返回的数据,
4. Feature feature = submit.get()
submit.get方法是一个阻塞方法,如果开启的线程内的逻辑没有处理完成,它会等开启的线程处理完成。
标签:null get volatile lin 开启 核心 线程池 throws thread
原文地址:https://www.cnblogs.com/sjzxxy/p/12155883.html