Java countDownLatch如何實(shí)現(xiàn)多線程任務(wù)阻塞等待
我這里需要通過(guò)多線程去處理數(shù)據(jù),然后在所有數(shù)據(jù)都處理完成后再往下執(zhí)行。這里就用到了CountDownLatch。把countdownlatch作為參數(shù)傳入到每個(gè)線程類里,在線程中處理完數(shù)據(jù)后執(zhí)行countdown方法。在所有countdownlatch歸零后,其await方法結(jié)束阻塞狀態(tài)而往下執(zhí)行。
具體代碼如下:
將多線程任務(wù)提交線程池
@Bean(name = "ggnews_executor")
public Executor postExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(1);
executor.setMaxPoolSize(1);
executor.setQueueCapacity(1);
executor.setKeepAliveSeconds(120);
executor.setThreadNamePrefix("executor-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
return executor;
}
//通過(guò)定時(shí)任務(wù)調(diào)用的fetch方法,為了避免定時(shí)任務(wù)在多次執(zhí)行中失效,通異步指定線程池的方式進(jìn)行調(diào)用
@Async("ggnews_executor")
public void fetch() {
if(fetchFlag.getAndSet(false)) {
List<FetchTag> tags = fetchTagService.selectFetchTagList(fetchTag);
CountDownLatch downLatch = new CountDownLatch(tags.size());
for (FetchTag tag : tags) {
FetchTag tagNew;
try {
tagNew =(FetchTag) tag.clone();
} catch (Throwable e) {
log.error("",e);
continue;
}
//作為參數(shù)將CountDownLatch傳入
InnerRunner innerRunner = new InnerRunner(downLatch, tagNew);
executor.execute(innerRunner);
}
try {
//等待線程執(zhí)行完畢,如果十分鐘后還沒(méi)結(jié)束也會(huì)停止阻塞狀態(tài)
downLatch.await(10,TimeUnit.MINUTES);
fetchFlag.getAndSet(true);
} catch (Throwable e) {
log.error("fetch()方法發(fā)生錯(cuò)誤:{}", e);
fetchFlag.getAndSet(true);
//e.printStackTrace();
} finally {
fetchFlag.getAndSet(true);
}
} else {
log.info("=======上次抓取尚未結(jié)束=========");
}
}
InnerRunner為要執(zhí)行具體任務(wù)的線程類
private class InnerRunner implements Runnable {
private CountDownLatch downLatch;
private FetchTag tag;
private InnerRunner(CountDownLatch downLatch, FetchTag tag) {
this.downLatch = downLatch;
this.tag = tag;
}
@Override
public void run() {
//將countDown方法移入到具體方法中的finally塊中,以保證即使在拋出異常的情況下也算執(zhí)行了此次任務(wù),countdown會(huì)被執(zhí)行
fetchGG(tag.getTag(), downLatch);
//downLatch.countDown();
this.tag = null;
}
}
private static final String GOOGLE_URL_IN = "https://news.google.com/rss/search?hl=hi&gl=IN&ceid=IN:hi&q=";
public void fetchGG(String tag, CountDownLatch downLatch) {
try {
Document document = Jsoup.parse(new URL(GOOGLE_URL_IN + URLEncoder.encode("\"" + tag + "\"", "utf-8")), 30000);
Elements elements = document.getElementsByTag("item");
int rank = 1;
for (Element element : elements) {
String sourceTitle = element.getElementsByTag("title").get(0).text();
log.info("source title:" + sourceTitle);
}
} catch (Throwable e) {
log.info("fetch google url error", e);
} finally {
//肯定會(huì)被執(zhí)行
downLatch.countDown();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java try-with-resource語(yǔ)法使用解析
這篇文章主要介紹了Java try-with-resource語(yǔ)法使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
詳解Spring Cloud負(fù)載均衡重要組件Ribbon中重要類的用法
本篇文章主要介紹了詳解Spring Cloud負(fù)載均衡重要組件Ribbon中重要類的用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
SpringBoot多數(shù)據(jù)源配置并通過(guò)注解實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源
本文主要介紹了SpringBoot多數(shù)據(jù)源配置并通過(guò)注解實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Mybatis批量插入數(shù)據(jù)的兩種方式總結(jié)與對(duì)比
批量插入功能是我們?nèi)粘9ぷ髦斜容^常見(jiàn)的業(yè)務(wù)功能之一,下面這篇文章主要給大家介紹了關(guān)于Mybatis批量插入數(shù)據(jù)的兩種方式總結(jié)與對(duì)比的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
SpringBoot基于SpringSecurity表單登錄和權(quán)限驗(yàn)證的示例
這篇文章主要介紹了SpringBoot基于SpringSecurity表單登錄和權(quán)限驗(yàn)證的示例。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java和C語(yǔ)言分別實(shí)現(xiàn)水仙花數(shù)及拓展代碼
這篇文章主要介紹了分別用Java和C語(yǔ)言實(shí)現(xiàn)水仙花數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
Spring Boot 2 實(shí)戰(zhàn):自定義啟動(dòng)運(yùn)行邏輯實(shí)例詳解
這篇文章主要介紹了Spring Boot 2 實(shí)戰(zhàn):自定義啟動(dòng)運(yùn)行邏輯,結(jié)合實(shí)例形式詳細(xì)分析了Spring Boot 2自定義啟動(dòng)運(yùn)行邏輯詳細(xì)操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-05-05

