编辑文章 公众号爬虫 null <p> </p> <div id="js_article" class="rich_media"> <div class="rich_media_inner"> <div id="page-content" class="rich_media_area_primary"> <div class="rich_media_area_primary_inner"> <div id="js_content" class="rich_media_content " style="visibility: show;"> <section style="padding-right: 10px; padding-left: 10px; line-height: 1.6; word-break: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 15px; letter-spacing: 0.05em; color: #595959;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com"> <h2 style="min-height: 32px; line-height: 28px; color: #773098; border-bottom: 1px solid #773098; border-top-color: #773098; border-right-color: #773098; border-left-color: #773098; font-size: 22px; margin: 1em auto; padding-top: 0.5em; padding-bottom: 0.5em; text-align: center; width: 85%; font-weight: bold; display: flex; flex-direction: column; justify-content: center;" data-tool="mdnice编辑器">1. Java的线程池</h2> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">① 合理使用线程池的好处</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">Java的线程池是运用场景最多的并发框架,几乎所有需要异步或者并发执行任务的程序都可以使用线程池。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">合理使用线程池能带来的好处:</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">降低资源消耗。</strong></p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">通过重复利用已经创建的线程降低线程创建的和销毁造成的消耗。例如,工作线程Woker会无线循环获取阻塞队列中的任务来执行。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">提高响应速度。</strong></p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">当任务到达时,任务可以不需要等到线程创建就能立即执行。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">提高线程的可管理性。</strong></p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">线程是稀缺资源,Java的线程池可以对线程资源进行统一分配、调优和监控。</p> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">② 线程池的工作流程</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">一个新的任务到线程池时,线程池的处理流程如下:</p> <ol class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">线程池判断核心线程池里的线程是否都在执行任务。如果不是,创建一个新的工作线程来执行任务。如果核心线程池里的线程都在执行任务,则进入下个流程。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">线程池判断阻塞队列是否已满。如果阻塞队列没有满,则将新提交的任务存储在阻塞队列中。如果阻塞队列已满,则进入下个流程。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">线程池判断线程池里的线程是否都处于工作状态。如果没有,则创建一个新的工作线程来执行任务。如果已满,则交给饱和策略来处理这个任务。</section> </li> </ol> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img style="display: block; margin-right: auto; margin-left: auto;" src="http://img.liflag.cn/post/7bd69c2deeac1516ca585840d348e615.png" data-ratio="0.6403402187120292" data-type="png" data-w="823" /></figure> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">线程池的核心实现类是ThreadPoolExecutor类,用来执行提交的任务。因此,任务提交到线程池时,具体的处理流程是由ThreadPoolExecutor类的execute()方法去完成的。</p> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img style="display: block; margin-right: auto; margin-left: auto;" src="http://img.liflag.cn/post/16ac0d4f134bf4a90ba8f02965082c14.png" data-ratio="0.9287833827893175" data-type="png" data-w="674" /></figure> <ol class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如果当前运行的线程少于corePoolSize,则创建新的工作线程来执行任务(执行这一步骤需要获取全局锁)。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如果当前运行的线程大于或等于corePoolSize,而且BlockingQueue未满,则将任务加入到BlockingQueue中。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如果BlockingQueue已满,而且当前运行的线程小于maximumPoolSize,则创建新的工作线程来执行任务(执行这一步骤需要获取全局锁)。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如果当前运行的线程大于或等于maximumPoolSize,任务将被拒绝,并调用RejectExecutionHandler.rejectExecution()方法。即调用饱和策略对任务进行处理。</section> </li> </ol> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">工作线程(Worker)</strong></p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">线程池在创建线程时,会将线程封装成工作线程Woker。Woker在执行完任务后,不是立即销毁而是循环获取阻塞队列里的任务来执行。</p> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">③ 线程池的创建(7个参数)</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">可以通过ThreadPoolExecutor来创建一个线程池:</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">new ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, <br /> TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)</code></p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">corePoolSize(线程池的基本大小):</strong></p> <ol class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">提交一个任务到线程池时,线程池会创建一个新的线程来执行任务。注意:即使有空闲的基本线程能执行该任务,也会创建新的线程。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如果线程池中的线程数已经大于或等于corePoolSize,则不会创建新的线程。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如果调用了线程池的prestartAllCoreThreads()方法,线程池会提前创建并启动所有基本线程。</section> </li> </ol> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">maximumPoolSize(线程池的最大数量):线程池允许创建的最大线程数。</strong></p> <ol class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">阻塞队列已满,线程数小于maximumPoolSize便可以创建新的线程执行任务。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如果使用无界的阻塞队列,该参数没有什么效果。</section> </li> </ol> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">workQueue(工作队列):用于保存等待执行的任务的阻塞队列。</strong></p> <ol class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">ArrayBlockingQueue:基于数组结构的有界阻塞队列,按FIFO(先进先出)原则对任务进行排序。使用该队列,线程池中能创建的最大线程数为maximumPoolSize。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">LinkedBlockingQueue:基于链表结构的无界阻塞队列,按FIFO(先进先出)原则对任务进行排序,吞吐量高于ArrayBlockingQueue。使用该队列,线程池中能创建的最大线程数为corePoolSize。静态工厂方法 Executor.newFixedThreadPool()使用了这个队列。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">SynchronousQueue:一个不存储元素的阻塞队列。添加任务的操作必须等到另一个线程的移除操作,否则添加操作一直处于阻塞状态。静态工厂方法 Executor.newCachedThreadPool()使用了这个队列。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">PriorityBlokingQueue:一个支持优先级的无界阻塞队列。使用该队列,线程池中能创建的最大线程数为corePoolSize。</section> </li> </ol> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">keepAliveTime(线程活动保持时间):线程池的工作线程空闲后,保持存活的时间。如果任务多而且任务的执行时间比较短,可以调大keepAliveTime,提高线程的利用率。</strong></p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">unit(线程活动保持时间的单位):可选单位有DAYS、HOURS、MINUTES、毫秒、微秒、纳秒。</strong></p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">handler(饱和策略,或者又称拒绝策略):当队列和线程池都满了,即线程池饱和了,必须采取一种策略处理提交的新任务。</strong></p> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">AbortPolicy:无法处理新任务时,直接抛出异常,这是默认策略。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">CallerRunsPolicy:用调用者所在的线程来执行任务。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">DiscardOldestPolicy:丢弃阻塞队列中最靠前的一个任务,并执行当前任务。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">DiscardPolicy:直接丢弃任务。</section> </li> </ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">threadFactory:构建线程的工厂类</strong></p> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">总结:</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">1.常用的5个,核心池、最大池、空闲时间、时间的单位、阻塞队列;另外两个:拒绝策略、线程工厂类</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">2.常见线程池的创建参数如下。PS: CachedThreadPool核心池为0,最大池为Integer.MAX_VALUE,相当于只使用了最大池;其他线程池,核心池与最大池一样大,因此相当于只用了核心池。</p> <div class="editor-preview markdown-body"> <pre><code>FixedThredPool: new ThreadExcutor(n, n, 0L, ms, new LinkedBlockingQueue<Runable>()<br />SingleThreadExecutor: new ThreadExcutor(1, 1, 0L, ms, new LinkedBlockingQueue<Runable>())<br />CachedTheadPool: new ThreadExcutor(0, max_valuem, 60L, s, new SynchronousQueue<Runnable>());<br />ScheduledThreadPoolExcutor: ScheduledThreadPool, SingleThreadScheduledExecutor.<br /></code></pre> </div> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"> 3.如果使用的阻塞队列为无界队列,则永远不会调用拒绝策略,因为再多的任务都可以放在队列中。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">4.SynchronousQueue是不存储任务的,新的任务要么立即被已有线程执行,要么创建新的线程执行。</p> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">④ 向线程池提交任务</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">使用ThreadPoolEXecutor.executor()方法来提交任务:</p> <div class="editor-preview markdown-body"> <pre><code>public void execute(Runnable command) {<br /> // command为null,抛出NullPointerException<br /> if (command == null)<br /> throw new NullPointerException(); <br /> int c = ctl.get();<br /> // 线程池中的线程数小于corePoolSize,创建新的线程<br /> if (workerCountOf(c) < corePoolSize) {<br /> if (addWorker(command, true))// 创建工作线程<br /> return;<br /> c = ctl.get();<br /> }<br /> // 将任务添加到阻塞队列,如果<br /> if (isRunning(c) && workQueue.offer(command)) {<br /> int recheck = ctl.get();<br /> if (! isRunning(recheck) && remove(command))<br /> reject(command);<br /> else if (workerCountOf(recheck) == 0)<br /> addWorker(null, false);<br /> }// 阻塞队列已满,尝试创建新的线程,如果超过maximumPoolSize,执行handler.rejectExecution()<br /> else if (!addWorker(command, false))<br /> reject(command);<br />}<br /></code></pre> </div> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"> ⑤ 线程池的五种运行状态</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">RUNNING :</strong> 该状态的线程池既能接受新提交的任务,又能处理阻塞队列中任务。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">SHUTDOWN:</strong> 该状态的线程池<strong style="color: #773098;">不能接收新提交的任务</strong>,<strong style="color: #773098;">但是能处理阻塞队列中的任务</strong>。(政府服务大厅不在允许群众拿号了,处理完手头的和排队的政务就下班。)</p> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">处于 RUNNING 状态时,调用 shutdown()方法会使线程池进入到该状态。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">注意:finalize() 方法在执行过程中也会隐式调用shutdown()方法。</section> </li> </ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">STOP:</strong> 该状态的线程池不接受新提交的任务,也不处理在阻塞队列中的任务,还会中断正在执行的任务。(政府服务大厅不再进行服务了,拿号、排队、以及手头工作都停止了。)</p> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">在线程池处于 RUNNING 或 SHUTDOWN 状态时,调用 shutdownNow() 方法会使线程池进入到该状态;</section> </li> </ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">TIDYING:</strong> 如果所有的任务都已终止,workerCount (有效线程数)=0 。</p> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">线程池进入该状态后会调用 terminated() 钩子方法进入TERMINATED 状态。</section> </li> </ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">TERMINATED:</strong> 在terminated()钩子方法执行完后进入该状态,默认terminated()钩子方法中什么也没有做。</p> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img style="display: block; margin-right: auto; margin-left: auto;" src="http://img.liflag.cn/post/decc1799b771b3433ea2a05a58915503.png" data-ratio="0.39416058394160586" data-type="png" data-w="959" /></figure> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">⑥ 线程池的关闭(shutdown或者shutdownNow方法)</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">可以通过调用线程池的shutdown或者shutdownNow方法来关闭线程池:遍历线程池中工作线程,逐个调用interrupt方法来中断线程。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">shutdown方法与shutdownNow的特点:</p> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">shutdown方法将线程池的状态设置为SHUTDOWN状态,只会中断空闲的工作线程。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">shutdownNow方法将线程池的状态设置为STOP状态,会中断所有工作线程,不管工作线程是否空闲。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">调用两者中任何一种方法,都会使isShutdown方法的返回值为true;线程池中所有的任务都关闭后,isTerminated方法的返回值为true。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">通常使用shutdown方法关闭线程池,如果不要求任务一定要执行完,则可以调用shutdownNow方法。</section> </li> </ul> <h2 style="min-height: 32px; line-height: 28px; color: #773098; border-bottom: 1px solid #773098; border-top-color: #773098; border-right-color: #773098; border-left-color: #773098; font-size: 22px; margin: 1em auto; padding-top: 0.5em; padding-bottom: 0.5em; text-align: center; width: 85%; font-weight: bold; display: flex; flex-direction: column; justify-content: center;" data-tool="mdnice编辑器">2. java线程池的调优以及监控</h2> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">① 线程池的调优(线程池的合理配置)</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">先从以下几个角度分析任务的特性:</p> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">任务的性质:CPU 密集型任务、IO 密集型任务和混合型任务。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">任务的优先级:高、中、低。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">任务的执行时间:长、中、短。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">任务的依赖性:是否依赖其他系统资源,如数据库连接。</section> </li> </ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">任务性质不同的任务可以用不同规模的线程池分开处理。可以通过 Runtime.getRuntime().availableProcessors() 方法获得当前设备的 CPU 个数。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">CPU 密集型任务配置</strong> 尽可能小的线程,如配置<code style="font-size: 14px; padding: 2px 4px; border-radius: 4px; margin-right: 2px; margin-left: 2px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #9654b5;">N^cpu+1</code>个线程的线程池。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">IO 密集型任务</strong>则由于线程并不是一直在执行任务,则配置尽可能多的线程,如<code style="font-size: 14px; padding: 2px 4px; border-radius: 4px; margin-right: 2px; margin-left: 2px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #9654b5;">2*N^cpu</code>。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器"><strong style="color: #773098;">混合型任务</strong> 如果可以拆分,则将其拆分成一个 CPU 密集型任务和一个 IO 密集型任务。只要这两个任务执行的时间相差不是太大,那么分解后执行的吞吐率要高于串行执行的吞吐率;如果这两个任务执行时间相差太大,则没必要进行分解。</p> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">优先级不同的任务可以使用优先级队列 PriorityBlockingQueue 来处理,它可以让优先级高的任务先得到执行。但是,如果一直有高优先级的任务加入到阻塞队列中,那么低优先级的任务可能永远不能执行。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">执行时间不同的任务可以交给不同规模的线程池来处理,或者也可以使用优先级队列,让执行时间短的任务先执行。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">依赖数据库连接池的任务,因为线程提交 SQL 后需要等待数据库返回结果,线程数应该设置得较大,这样才能更好的利用 CPU。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">建议使用有界队列,有界队列能增加系统的稳定性和预警能力。可以根据需要设大一点,比如几千。使用无界队列,线程池的队列就会越来越大,有可能会撑满内存,导致整个系统不可用。② 线程池的监控</section> </li> </ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">可以通过线程池提供的参数读线程池进行监控,有以下属性可以使用:</p> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;"><code style="font-size: 14px; padding: 2px 4px; border-radius: 4px; margin-right: 2px; margin-left: 2px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #9654b5;">taskCount</code>:线程池需要执行的任务数量,包括已经执行完的、未执行的和正在执行的。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;"><code style="font-size: 14px; padding: 2px 4px; border-radius: 4px; margin-right: 2px; margin-left: 2px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #9654b5;">completedTaskCount</code>:线程池在运行过程中已完成的任务数量, <code style="font-size: 14px; padding: 2px 4px; border-radius: 4px; margin-right: 2px; margin-left: 2px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #9654b5;">completedTaskCount <= taskCount</code>。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;"><code style="font-size: 14px; padding: 2px 4px; border-radius: 4px; margin-right: 2px; margin-left: 2px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #9654b5;">largestPoolSize</code>:线程池曾经创建过的最大线程数量,通过这个数据可以知道线程池是否满过。如等于线程池的最大大小,则表示线程池曾经满了。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;"><code style="font-size: 14px; padding: 2px 4px; border-radius: 4px; margin-right: 2px; margin-left: 2px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #9654b5;">getPoolSize</code>: 线程池的线程数量。如果线程池不销毁的话,池里的线程不会自动销毁,所以线程池的线程数量只增不减。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;"><code style="font-size: 14px; padding: 2px 4px; border-radius: 4px; margin-right: 2px; margin-left: 2px; background-color: rgba(27, 31, 35, 0.05); font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #9654b5;">getActiveCount</code>:获取活动的线程数。</section> </li> </ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">通过继承线程池并重写线程池的 beforeExecute,afterExecute 和 terminated 方法,我们可以在任务执行前,执行后和线程池关闭前干一些事情。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">如监控任务的平均执行时间,最大执行时间和最小执行时间等。这几个方法在线程池里是空方法,如:</p> <div class="editor-preview markdown-body"> <pre><code>protected void beforeExecute(Thread t, Runnable r) { }</code></pre> </div> <h2 style="min-height: 32px; line-height: 28px; color: #773098; border-bottom: 1px solid #773098; border-top-color: #773098; border-right-color: #773098; border-left-color: #773098; font-size: 22px; margin: 1em auto; padding-top: 0.5em; padding-bottom: 0.5em; text-align: center; width: 85%; font-weight: bold; display: flex; flex-direction: column; justify-content: center;" data-tool="mdnice编辑器">3. Java线程池的常见问题</h2> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">1. 讲讲Java的线程池</h3> <h4 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;" data-tool="mdnice编辑器">基础讲解:</h4> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">以ThreadPoolExecutor为切入点,讲解excute()方法中所体现的Java线程池运行流程。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">工作线程Worker,它的循环工作特点</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如何新建线程池:7个参数(重点在阻塞队列和饱和策略)</section> </li> </ul> <h5 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black;" data-tool="mdnice编辑器">进阶:</h5> <ul class="list-paddingleft-2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black;" data-tool="mdnice编辑器"> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">线程池五个状态的特点以及如何进行状态之间的切换:running、shutdown、stop、tidying、terminated。</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">如何关闭线程:shutdown方法和shutdownNow方法的特点</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">线程池的调优(针对任务的不同特性 + 建议使用有界队列)</section> </li> <li> <section style="line-height: 26px; color: #010101; margin-top: 10px; margin-bottom: 10px;">线程池的监控参数以及可以重写的方法。</section> </li> </ul> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">两种主要的线程池类型:普通的线程池ThreadPoolExecutor,支持延迟或周期性执行的任务的线程池ScheduledThreadPoolExcutor。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">讲解ThreadPoolExcutor中5个常用参数+2个不常用参数,包含的三种线程池:创建时的参数、运行的流程、各自适合的场景。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">讲解ScheduledThreadPoolExecutor的阻塞队列的原理、如何更改任务的time。</p> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">提供了五种定义好的线程池,都可以通过Executors工具类去调用,比如Executors.newFixedThreadPool(12)</p> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">2. 具体的场景</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">如果corePoolSize为x,maximumPoolSize为y,阻塞队列为z,第w个任务进来如何分配?</p> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">3. 线程池如何进行调优?</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">线程池的调优(针对任务的不同特性 + 建议使用有界队列)</p> <h3 style="font-size: 20px; margin-top: 1.2em; margin-bottom: 1em; font-weight: bold; color: #773098;" data-tool="mdnice编辑器">4. 线程池中的核心参数</h3> <p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: black; margin: 1em 4px;" data-tool="mdnice编辑器">超过核心size怎么处理,队列满怎么处理,拒绝策略有哪些?(比较具体)</p> </section> <section style="padding-right: 10px; padding-left: 10px; white-space: normal; color: black; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 16px; letter-spacing: 0px; text-align: left; background-color: #ffffff; line-height: 1.6; word-break: break-word;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com"></section> <p style="padding: 8px 10px; letter-spacing: 0px; line-height: 26px; word-break: break-word;"> </p> </div> </div> </div> </div> </div> <p> </p> <p class="copyright">注意:本文归作者所有,未经作者允许,不得转载</p> 标题: 分类: 请选择分类(必选) 博客 酷玩 分享 相册 标签: 按 Tab、回车或逗号把输入的文字变成标签;点 × 或在输入框为空时按退格删除;最多 8 个 封面图: 上传图片 建议尺寸 800x400,图片大小不超过 1MB 展开更多设置(关键词、摘要、转载、评论) 关键词: 摘要: 1. Java的线程池 ① 合理使用线程池的好处 Java的线程池是运用场景最多的并发框架,几乎所有需要异步或者并发执行任务的程序都可以使用线程池。 合理使用线程池能带来的好处: 降低资源消耗。 通过重复利用已经创建的线... 选项: 是否转载 启用评论 勾选「是否转载」可标注文章来源,原文链接可留空。 正文: B I U H2 H3 引用 • 列表 1. 列表 链接 图片 清格式 源码 该文章是富文本(HTML)写的,已自动切到富文本编辑器,不会按 markdown 解析;要精确改 HTML 点「源码」。 保存修改