???????Java?????
???????????? ???????[ 2016/4/7 10:38:14 ] ??????????????????? ???????
??????.??????????????????
?????????????????????????ThreadPoolExecutor????????????????????????????????????????????漸?????潲??
????1.??????
????2.????????
????3.?????е????????
????4.????????м???????
????5.??????????
????6.???????
????7.????????????????
????1.??????
??????ThreadPoolExecutor?ж????????Volatile?????????????????static final???????????????????
????volatile int runState;
????static final int RUNNING = 0;
????static final int SHUTDOWN = 1;
????static final int STOP = 2;
????static final int TERMINATED = 3;
????runState??????????????????????volatile???????????????????????
????????????static final???????runState????????????
????????????????????????????RUNNING????
?????????????shutdown()???????????????SHUTDOWN????????????????????μ??????????????????????????
?????????????shutdownNow()???????????????STOP??????????????????μ????????????????????????е?????
?????????????SHUTDOWN??STOP???????????й????????????????????????????????н???????????????TERMINATED????
????2.????????
?????????????????????????????????????????????????????????ThreadPoolExecutor???????????Щ???????????????
????private final BlockingQueue<Runnable> workQueue; //????????У????????????е?????
????private final ReentrantLock mainLock = new ReentrantLock(); //?????????????????????????????????С
????//??runState????????????????
????private final HashSet<Worker> workers = new HashSet<Worker>(); //????????????
????private volatile long keepAliveTime; //????????
????private volatile boolean allowCoreThreadTimeOut; //????????????????????????
????private volatile int corePoolSize; //???????С?????????е?????????????????????????????????????????У?
????private volatile int maximumPoolSize; //?????????????????
????private volatile int poolSize; //?????е?????????
????private volatile RejectedExecutionHandler handler; //??????????
????private volatile ThreadFactory threadFactory; //???????????????????
????private int largestPoolSize; //??????????????????????????????
????private long completedTaskCount; //????????????????????????
??????????????????????????????????????????????corePoolSize??maximumPoolSize??largestPoolSize??????????
????corePoolSize??????????????????С??????????????????????С??????????????
?????????????????????????????10????????????????????????????
???????????10?????????й???????е?????????????????е????????
??????10?????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????4??????????????
???????????????????4?????????????
??????????14??????????????????????????????????????????????????μ?????????????????Щ???????
????????14?????????????????????????????????????????????????????????????4????????????????????10????????????????????????????
????????????е?corePoolSize??10????maximumPoolSize??14??10+4????
????????corePoolSize???????С??maximumPoolSize???????????????????????????????????????????????????
????????????????????????滹???corePoolSize??????????С??
????largestPoolSize??????????????????????????????????????????й???????????????????????????κι????
????????????????????????????????????????????????Щ?????
??????ThreadPoolExecutor???У??????????????????execute()????????????submit????????????????????submit?????????????????execute()???????????????????о?execute()????????????????
????public void execute(Runnable command) {
????if (command == null)
????throw new NullPointerException();
????if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command)) {
????if (runState == RUNNING && workQueue.offer(command)) {
????if (runState != RUNNING || poolSize == 0)
????ensureQueuedTaskHandled(command);
????}
????else if (!addIfUnderMaximumPoolSize(command))
????reject(command); // is shutdown or saturated
????}
????}
????????????????????????????????????????????????????
??????????ж?????????command????null??????null????????????????
??????????????????ú??????£?
????if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command))
????????????????????????????????????????????????е?????????С???????С??????????????????if???????
????????????е???????С???????С?????????к??????????У?
????addIfUnderCorePoolSize(command)
????????????addIfUnderCorePoolSize???????????false???????????????if???飬????????????????????????
????????????addIfUnderCorePoolSize???????????false?????????ж??
????if (runState == RUNNING && workQueue.offer(command))
?????????????????RUNNING?????????????????????У????????????????RUNNING??????????????????????????У?
????addIfUnderMaximumPoolSize(command)
??????????addIfUnderMaximumPoolSize?????????????reject()???????????????????
?????????棺
????if (runState == RUNNING && workQueue.offer(command))
??????????У???????????????RUNNING????????????????????г??????????????ж??
????if (runState != RUNNING || poolSize == 0)
????????ж?????????????????????????????е????????????????shutdown????shutdownNow????????????????????????????????????У?
????ensureQueuedTaskHandled(command)
???????????????????????????????? ??????????????е????????????
????????????2???????????????addIfUnderCorePoolSize??addIfUnderMaximumPoolSize??
????private boolean addIfUnderCorePoolSize(Runnable firstTask) {
????Thread t = null;
????final ReentrantLock mainLock = this.mainLock;
????mainLock.lock();
????try {
????if (poolSize < corePoolSize && runState == RUNNING)
????t = addThread(firstTask); //???????????firstTask????
????} finally {
????mainLock.unlock();
????}
????if (t == null)
????return false;
????t.start();
????return true;
????}
?????????addIfUnderCorePoolSize????????????????????????????????????????????С???е?????????濴?????????????????????????????漰??????????仯???????if????ж????????е??????????С???????С????????????????????????execute()?????в???????ж?????????????????????С???????С??????addIfUnderCorePoolSize??????????????????????ж??????????????ж?????в???м?????????????execute?????ж?????poolSizeС??corePoolSize?????ж????????????????????????????????????????poolSize??С??corePoolSize???????????????????????ж?????????ж????????????RUNNING???????????????п?????????????е?????shutdown????shutdownNow??????????????
????t = addThread(firstTask);
?????????????????????????????????????????????Thread??????????????????ж?t????????????????????????????poolSize>=corePoolSize????runState??????RUNNING???????????t.start()???????????
???????????????addThread??????????
????private Thread addThread(Runnable firstTask) {
????Worker w = new Worker(firstTask);
????Thread t = threadFactory.newThread(w); //??????????????????
????if (t != null) {
????w.thread = t; //???????????????????w????????
????workers.add(w);
????int nt = ++poolSize; //??????????1
????if (nt > largestPoolSize)
????largestPoolSize = nt;
????}
????return t;
????}
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Java???????????Щ???????????????Java????????????????Java?б???Map????????Java Web???????????????Java??????????????д?????Java????????7???????????????????????(java .net ?????)???Java??????????Python??????Java webdriver??λ????????′????е?????Java??д??????????????????Java???????????????JavaScript????????????Java?????????????????? Java???????10??????????????Java?м????????????????java???????ü???????????м???????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????