一、前言
根据不同应用场景java.util.concurrent.Executors提供场景使用多线程使用方式,分别是newFixedThreadPool固定线程池大小、newSingleThreadExecutor单线程池、newCachedThreadPool无上限缓存线程池、newScheduledThreadPool定时执行线程池,这四种都是基于java.util.concurrent.ThreadPoolExecutor实现的,内部原理都是一样,具体说明如下:
1.基于同样的线程执行状态(RUNNING、SHUTDOWN、STOP、TERMINATED)@b@2.同样的线程池工作队列workQueue(ArrayBlockingQueue、LinkedBlockingQueue、SynchronousQueue)@b@3.基本池大小控制属性(corePoolSize-默认创建线程数大小、maximumPoolSize-最大线程大小、poolSize-当前线程池大小)@b@4.同样的时效时间控制keepAliveTime(线程没有任务执行时最多保持多久时间会终止)、@b@ unit(@b@ TimeUnit.DAYS-天、@b@ TimeUnit.HOURS-小时、@b@ TimeUnit.MINUTES-分钟、@b@ TimeUnit.SECONDS-秒、@b@ TimeUnit.MILLISECONDS-毫秒、@b@ TimeUnit.MICROSECONDS-微妙、@b@ TimeUnit.NANOSECONDS-纳秒@b@ )
Executors构成newFixedThreadPool方法源码部分
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {@b@ return new ThreadPoolExecutor(nThreads, nThreads,@b@ 0L, TimeUnit.MILLISECONDS,@b@ new LinkedBlockingQueue<Runnable>(),@b@ threadFactory);@b@ }
Executors构成newSingleThreadExecutor方法源码部分
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {@b@ return new FinalizableDelegatedExecutorService@b@ (new ThreadPoolExecutor(1, 1,@b@ 0L, TimeUnit.MILLISECONDS,@b@ new LinkedBlockingQueue<Runnable>(),@b@ threadFactory));@b@ }
Executors构成newCachedThreadPool方法源码部分
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {@b@ return new ThreadPoolExecutor(0, Integer.MAX_VALUE,@b@ 60L, TimeUnit.SECONDS,@b@ new SynchronousQueue<Runnable>(),@b@ threadFactory);@b@ }
Executors构成newScheduledThreadPool方法源码部分
public static ScheduledExecutorService newScheduledThreadPool(@b@ int corePoolSize, ThreadFactory threadFactory) {@b@ return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);@b@ }
二、示例说明
1.newFixedThreadPool-可接受设定线程数量并发执行线程任务,超出部分存入workQueue队列,再依次按照并发数量执行,如下
package test;@b@import java.util.concurrent.ExecutorService; @b@import java.util.concurrent.Executors;@b@@b@public class FixedThreadPoolTest {@b@ @b@ public static void main(String[] args) { @b@ @b@ ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4); @b@ @b@ for (int i = 0; i < 20; i++) { @b@ final int index = i; @b@ fixedThreadPool.execute(new Runnable() { @b@ public void run() { @b@ try { @b@ System.out.println(index); @b@ Thread.sleep(2000); @b@ } catch (InterruptedException e) { @b@ e.printStackTrace(); @b@ } @b@ } @b@ }); @b@ } @b@ } @b@@b@}
控制台没2s打印4个数字、5次执行完成
2.newSingleThreadExecutor-单线程执行任务,超出部分存入workQueue队列,按照指定顺序(FIFO, LIFO, 优先级)执行
package test;@b@@b@import java.util.concurrent.ExecutorService; @b@import java.util.concurrent.Executors; @b@@b@public class SingleThreadExecutorTest {@b@@b@ public static void main(String[] args) { @b@ @b@ ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); @b@ for (int i = 0; i < 20; i++) { @b@ final int index = i; @b@ singleThreadExecutor.execute(new Runnable() { @b@ public void run() { @b@ try { @b@ System.out.println(index); @b@ Thread.sleep(2000); @b@ } catch (InterruptedException e) { @b@ e.printStackTrace(); @b@ } @b@ } @b@ }); @b@ } @b@ @b@ } @b@ @b@}
3.newCachedThreadPool-无上限(Integer.MAX_VALUE)线程池并发执行,空闲线程根据缓存线程机制自动回收,无空闲的情况再创建线程执行
package test;@b@@b@import java.util.concurrent.ExecutorService; @b@import java.util.concurrent.Executors; @b@@b@public class CachedThreadPoolTest {@b@ @b@ public static void main(String[] args) { @b@ ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); @b@ @b@ for (int i = 0; i < 20; i++) { @b@ @b@ final int index = i; @b@ try { @b@ Thread.sleep(index * 1000); @b@ } catch (InterruptedException e) { @b@ e.printStackTrace(); @b@ } @b@ @b@ cachedThreadPool.execute(new Runnable() { @b@ public void run() { @b@ System.out.println(Thread.currentThread().getId()+"_"+index); @b@ } @b@ }); @b@ } @b@ @b@ } @b@@b@}
控制台在等他有效时间内保证一个线程执行完成,因此打印的都是同一个线程再执行,如果没执行完就打印新的线程再执行任务
4.newScheduledThreadPool -创建指定线程池线程池,定期执行任务(详细的使用说明可参考其他文章)