1 池化技术之线程池什么是池化技术?简单来说就是优化资源的使用 , 我准备好了一些资源 , 有人要用就到我这里拿 , 用完了就还给我 。而一个比较重要的的实现就是线程池 。那么线程池用到了池化技术有什么好处呢?
- 降低资源的消耗
- 提高响应的速度
- 方便管理
2 线程池的五种实现方式其实线程池我更愿意说成四种封装实现方式 , 一种原始实现方式 。这四种封装的实现方式都是依赖于最原始的的实现方式 。所以这里我们先介绍四种封装的实现方式
2.1 newSingleThreadExecutor()这个线程池很有意思 , 说是线程池 , 但是池子里面只有一条线程 。如果线程因为异常而停止 , 会自动新建一个线程补充 。我们可以测试一下:我们对线程池执行十条打印任务 , 可以发现它们用的都是同一条线程
public static void test01() {ExecutorService threadPool = Executors.newSingleThreadExecutor();try {//对线程进行执行十条打印任务for(int i = 1; i <= 10; i++){threadPool.execute(()->{System.out.println(Thread.currentThread().getName()+"=>执行完毕!");});}} catch (Exception e) {e.printStackTrace();} finally {//用完线程池一定要记得关闭threadPool.shutdown();}}
pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!
2.2 newFixedThreadPool(指定线程数量)这个线程池是可以指定我们的线程池大小的 , 可以针对我们具体的业务和情况来分配大小 。它是创建一个核心线程数跟最大线程数相同的线程池 , 因此池中的线程数量既不会增加也不会变少 , 如果有空闲线程任务就会被执行 , 如果没有就放入任务队列 , 等待空闲线程 。我们同样来测试一下:public static void test02() {ExecutorService threadPool = Executors.newFixedThreadPool(5);try {//对线程进行执行十条打印任务for(int i = 1; i <= 10; i++){threadPool.execute(()->{System.out.println(Thread.currentThread().getName()+"=>执行完毕!");});}} catch (Exception e) {e.printStackTrace();} finally {//用完线程池一定要记得关闭threadPool.shutdown();}}
我们创建了五条线程的线程池 , 在打印任务的时候 , 可以发现线程都有进行工作pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-1=>执行完毕!pool-1-thread-2=>执行完毕!pool-1-thread-3=>执行完毕!pool-1-thread-5=>执行完毕!pool-1-thread-4=>执行完毕!
2.3 newCachedThreadPool()这个线程池是创建一个核心线程数为0 , 最大线程为Inter.MAX_VALUE的线程池 , 也就是说没有限制,线程池中的线程数量不确定 , 但如果有空闲线程可以复用 , 则优先使用 , 如果没有空闲线程 , 则创建新线程处理任务 , 处理完放入线程池 。我们同样来测试一下
文章插图
2.4 newScheduledThreadPool(指定最大线程数量)创建一个没有最大线程数限制的可以定时执行线程池在这里 , 还有创建一个只有单个线程的可以定时执行线程池(Executors.newSingleThreadScheduledExecutor())这些都是上面的线程池扩展开来了 , 不详细介绍了 。
3 介绍线程池的七大参数上面我们也说到了线程池有五种实现方式 , 但是实际上我们就介绍了四种 。那么最后一种是什么呢?不急 , 我们可以点开我们上面线程池实现方式的源码进行查看 , 可以发现
- newSingleThreadExecutor()的实现源码

文章插图
而点开其他几个线程池到最后都可以发现 , 他们实际上用的就是这个ThreadPoolExecutor 。我们把源代码粘过来分析 , 其实也就是这七大参数
/*** Creates a new {@code ThreadPoolExecutor} with the given initial* parameters.** @param corePoolSize the number of threads to keep in the pool, even*if they are idle, unless {@code allowCoreThreadTimeOut} is set* @param maximumPoolSize the maximum number of threads to allow in the*pool* @param keepAliveTime when the number of threads is greater than*the core, this is the maximum time that excess idle threads*will wait for new tasks before terminating.* @param unit the time unit for the {@code keepAliveTime} argument* @param workQueue the queue to use for holding tasks before they are*executed.This queue will hold only the {@code Runnable}*tasks submitted by the {@code execute} method.* @param threadFactory the factory to use when the executor*creates a new thread* @param handler the handler to use when execution is blocked*because the thread bounds and queue capacities are reached* @throws IllegalArgumentException if one of the following holds:<br>*{@code corePoolSize < 0}<br>*{@code keepAliveTime < 0}<br>*{@code maximumPoolSize <= 0}<br>*{@code maximumPoolSize < corePoolSize}* @throws NullPointerException if {@code workQueue}*or {@code threadFactory} or {@code handler} is null*/public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) {if (corePoolSize < 0 ||maximumPoolSize <= 0 ||maximumPoolSize < corePoolSize ||keepAliveTime < 0)throw new IllegalArgumentException();if (workQueue == null || threadFactory == null || handler == null)throw new NullPointerException();this.acc = System.getSecurityManager() == null ?null :AccessController.getContext();this.corePoolSize = corePoolSize;this.maximumPoolSize = maximumPoolSize;this.workQueue = workQueue;this.keepAliveTime = unit.toNanos(keepAliveTime);this.threadFactory = threadFactory;this.handler = handler;}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 什么是“进程、线程、协程”?
- 智能手机|千元级闪充旗舰 realme真我 Q5 Pro大升级电池+80W快充:33分钟充满
- 金花茶的功效与作用,详谈关于芍药花茶基本的功效与作用
- 有没有猫仙 关于猫的灵异事件真实
- 纽扣电池cr2030和cr2032通用吗,纽扣电池cr2032和cr1620可以通用吗?
- 燃料电池是通过什么产生电 微生物燃料电池的应用
- 外星人假说 关于外星人的猜想
- 关于微服务的几点老板关心问题
- 关于内存的那些知识误区盘点
- 彗星的介绍 关于彗星的资料