午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java判斷線程池線程是否執(zhí)行完畢

瀏覽:22日期:2022-09-01 10:18:32

在使用多線程的時(shí)候有時(shí)候我們會(huì)使用 java.util.concurrent.Executors的線程池,當(dāng)多個(gè)線程異步執(zhí)行的時(shí)候,我們往往不好判斷是否線程池中所有的子線程都已經(jīng)執(zhí)行完畢,但有時(shí)候這種判斷卻很有用,例如我有個(gè)方法的功能是往一個(gè)文件異步地寫入內(nèi)容,我需要在所有的子線程寫入完畢后在文件末尾寫“---END---”及關(guān)閉文件流等,這個(gè)時(shí)候我就需要某個(gè)標(biāo)志位可以告訴我是否線程池中所有的子線程都已經(jīng)執(zhí)行完畢,我使用這種方式來(lái)判斷。

public class MySemaphore { public static void main(String[] args) throws IOException, InterruptedException { final File stream = new File('c:tempstonefengstream.txt'); final OutputStream os = new FileOutputStream(stream); final OutputStreamWriter writer = new OutputStreamWriter(os); final Semaphore semaphore = new Semaphore(10); ExecutorService exec = Executors.newCachedThreadPool(); final long start = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { final int num = i; Runnable task = new Runnable() {@Overridepublic void run() { try { semaphore.acquire(); writer.write(String.valueOf(num)+'n'); semaphore.release(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); }} }; exec.submit(task); } exec.shutdown(); while(true){ if(exec.isTerminated()){writer.write('---END---n');writer.close();System.out.println('所有的子線程都結(jié)束了!');break; } Thread.sleep(1000); } final long end = System.currentTimeMillis(); System.out.println((end-start)/1000); }}

當(dāng)調(diào)用ExecutorService.shutdown方法的時(shí)候,線程池不再接收任何新任務(wù),但此時(shí)線程池并不會(huì)立刻退出,直到添加到線程池中的任務(wù)都已經(jīng)處理完成,才會(huì)退出。在調(diào)用shutdown方法后我們可以在一個(gè)死循環(huán)里面用isTerminated方法判斷是否線程池中的所有線程已經(jīng)執(zhí)行完畢,如果子線程都結(jié)束了,我們就可以做關(guān)閉流等后續(xù)操作了。

判斷線程池中的線程是否全部執(zhí)行完畢的另外一種解決方案則是使用閉鎖(CountDownLatch)來(lái)實(shí)現(xiàn),CountDownLatch是一種靈活的閉鎖實(shí)現(xiàn),它可以使一個(gè)或多個(gè)線程等待一組事件發(fā)生。閉鎖狀態(tài)包括一個(gè)計(jì)數(shù)器,該計(jì)數(shù)器被初始化為一個(gè)正數(shù),表示需要等待的事件數(shù)量。countDown方法遞減計(jì)數(shù)器,表示有一個(gè)事件已經(jīng)發(fā)生了,而await方法等待計(jì)數(shù)器達(dá)到零,即表示需要等待的事情都已經(jīng)發(fā)生。可以使用閉鎖來(lái)這樣設(shè)計(jì)程序達(dá)到目的:

public class CountDownLatchApproach { public static void main(String[] args) throws IOException, InterruptedException { final int nThreads = 10; final CountDownLatch endGate = new CountDownLatch(nThreads); final File stream = new File('c:tempstonefengstream.txt'); final OutputStream os = new FileOutputStream(stream); final OutputStreamWriter writer = new OutputStreamWriter(os); ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < nThreads; i++) { final int num = i; Runnable task = new Runnable() {@Overridepublic void run() { try { writer.write(String.valueOf(num)+'n'); } catch (IOException e) { e.printStackTrace(); } finally { endGate.countDown(); }} }; exec.submit(task); } endGate.await(); writer.write('---END---n'); writer.close(); }}

這種解決方案雖然可以達(dá)到目的但是性能差到?jīng)]朋友,我更傾向于使用第一種方案。

現(xiàn)在我們有了更優(yōu)雅的第三種方案,它的執(zhí)行性能也不錯(cuò)。

public class MySemaphore { public static void main(String[] args) throws IOException, InterruptedException { final File stream = new File('c:tempstonefengstream.txt'); final OutputStream os = new FileOutputStream(stream); final OutputStreamWriter writer = new OutputStreamWriter(os); final Semaphore semaphore = new Semaphore(10); ExecutorService exec = Executors.newCachedThreadPool(); final long start = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { final int num = i; Runnable task = new Runnable() {@Overridepublic void run() { try { semaphore.acquire(); writer.write(String.valueOf(num)+'n'); semaphore.release(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); }} }; exec.submit(task); } exec.shutdown(); exec.awaitTermination(1, TimeUnit.HOURS); writer.write('---END---n'); writer.close(); System.out.println('ËùÓеÄ×ÓÏ̶߳¼½áÊøÁË£¡'); final long end = System.currentTimeMillis(); System.out.println((end-start)/1000); }}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 亚欧色 | 日韩精品乱码久久久久久 | 午夜精品一区二区三区视频 | 中文字幕亚洲欧美日韩 | 中文字幕av一区二区三区谷原希美 | 最近日本中文字幕 | 亚洲色图清纯唯美 | 天天干一干 | 欧美影院一区 | 日韩一级黄色大片 | 亚洲婷婷av | 午夜激情在线观看 | 亚洲伊人影院 | 国产成人亚洲综合a∨婷婷 91亚洲精品在线观看 | 天天人人| 婷婷激情五月综合 | 亚洲成熟少妇视频在线观看 | 欧美精品一二 | 日本欧美精品 | 视色,视色影院,视色影库,视色网 | 国产精品婷婷 | 精品国产黄色 | 久久裸体视频 | 天天摸天天操天天干 | 欧美午夜精品一区二区三区 | 好吊色网站 | 毛片的网址 | 在线一二区 | 日本中文字幕在线观看视频 | 国产成人综合网 | 亚洲第一区在线 | 婷婷丁香综合 | 久热中文字幕 | 丁香婷婷久久久综合精品国产 | 蜜臀久久99精品久久久久宅男 | 黄色免费录像 | 婷婷综合五月 | 色噜噜日韩精品欧美一区二区 | 亚洲男人天堂2023 | 成年人晚上看的视频 | 直接看毛片 |