ThreadPoolExecutor一些原理问题
来源:互联网 发布:兰州招聘seo 编辑:程序博客网 时间:2024/05/21 07:23
今天被同事问到,ThreadPoolExecutor是怎么在线程执行后,保存线程的; worker线程去调用具体的业务线程,是不是就启动了两次线程。
平时用的比较多的是用ExecutorService 来创建newCachedThreadPool(), newFixedThreadPool(),SingleThreadExecutor()等
看了一下源代码,这几个建线程池的方法都是创建ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
corePoolSize就是运行的核心线程的数,maximumPoolSize就是最大线程数,keepAliveTime是空闲线程的等待时间,workQueue是用来存放等待线程的。
当poolSize<corePoolSize时,每新来一个任务,则会创建一个工作线程(worker)来处理当前的任务, worker 处理完第一个线程后,会处理queue里的线程。worker在keepAliveTime的时间内,如果没有收到新的任务,则该worker会消失
1. ThreadPoolExecutor是怎么在线程执行后,一直保留worker线程(小于corePoolSize)存活的?
答案在getTask方法的代码里:
Runnable getTask() { for (;;) { try { int state = runState; if (state > SHUTDOWN) return null; Runnable r; if (state == SHUTDOWN) // Help drain queue r = workQueue.poll(); else if (poolSize > corePoolSize || allowCoreThreadTimeOut) r = workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS); else r = workQueue.take(); if (r != null) return r; if (workerCanExit()) { if (runState >= SHUTDOWN) // Wake up others interruptIdleWorkers(); return null; } // Else retry } catch (InterruptedException ie) { // On interruption, re-check runState } } }
2. worker线程去调用具体的业务线程,是不是就启动了两次线程
注意: worker线程会去调用任务线程里的run方法。而不是start方法。从而避免了多次创建线程private void runTask(Runnable task) { final ReentrantLock runLock = this.runLock; runLock.lock(); try { /* * Ensure that unless pool is stopping, this thread * does not have its interrupt set. This requires a * double-check of state in case the interrupt was * cleared concurrently with a shutdownNow -- if so, * the interrupt is re-enabled. */ if (runState < STOP && Thread.interrupted() && runState >= STOP) thread.interrupt(); /* * Track execution state to ensure that afterExecute * is called only if task completed or threw * exception. Otherwise, the caught runtime exception * will have been thrown by afterExecute itself, in * which case we don't want to call it again. */ boolean ran = false; beforeExecute(thread, task); try { task.run(); ran = true; afterExecute(task, null); ++completedTasks; } catch (RuntimeException ex) { if (!ran) afterExecute(task, ex); throw ex; } } finally { runLock.unlock(); } }
0 0
- ThreadPoolExecutor一些原理问题
- ThreadPoolExecutor 原理
- ThreadPoolExecutor原理
- ThreadPoolExecutor原理
- ThreadPoolExecutor一些小结
- ThreadPoolExecutor原理及使用
- ThreadPoolExecutor工作原理
- ThreadPoolExecutor运行原理
- ThreadPoolExecutor实现原理
- ThreadPoolExecutor原理与使用
- ThreadPoolExecutor原理及使用
- ThreadPoolExecutor原理简介
- ThreadPoolExecutor问题定位
- ThreadPoolExecutor的原理及使用方法
- 线程池ThreadPoolExecutor的原理
- 关于一些原理概念问题
- jdk concurrent threadPool---ThreadPoolExecutor原理分析
- 深入了解ThreadPoolExecutor类的工作原理
- cocos2dx andriod平台遇到的问题总结
- vs2010调试断点无法命中问题
- [Android开发] 修改布局文件后出现java.lang.ClassCastException
- 【串和序列处理 2】字符串编辑距离算法
- UPC——2219——A^X mod P
- ThreadPoolExecutor一些原理问题
- Practical Common Lisp学习笔记(二)快捷键备忘
- 程序员必知的8大排序(java实现)
- 解决LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
- exe + task scheduler 清理log
- BCG和Xtreme toolkit2种界面库比较
- Qt中串口16进制发送和接收数据
- ios图片加水印或文字
- java中final类的一些思考