日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ThreadPoolExecutor(三)——Worker

發(fā)布時(shí)間:2024/3/13 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ThreadPoolExecutor(三)——Worker 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.Worker

Worker是ThreadPoolExecutor的一個(gè)內(nèi)部類,實(shí)現(xiàn)了AbstractQueuedSynchronizer抽象類。

/*** Class Worker mainly maintains interrupt control state for* threads running tasks, along with other minor bookkeeping.* This class opportunistically extends AbstractQueuedSynchronizer* to simplify acquiring and releasing a lock surrounding each* task execution. This protects against interrupts that are* intended to wake up a worker thread waiting for a task from* instead interrupting a task being run. We implement a simple* non-reentrant mutual exclusion lock rather than use ReentrantLock* because we do not want worker tasks to be able to reacquire the* lock when they invoke pool control methods like setCorePoolSize.*/

看注釋,該類主要是控制在線程執(zhí)行任務(wù)時(shí)的interrupt操作的。它繼承了AbstractQueuedSynchronizer類,實(shí)現(xiàn)了一個(gè)非重入的鎖。該鎖會(huì)保護(hù)一個(gè)正在等待任務(wù)被執(zhí)行的Worker不被interrupt操作打斷。

為什么不用ReentrantLock,要用非重入的鎖?因?yàn)樽髡卟幌胱屵@個(gè)Worker task在setCorePoolSize這種線程池控制方法調(diào)用時(shí)能重新獲取到鎖。

2.構(gòu)造方法

/*** Creates with given first task and thread from ThreadFactory.* @param firstTask the first task (null if none)*/Worker(Runnable firstTask) {this.firstTask = firstTask;this.thread = getThreadFactory().newThread(this);}

用自己作為task構(gòu)造一個(gè)線程,同時(shí)把外層任務(wù)賦值給自己的task成員變量,相當(dāng)于對(duì)task做了一個(gè)包裝。

3.run方法

run方法調(diào)用了ThreadPoolExecutor的runWorker方法,

4.runWorker方法

/*** Main worker run loop. Repeatedly gets tasks from queue and* executes them, while coping with a number of issues:** 1. We may start out with an initial task, in which case we* don't need to get the first one. Otherwise, as long as pool is* running, we get tasks from getTask. If it returns null then the* worker exits due to changed pool state or configuration* parameters. Other exits result from exception throws in* external code, in which case completedAbruptly holds, which* usually leads processWorkerExit to replace this thread.** 2. Before running any task, the lock is acquired to prevent* other pool interrupts while the task is executing, and* clearInterruptsForTaskRun called to ensure that unless pool is* stopping, this thread does not have its interrupt set.** 3. Each task run is preceded by a call to beforeExecute, which* might throw an exception, in which case we cause thread to die* (breaking loop with completedAbruptly true) without processing* the task.** 4. Assuming beforeExecute completes normally, we run the task,* gathering any of its thrown exceptions to send to* afterExecute. We separately handle RuntimeException, Error* (both of which the specs guarantee that we trap) and arbitrary* Throwables. Because we cannot rethrow Throwables within* Runnable.run, we wrap them within Errors on the way out (to the* thread's UncaughtExceptionHandler). Any thrown exception also* conservatively causes thread to die.** 5. After task.run completes, we call afterExecute, which may* also throw an exception, which will also cause thread to* die. According to JLS Sec 14.20, this exception is the one that* will be in effect even if task.run throws.** The net effect of the exception mechanics is that afterExecute* and the thread's UncaughtExceptionHandler have as accurate* information as we can provide about any problems encountered by* user code.** @param w the worker*/final void runWorker(Worker w) {Runnable task = w.firstTask;w.firstTask = null;boolean completedAbruptly = true;try {while (task != null || (task = getTask()) != null) {w.lock();clearInterruptsForTaskRun();try {beforeExecute(w.thread, task);Throwable thrown = null;try {task.run();} catch (RuntimeException x) {thrown = x; throw x;} catch (Error x) {thrown = x; throw x;} catch (Throwable x) {thrown = x; throw new Error(x);} finally {afterExecute(task, thrown);}} finally {task = null;w.completedTasks++;w.unlock();}}completedAbruptly = false;} finally {processWorkerExit(w, completedAbruptly);}}

這是執(zhí)行task的主流程。

上面說過,addWorker會(huì)用當(dāng)前task創(chuàng)建一個(gè)Worker對(duì)象,相當(dāng)于對(duì)task的包裝,然后用Worker對(duì)象作為task創(chuàng)建一個(gè)Thread,該Thread保存在Worker的thread成員變量中。在addWorker中啟動(dòng)了這個(gè)線程,線程中執(zhí)行runWorker方法。

先看注釋:

1.首先取傳入的task執(zhí)行,如果task是null,只要該線程池處于運(yùn)行狀態(tài),就會(huì)通過getTask方法從workQueue中取任務(wù)。ThreadPoolExecutor的execute方法會(huì)在無法產(chǎn)生core線程的時(shí)候向workQueue隊(duì)列中offer任務(wù)。

getTask方法從隊(duì)列中取task的時(shí)候會(huì)根據(jù)相關(guān)配置決定是否阻塞和阻塞多久。如果getTask方法結(jié)束,返回的是null,runWorker循環(huán)結(jié)束,執(zhí)行processWorkerExit方法。

至此,該線程結(jié)束自己的使命,從線程池中“消失”。

2.在開始執(zhí)行任務(wù)之前,會(huì)調(diào)用Worker的lock方法,目的是阻止task正在被執(zhí)行的時(shí)候被interrupt,通過調(diào)用clearInterruptsForTaskRun方法來保證的(后面可以看一下這個(gè)方法),該線程沒有自己的interrupt set了。

3.beforeExecute和afterExecute方法用于在執(zhí)行任務(wù)前后執(zhí)行一些自定義的操作,這兩個(gè)方法是空的,留給繼承類去填充功能。

我們可以在beforeExecute方法中拋出異常,這樣task不會(huì)被執(zhí)行,而且在跳出該循環(huán)的時(shí)候completedAbruptly的值是true,表示the worker died due to user exception,會(huì)用decrementWorkerCount調(diào)整wc。

4.因?yàn)镽unnable的run方法不能拋出Throwables異常,所以這里重新包裝異常然后拋出,拋出的異常會(huì)使當(dāng)當(dāng)前線程死掉,可以在afterExecute中對(duì)異常做一些處理。

5.afterExecute方法也可能拋出異常,也可能使當(dāng)前線程死掉。

總結(jié)

以上是生活随笔為你收集整理的ThreadPoolExecutor(三)——Worker的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲国产一区二区三区在线观看 | 日韩一卡| 呦女精品 | 打屁股黄文 | 高清国产mv在线观看 | 欧美亚洲在线 | 美女av网站| 尤物在线视频观看 | 草久网 | 不卡的av电影 | 亚洲视频日韩 | 精品国产一区二区三区久久久 | 自拍偷拍亚洲视频 | 久久香蕉热 | 午夜免费影院 | 中文无码一区二区三区在线观看 | 精品久久久久久一区二区里番 | 欧美日韩视频无码一区二区三 | 日韩一级片网址 | 69精品人妻一区二区三区 | 色先锋在线 | 国产一区一一区高清不卡 | 国产精品久久久免费观看 | 一级成人免费视频 | 男人操女人的网站 | 免费国产视频 | 男女啪啪免费网站 | japanese av在线 | 欧美精品在线一区二区 | 韩国理论午夜 | 日韩全黄 | 亚洲乱码国产乱码精品 | 亚洲高清在线一区 | 国语av在线 | 99av在线 | a毛片视频 | 亚洲男人的天堂av | 色婷婷久久一区二区三区麻豆 | 福利在线影院 | 国产午夜精品久久 | 鲁啊鲁在线视频 | 午夜精品影院 | 欧美日本另类 | 白丝少妇 | a级片免费观看 | 欧美人与禽性xxxxx杂性 | 日本少妇色视频 | 亚洲av成人精品日韩在线播放 | 97在线看 | 国产制服在线 | 伦在线 | 香蕉久久夜色 | 午夜视频色 | 国产精品老牛影院99av | 精品人妻伦一二三区免费 | 激情图片网站 | 国产乱淫av | 亚洲国产欧美一区二区三区深喉 | 少妇人妻偷人精品无码视频 | 五月婷婷激情小说 | aaaaa毛片 | 久草新免费 | 亚洲四虎av | 在线va视频 | 亚洲精品国产免费 | 青青草激情 | 在线免费观看av网址 | 好吊在线视频 | 国产91av在线播放 | 官场艳妇疯狂性关系 | 伊人91视频| 日本青草视频 | 午夜成人影视 | 国产在线视频第一页 | 国产精选视频在线观看 | 激情视频在线免费观看 | 国产一区91精品张津瑜 | 一区二区视频在线看 | 波多野结衣亚洲一区二区 | 在线观看www | 国产成人精品一区二三区四区五区 | 久久国产精品免费视频 | 欧美日a | 久久久www免费人成人片 | av在线地址| 欧美激情一级精品国产 | 涩涩在线观看 | 免费在线视频一区二区 | 爱福利视频广场 | 日韩亚洲欧美综合 | 亚洲精品乱码久久久久 | 欧美一区二区三 | 国产成人在线一区二区 | 无码国产精品一区二区色情男同 | 无码精品国产一区二区三区免费 | 成人乱人乱一区二区三区 | 在线观看av片 | 九一国产在线 | 9999re|