咱们以前使用线程的时候都是使用new Thread来进行线程的建立,可是这样会有一些问题。如:java
相比new Thread,Java提供的四种线程池的好处在于:缓存
java.util.concurrent.Executor 负责线程的使用和调度的根接口 |--ExecutorService 子接口: 线程池的主要接口 |--ThreadPoolExecutor 线程池的实现类 |--ScheduledExceutorService 子接口: 负责线程的调度 |--ScheduledThreadPoolExecutor : 继承ThreadPoolExecutor,实现了ScheduledExecutorService
ExecutorService newFixedThreadPool() : 建立固定大小的线程池
ExecutorService newCachedThreadPool() : 缓存线程池,线程池的数量不固定,能够根据需求自动的更改数量。
ExecutorService newSingleThreadExecutor() : 建立单个线程池。 线程池中只有一个线程
ScheduledExecutorService newScheduledThreadPool() : 建立固定大小的线程,能够延迟或定时的执行任务并发
建立一个可缓存线程池,若是线程池长度超过处理须要,可灵活回收空闲线程,若无可回收,则新建线程。ide
/** * 建立一个可缓存线程池,若是线程池长度超过处理须要,可灵活回收空闲线程,若无可回收,则新建线程。 * 线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。 * * @throws InterruptedException */ public static void cachedThreadPool() throws InterruptedException { ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for(int i = 0; i < 10; i++) { final int index = i; // 加上这一行代码,让前面的线程执行完成,能够看出,一直用的都是同一个线程,没有新建 TimeUnit.SECONDS.sleep(1); cachedThreadPool.execute(() - > { System.out.println(Thread.currentThread() + "====" + index); }); } }
加上TimeUnit.SECONDS.sleep(1);执行结果:
一直复用的是同一个线程工具
Thread[pool-1-thread-1,5,main]====0 Thread[pool-1-thread-1,5,main]====1 Thread[pool-1-thread-1,5,main]====2 Thread[pool-1-thread-1,5,main]====3 Thread[pool-1-thread-1,5,main]====4 Thread[pool-1-thread-1,5,main]====5 Thread[pool-1-thread-1,5,main]====6 Thread[pool-1-thread-1,5,main]====7 Thread[pool-1-thread-1,5,main]====8 Thread[pool-1-thread-1,5,main]====9
去掉TimeUnit.SECONDS.sleep(1);执行结果:
建立了10个不一样的线程性能
Thread[pool-1-thread-1,5,main]====0 Thread[pool-1-thread-2,5,main]====1 Thread[pool-1-thread-3,5,main]====2 Thread[pool-1-thread-4,5,main]====3 Thread[pool-1-thread-5,5,main]====4 Thread[pool-1-thread-6,5,main]====5 Thread[pool-1-thread-7,5,main]====6 Thread[pool-1-thread-9,5,main]====8 Thread[pool-1-thread-8,5,main]====7 Thread[pool-1-thread-10,5,main]====9
建立一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。线程
/** * 建立一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。 * 本例中,由于线程池大小为3,每一个任务输出index后sleep 2秒,因此每两秒打印3个数字。 */ public static void fixedThreadPool() { ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); for(int i = 0; i < 10; i++) { final int index = i; fixedThreadPool.execute(() - > { try { TimeUnit.SECONDS.sleep(2); } catch(InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread() + "====" + index); }); } }
执行结果:
由于线程池大小为3,只会建立3个线程,每一个任务输出index后sleep 2秒,因此每两秒打印3个数字。code
Thread[pool-1-thread-1,5,main]====0 Thread[pool-1-thread-3,5,main]====2 Thread[pool-1-thread-2,5,main]====1 Thread[pool-1-thread-1,5,main]====3 Thread[pool-1-thread-3,5,main]====4 Thread[pool-1-thread-2,5,main]====5 Thread[pool-1-thread-1,5,main]====6 Thread[pool-1-thread-3,5,main]====7 Thread[pool-1-thread-2,5,main]====8 Thread[pool-1-thread-1,5,main]====9
建立一个定长线程池,支持定时及周期性任务执行。对象
/** * 建立一个定长线程池,延迟1秒,每隔1秒周期性任务执行 */ public static void scheduledThreadPool() { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2); scheduledThreadPool.scheduleAtFixedRate(() - > { System.out.println("scheduledThreadPool执行中..."); }, 1, 1, TimeUnit.SECONDS); }
执行结果:继承
scheduledThreadPool执行中... scheduledThreadPool执行中... ...... scheduledThreadPool执行中... scheduledThreadPool执行中... ......
建立一个单线程化的线程池,它只会用惟一的工做线程来执行任务,保证全部任务按照指定顺序(FIFO, LIFO, 优先级)执行。
/** * 建立一个单线程化的线程池,它只会用惟一的工做线程来执行任务,保证全部任务按照指定顺序(FIFO, LIFO, 优先级)执行 */ public static void singleThreadExecutor() { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for(int i = 0; i < 10; i++) { final int index = i; singleThreadExecutor.execute(() - > { System.out.println(Thread.currentThread() + "====" + index); }); } }
执行结果:
只会建立一个线程
Thread[pool-1-thread-1,5,main]====0 Thread[pool-1-thread-1,5,main]====1 Thread[pool-1-thread-1,5,main]====2 Thread[pool-1-thread-1,5,main]====3 Thread[pool-1-thread-1,5,main]====4 Thread[pool-1-thread-1,5,main]====5 Thread[pool-1-thread-1,5,main]====6 Thread[pool-1-thread-1,5,main]====7 Thread[pool-1-thread-1,5,main]====8 Thread[pool-1-thread-1,5,main]====9