一. 线程池

我们之前使用线程的时候都是使用new Thread来进行线程的创建,但是这样会有一些问题。如:

a. 每次new Thread新建对象性能差。b. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或oom。c. 缺乏更多功能,如定时执行、定期执行、线程中断。

相比new Thread,Java提供的四种线程池的好处在于:

a. 重用存在的线程,减少对象创建、消亡的开销,性能佳。b. 可有效控制最大并发线程数,提高系统资源的使用率,同时避免过多资源竞争,避免堵塞。c. 提供定时执行、定期执行、单线程、并发数控制等功能。二. 线程池的体系结构

java.util.concurrent.Executor 负责线程的使用和调度的根接口 |--ExecutorService 子接口: 线程池的主要接口 |--ThreadPoolExecutor 线程池的实现类 |--ScheduledExceutorService 子接口: 负责线程的调度 |--ScheduledThreadPoolExecutor : 继承ThreadPoolExecutor,实现了ScheduledExecutorService三. 工具类:Executors

ExecutorService newFixedThreadPool() : 创建固定大小的线程池
ExecutorService newCachedThreadPool() : 缓存线程池,线程池的数量不固定,可以根据需求自动的更改数量。
ExecutorService newSingleThreadExecutor() : 创建单个线程池。 线程池中只有一个线程
ScheduledExecutorService newScheduledThreadPool() : 创建固定大小的线程,可以延迟或定时的执行任务

四. 举例1. newCachedThreadPool

创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。

/** * 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。 * 线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。 * * @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]====0Thread[pool-1-thread-1,5,main]====1Thread[pool-1-thread-1,5,main]====2Thread[pool-1-thread-1,5,main]====3Thread[pool-1-thread-1,5,main]====4Thread[pool-1-thread-1,5,main]====5Thread[pool-1-thread-1,5,main]====6Thread[pool-1-thread-1,5,main]====7Thread[pool-1-thread-1,5,main]====8Thread[pool-1-thread-1,5,main]====9

去掉TimeUnit.SECONDS.sleep(1);执行结果:
创建了10个不同的线程

Thread[pool-1-thread-1,5,main]====0Thread[pool-1-thread-2,5,main]====1Thread[pool-1-thread-3,5,main]====2Thread[pool-1-thread-4,5,main]====3Thread[pool-1-thread-5,5,main]====4Thread[pool-1-thread-6,5,main]====5Thread[pool-1-thread-7,5,main]====6Thread[pool-1-thread-9,5,main]====8Thread[pool-1-thread-8,5,main]====7Thread[pool-1-thread-10,5,main]====92. newFixedThreadPool

创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。

/** * 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。 * 本例中,因为线程池大小为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个数字。

Thread[pool-1-thread-1,5,main]====0Thread[pool-1-thread-3,5,main]====2Thread[pool-1-thread-2,5,main]====1Thread[pool-1-thread-1,5,main]====3Thread[pool-1-thread-3,5,main]====4Thread[pool-1-thread-2,5,main]====5Thread[pool-1-thread-1,5,main]====6Thread[pool-1-thread-3,5,main]====7Thread[pool-1-thread-2,5,main]====8Thread[pool-1-thread-1,5,main]====93. newScheduledThreadPool

创建一个定长线程池,支持定时及周期性任务执行。

/** * 创建一个定长线程池,延迟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执行中.........4. newSingleThreadExecutor

创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(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]====0Thread[pool-1-thread-1,5,main]====1Thread[pool-1-thread-1,5,main]====2Thread[pool-1-thread-1,5,main]====3Thread[pool-1-thread-1,5,main]====4Thread[pool-1-thread-1,5,main]====5Thread[pool-1-thread-1,5,main]====6Thread[pool-1-thread-1,5,main]====7Thread[pool-1-thread-1,5,main]====8Thread[pool-1-thread-1,5,main]====9