public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
private ThreadPool(int corepoolsize, int maximumpoolsize, long keepalivetime){ this.corepoolsize = corepoolsize; this.maximumpoolsize = maximumpoolsize; this.keepalivetime = keepalivetime; } public void executor(Runnable runnable){ if (runnable == null){ return; } if (mexecutor == null){ mexecutor = new ThreadPoolExecutor(corepoolsize, //核心线程数 maximumpoolsize, //最大线程数 keepalivetime, //闲置线程存活时间 TimeUnit.MILLISECONDS, // 时间单位 new LinkedBlockingDeque<Runnable>(), //线程队列 Executors.defaultThreadFactory(), //线程工厂 new ThreadPoolExecutor.AbortPolicy() //队列已满,而且当前线程数已经超过最大线程数时的异常处理策略 ); } mexecutor.execute(runnable); }
// 获取单例的线程池对象 public static ThreadPool getThreadPool() { if (mThreadPool == null) { synchronized (ThreadManager.class) { if (mThreadPool == null) { int cpuNum = Runtime.getRuntime().availableProcessors();// 获取处理器数量 int threadNum = cpuNum * 2 1;// 根据cpu数量,计算出合理的线程并发数 mThreadPool = new ThreadPool(threadNum, threadNum, 0L); } } } return mThreadPool; }