我們都知道,在JDK1.5之前,java中要進(jìn)行業(yè)務(wù)并發(fā)時(shí),通常需要有程序員獨(dú)立完成代碼實(shí)現(xiàn),當(dāng)然也有一些開源的框架提供了這些功能,但是這些依然沒有JDK自帶的功能使用起來方便。而當(dāng)針對(duì)高質(zhì)量Java多線程并發(fā)程序設(shè)計(jì)時(shí),為防止死蹦等現(xiàn)象的出現(xiàn),比如使用java之前的wait()、notify()和synchronized等,每每需要考慮性能、死鎖、公平性、資源管理以及如何避免線程安全性方面帶來的危害等諸多因素,往往會(huì)采用一些較為復(fù)雜的安全策略,加重了程序員的開發(fā)負(fù)擔(dān).萬(wàn)幸的是,在JDK1.5出現(xiàn)之后,Sun大神(Doug Lea)終于為我們這些可憐的小程序員推出了java.util.concurrent工具包以簡(jiǎn)化并發(fā)完成。開發(fā)者們借助于此,將有效的減少競(jìng)爭(zhēng)條件(race conditions)和死鎖線程。concurrent包很好的解決了這些問題,為我們提供了更實(shí)用的并發(fā)程序模型。
Executor :具體Runnable任務(wù)的執(zhí)行者。 ExecutorService :一個(gè)線程池管理者,其實(shí)現(xiàn)類有多種。我們能把Runnable,Callable提交到池中讓其調(diào)度。 Semaphore :一個(gè)計(jì)數(shù)信號(hào)量 ReentrantLock :一個(gè)可重入的互斥鎖定 Lock,功能類似synchronized,但要強(qiáng)大的多。 Future :是與Runnable,Callable進(jìn)行交互的接口,比如一個(gè)線程執(zhí)行結(jié)束后取返回的結(jié)果等等,還提供了cancel終止線程。 BlockingQueue :阻塞隊(duì)列。 CompletionService : ExecutorService的擴(kuò)展,可以獲得線程執(zhí)行結(jié)果的 CountDownLatch :一個(gè)同步輔助類,在完成一組正在其他線程中執(zhí)行的操作之前,它允許一個(gè)或多個(gè)線程一直等待。 CyclicBarrier :一個(gè)同步輔助類,它允許一組線程互相等待,直到到達(dá)某個(gè)公共屏障點(diǎn) Future :Future 表示異步計(jì)算的結(jié)果。 ScheduledExecutorService :一個(gè) ExecutorService,可安排在給定的延遲后運(yùn)行或定期執(zhí)行的命令。
newFixedThreadPool(固定大小線程池):創(chuàng)建一個(gè)可重用固定線程集合的線程池,以共享的無(wú)界隊(duì)列方式來運(yùn)行這些線程(只有要請(qǐng)求的過來,就會(huì)在一個(gè)隊(duì)列里等待執(zhí)行)。如果在關(guān)閉前的執(zhí)行期間由于失敗而導(dǎo)致任何線程終止,那么一個(gè)新線程將代替它執(zhí)行后續(xù)的任務(wù)(如果需要)。
newCachedThreadPool(無(wú)界線程池,可以進(jìn)行自動(dòng)線程回收):創(chuàng)建一個(gè)可根據(jù)需要?jiǎng)?chuàng)建新線程的線程池,但是在以前構(gòu)造的線程可用時(shí)將重用它們。對(duì)于執(zhí)行很多短期異步任務(wù)的程序而言,這些線程池通常可提高程序性能。調(diào)用 execute 將重用以前構(gòu)造的線程(如果線程可用)。如果現(xiàn)有線程沒有可用的,則創(chuàng)建一個(gè)新線程并添加到池中。終止并從緩存中移除那些已有 60 秒鐘未被使用的線程。因此,長(zhǎng)時(shí)間保持空閑的線程池不會(huì)使用任何資源。注意,可以使用 ThreadPoolExecutor 構(gòu)造方法創(chuàng)建具有類似屬性但細(xì)節(jié)不同(例如超時(shí)參數(shù))的線程池。
newSingleThreadExecutor(單個(gè)后臺(tái)線程):創(chuàng)建一個(gè)使用單個(gè) worker 線程的 Executor,以無(wú)界隊(duì)列方式來運(yùn)行該線程。(注意,如果因?yàn)樵陉P(guān)閉前的執(zhí)行期間出現(xiàn)失敗而終止了此單個(gè)線程,那么如果需要,一個(gè)新線程將代替它執(zhí)行后續(xù)的任務(wù))。可保證順序地執(zhí)行各個(gè)任務(wù),并且在任意給定的時(shí)間不會(huì)有多個(gè)線程是活動(dòng)的。與其他等效的 newFixedThreadPool(1) 不同,可保證無(wú)需重新配置此方法所返回的執(zhí)行程序即可使用其他的線程。
public class Test { public static void main(String[] args) { ExecutorService service = Executors.newFixedThreadPool(4); for (int i = 0 ; i < 5;i++){ service.execute(new WorkerExecutor(i)); } }}class WorkerExecutor extends Thread{ PRivate int mIndex; public WorkerExecutor(int index){ this.mIndex = index; } @Override public void run() { super.run(); try { System.out.println("Worker:["+this.mIndex+"]--->start...."); Thread.sleep((int)(Math.random()*10000)); System.out.println("Worker:["+this.mIndex+"]--->end...."); } catch (InterruptedException e) { e.printStackTrace(); } }}打印如下:
Worker:[1]--->start....Worker:[0]--->start....Worker:[2]--->start....Worker:[3]--->start....Worker:[1]--->end....Worker:[4]--->start....Worker:[0]--->end....Worker:[2]--->end....Worker:[4]--->end....Worker:[3]--->end....上面代碼只能執(zhí)行4個(gè)線程,如果4個(gè)線程都有任務(wù)那么之后的只能進(jìn)入隊(duì)列等待也就是說,我們將所有的線程提交后,線程池會(huì)等待執(zhí)行完最后shutdown。我們也會(huì)發(fā)現(xiàn),提交的線程被放到一個(gè)“無(wú)界隊(duì)列里”。這是一個(gè)有序隊(duì)列(BlockingQueue,這個(gè)下面會(huì)說到)。另外它使用了Executors的靜態(tài)函數(shù)生成一個(gè)固定的線程池,顧名思義,線程池的線程是不會(huì)釋放的,即使它是Idle。這就會(huì)產(chǎn)生性能問題,比如如果線程池的大小為200,當(dāng)全部使用完畢后,所有的線程會(huì)繼續(xù)留在池中,相應(yīng)的內(nèi)存和線程切換(while(true)+sleep循環(huán))都會(huì)增加。如果要避免這個(gè)問題,就必須直接使用ThreadPoolExecutor()來構(gòu)造。可以像通用的線程池一樣設(shè)置“最大線程數(shù)”、“最小線程數(shù)”和“空閑線程keepAlive的時(shí)間”。
int corePoolSize = 4; int maximumPoolSize = 10; long keepAliveTime = 60; ArrayBlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(maximumPoolSize); ExecutorService service = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, blockingQueue); for (int i = 0; i < 4; i++) { service.execute(new WorkerExecutor(i)); }(ThreadPoolExecutor 詳細(xì)介紹:http://blog.csdn.net/wangwenhui11/article/details/6760474)
支持兩個(gè)附加操作的 Queue,這兩個(gè)操作是:檢索元素時(shí)等待隊(duì)列變?yōu)榉强眨约按鎯?chǔ)元素時(shí)等待空間變得可用。BlockingQueue 不接受 null 元素。試圖 add、put 或 offer 一個(gè) null 元素時(shí),某些實(shí)現(xiàn)會(huì)拋出 NullPointerException。null 被用作指示 poll 操作失敗的警戒值。BlockingQueue 可以是限定容量的。它在任意給定時(shí)間都可以有一個(gè) remainingCapacity,超出此容量,便無(wú)法無(wú)阻塞地 put 額外的元素。沒有任何內(nèi)部容量約束的 BlockingQueue 總是報(bào)告 Integer.MAX_VALUE 的剩余容量。BlockingQueue 實(shí)現(xiàn)主要用于生產(chǎn)者-使用者隊(duì)列,但它另外還支持 Collection 接口。因此,舉例來說,使用 remove(x) 從隊(duì)列中移除任意一個(gè)元素是有可能的。然而,這種操作通常不會(huì)有效執(zhí)行,只能有計(jì)劃地偶爾使用,比如在取消排隊(duì)信息時(shí)。BlockingQueue 實(shí)現(xiàn)是線程安全的。所有排隊(duì)方法都可以使用內(nèi)部鎖定或其他形式的并發(fā)控制來自動(dòng)達(dá)到它們的目的。然而,大量的 Collection 操作(addAll、containsAll、retainAll 和 removeAll)沒有必要自動(dòng)執(zhí)行,除非在實(shí)現(xiàn)中特別說明。因此,舉例來說,在只添加了c中的一些元素后,addAll(c) 有可能失敗(拋出一個(gè)異常)。BlockingQueue 實(shí)質(zhì)上不支持使用任何一種“close”或“shutdown”操作來指示不再添加任何項(xiàng)。這種功能的需求和使用有依賴于實(shí)現(xiàn)的傾向。例如,一種常用的策略是:對(duì)于生產(chǎn)者,插入特殊的 end-of-stream 或 poison 對(duì)象,并根據(jù)使用者獲取這些對(duì)象的時(shí)間來對(duì)它們進(jìn)行解釋。
public class Test { public static void main(String[] args) { ExecutorService service = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { service.submit(new WorkerExecutor(i)); } Thread thread = new Thread() { @Override public void run() { super.run(); try { while (true) { Thread.sleep((int) (Math.random() * 1000)); if (WorkerExecutor.mBlockingQueue.isEmpty()) break; String str = WorkerExecutor.mBlockingQueue.take(); System.out.println(str + " has take!"); } } catch (InterruptedException e) { e.printStackTrace(); } } }; service.submit(thread); service.shutdown(); }}class WorkerExecutor extends Thread { public static BlockingQueue<String> mBlockingQueue = new LinkedBlockingQueue<>(3); private int mIndex; public WorkerExecutor(int index) { this.mIndex = index; } @Override public void run() { super.run(); try { mBlockingQueue.put(String.valueOf(mIndex)); System.out.println("{" + this.mIndex + "} in queue!"); } catch (InterruptedException e) { e.printStackTrace(); } }}執(zhí)行結(jié)果如下:
{1} in queue!{2} in queue!{0} in queue!0 has take!{3} in queue!1 has take!{4} in queue!2 has take!{5} in queue!3 has take!{6} in queue!4 has take!{7} in queue!5 has take!{8} in queue!6 has take!{9} in queue!7 has take!8 has take!9 has take!Semaphore是一個(gè)計(jì)數(shù)信號(hào)量,它的本質(zhì)是一個(gè)”共享鎖”。 信號(hào)量維護(hù)了一個(gè)信號(hào)量許可集。線程可以通過調(diào)用acquire()來獲取信號(hào)量的許可;當(dāng)信號(hào)量中有可用的許可時(shí),線程能獲取該許可;否則線程必須等待,直到有可用的許可為止。 線程可以通過release()來釋放它所持有的信號(hào)量許可 Semaphore 通常用于限制可以訪問某些資源(物理或邏輯的)的線程數(shù)目。例如,下面的類使用信號(hào)量控制對(duì)內(nèi)容池的訪問:
public class Test { public static void main(String[] args) { ExecutorService service = Executors.newCachedThreadPool(); Semaphore semaphore = new Semaphore(2); for (int i = 0; i < 10; i++) { service.submit(new WorkerExecutor(i+1,semaphore)); } service.shutdown(); semaphore.acquireUninterruptibly(2);//從此信號(hào)量獲取給定數(shù)目的許可,在提供這些許可前一直將線程阻塞。 System.out.println("全部使用完畢"); semaphore.release(); }}class WorkerExecutor extends Thread { private Semaphore position; private int id; public WorkerExecutor(int id, Semaphore pos) { this.id = id; this.position = pos; } @Override public void run() { super.run(); try { if (position.availablePermits() > 0) {//返回此信號(hào)量中當(dāng)前可用的許可數(shù)。 System.out.println("worker"+this.id + ": 有可用許可"); } else { System.out.println("worker"+this.id + ": 沒有可用許可 等待"); } position.acquire();// 從此信號(hào)量獲取一個(gè)許可,在提供一個(gè)許可前一直將線程阻塞,否則線程被中斷。 System.out.println("worker"+this.id+":獲得許可"); Thread.sleep((int)(Math.random()*1000)); System.out.println("worker"+this.id+":使用完畢 釋放許可"); position.release();//釋放一個(gè)許可,將其返回給信號(hào)量 } catch (Exception e) { e.printStackTrace(); } }}運(yùn)行結(jié)果如下:
worker1: 有可用許可worker1:獲得許可worker2: 有可用許可worker2:獲得許可worker3: 沒有可用許可 等待worker4: 沒有可用許可 等待worker5: 沒有可用許可 等待worker6: 沒有可用許可 等待worker7: 沒有可用許可 等待worker8: 沒有可用許可 等待worker9: 沒有可用許可 等待worker10: 沒有可用許可 等待worker2:使用完畢 釋放許可worker3:獲得許可worker1:使用完畢 釋放許可worker4:獲得許可worker3:使用完畢 釋放許可worker5:獲得許可worker4:使用完畢 釋放許可worker6:獲得許可worker6:使用完畢 釋放許可worker7:獲得許可worker5:使用完畢 釋放許可worker8:獲得許可worker7:使用完畢 釋放許可worker9:獲得許可worker9:使用完畢 釋放許可worker10:獲得許可worker8:使用完畢 釋放許可worker10:使用完畢 釋放許可全部使用完畢ReentrantLock可以等同于synchronized使用、但是它比synchronized有更強(qiáng)的功能、可以提供更靈活的鎖機(jī)制、同時(shí)減少死鎖的發(fā)生概率。我們平時(shí)用用synchronized也就夠了、但是要寫好一個(gè)復(fù)雜的多線程系統(tǒng)、為了提供更靈活的同步機(jī)制、就需要用到ReentrantLock了。 ReentrantLock 將由最近成功獲得鎖定,并且還沒有釋放該鎖定的線程所擁有。當(dāng)鎖定沒有被另一個(gè)線程所擁有時(shí),調(diào)用 lock 的線程將成功獲取該鎖定并返回。如果當(dāng)前線程已經(jīng)擁有該鎖定,此方法將立即返回。可以使用 isHeldByCurrentThread() 和 getHoldCount() 方法來檢查此情況是否發(fā)生。 此類的構(gòu)造方法接受一個(gè)可選的公平參數(shù)。當(dāng)設(shè)置為true時(shí),在多個(gè)線程的爭(zhēng)用下,這些鎖定傾向于將訪問權(quán)授予等待時(shí)間最長(zhǎng)的線程。否則此鎖定將無(wú)法保證任何特定訪問順序。與采用默認(rèn)設(shè)置(使用不公平鎖定)相比,使用公平鎖定的程序在許多線程訪問時(shí)表現(xiàn)為很低的總體吞吐量(即速度很慢,常常極其慢),但是在獲得鎖定和保證鎖定分配的均衡性時(shí)差異較小。不過要注意的是,公平鎖定不能保證線程調(diào)度的公平性。因此,使用公平鎖定的眾多線程中的一員可能獲得多倍的成功機(jī)會(huì),這種情況發(fā)生在其他活動(dòng)線程沒有被處理并且目前并未持有鎖定時(shí)。還要注意的是,未定時(shí)的 tryLock 方法并沒有使用公平設(shè)置。因?yàn)榧词蛊渌€程正在等待,只要該鎖定是可用的,此方法就可以獲得成功。
以上幾個(gè)特點(diǎn)使ReentrantLock相比synchronized的鎖粒度更細(xì)、使用起來更靈活、可以滿足更多的功能。 示例如下
public class Test { public static void main(String[] args) { ExecutorService service = Executors.newCachedThreadPool(); TestReentrantLock lock = new TestReentrantLock(); for (int i = 0; i < 10; i++) { service.submit(new WorkerExecutor(i + 1, lock)); } service.shutdown(); }}class WorkerExecutor extends Thread { private TestReentrantLock mLock; private int id; public WorkerExecutor(int id, TestReentrantLock lock) { this.id = id; this.mLock = lock; } @Override public void run() { super.run(); mLock.print(id); }}class TestReentrantLock { private ReentrantLock mLock = new ReentrantLock(); public void print(int id) { try { mLock.lock(); System.out.println(id + "獲得"); Thread.sleep((int) (Math.random() * 1000)); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(id + "釋放"); mLock.unlock(); } }}當(dāng)向Executor提交批處理任務(wù)時(shí),并且希望在它們完成后獲得結(jié)果,如果用FutureTask,你可以循環(huán)獲取task,并用future.get()去獲取結(jié)果,但是如果這個(gè)task沒有完成,你就得阻塞在這里,這個(gè)實(shí)效性不高,其實(shí)在很多場(chǎng)合,其實(shí)你拿第一個(gè)任務(wù)結(jié)果時(shí),此時(shí)結(jié)果并沒有生成并阻塞,其實(shí)在阻塞在第一個(gè)任務(wù)時(shí),第二個(gè)task的任務(wù)已經(jīng)早就完成了,顯然這種情況用future task不合適的,效率也不高。
自己維護(hù)list和CompletionService的區(qū)別:
從list中遍歷的每個(gè)Future對(duì)象并不一定處于完成狀態(tài),這時(shí)調(diào)用get()方法就會(huì)被阻塞住,如果系統(tǒng)是設(shè)計(jì)成每個(gè)線程完成后就能根據(jù)其結(jié)果繼續(xù)做后面的事,這樣對(duì)于處于list后面的但是先完成的線程就會(huì)增加了額外的等待時(shí)間。CompletionService采取的是BlockingQueue無(wú)界隊(duì)列來管理Future。則有一個(gè)線程執(zhí)行完畢把返回結(jié)果放到BlockingQueue里面。就可以通過completionServcie.take().get()取出結(jié)果。方法區(qū)別:
take 方獲取并移除表示下一個(gè)已完成任務(wù)的 Future,如果目前不存在這樣的任務(wù),則等待。<如果需要用到返回值建議用take>poll 獲取并移除表示下一個(gè)已完成任務(wù)的 Future,如果不存在這樣的任務(wù),則返回null。以下是jdk關(guān)于CompletionService的簡(jiǎn)介:
public interface CompletionService將生產(chǎn)新的異步任務(wù)與使用已完成任務(wù)的結(jié)果分離開來的服務(wù)。生產(chǎn)者 submit 執(zhí)行的任務(wù)。使用者 take 已完成的任務(wù),并按照完成這些任務(wù)的順序處理它們的結(jié)果。例如,CompletionService 可以用來管理異步 IO ,執(zhí)行讀操作的任務(wù)作為程序或系統(tǒng)的一部分提交,然后,當(dāng)完成讀操作時(shí),會(huì)在程序的不同部分執(zhí)行其他操作,執(zhí)行操作的順序可能與所請(qǐng)求的順序不同。通常,CompletionService 依賴于一個(gè)單獨(dú)的 Executor 來實(shí)際執(zhí)行任務(wù),在這種情況下,CompletionService 只管理一個(gè)內(nèi)部完成隊(duì)列。ExecutorCompletionService 類提供了此方法的一個(gè)實(shí)現(xiàn)。 內(nèi)存一致性效果:線程中向 CompletionService 提交任務(wù)之前的操作 happen-before 該任務(wù)執(zhí)行的操作,后者依次 happen-before 緊跟在從對(duì)應(yīng) take() 成功返回的操作。 public class Test { public static class Task implements Callable<Integer> { private int i; public Task(int i) { this.i = i; } @Override public Integer call() throws Exception { Thread.sleep(new Random().nextInt(5000)); System.out.println(Thread.currentThread().getName() + " " + i); return i; } } public void run() { ExecutorService pool = Executors.newFixedThreadPool(10); CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(pool); try { for (int i = 0; i < 10; i++) completionService.submit(new Task(i)); for (int i = 0; i < 10; i++) System.out.println(completionService.take().get()); } catch (Exception e) { e.printStackTrace(); } finally { pool.shutdown(); } } public static void main(String[] args) throws Exception { new Test().run(); }}運(yùn)行結(jié)果
pool-1-thread-6 55pool-1-thread-7 66pool-1-thread-3 22pool-1-thread-8 77pool-1-thread-1 00pool-1-thread-2 11pool-1-thread-9 88pool-1-thread-5 44pool-1-thread-10 99pool-1-thread-4 33從結(jié)果中不難看出。只要有一個(gè)線程執(zhí)行完畢后,主程序就立馬獲取結(jié)果。
一個(gè)同步輔助類,在完成一組正在其他線程中執(zhí)行的操作之前,它允許一個(gè)或多個(gè)線程一直等待。用給定的計(jì)數(shù) 初始化 CountDownLatch。由于調(diào)用了 countDown() 方法,所以在當(dāng)前計(jì)數(shù)到達(dá)零之前,await 方法會(huì)一直受阻塞。之后,會(huì)釋放所有等待的線程,await 的所有后續(xù)調(diào)用都將立即返回。這種現(xiàn)象只出現(xiàn)一次——計(jì)數(shù)無(wú)法被重置。如果需要重置計(jì)數(shù),請(qǐng)考慮使用 CyclicBarrier。CountDownLatch 是一個(gè)通用同步工具,它有很多用途。將計(jì)數(shù) 1 初始化的 CountDownLatch 用作一個(gè)簡(jiǎn)單的開/關(guān)鎖存器,或入口:在通過調(diào)用 countDown() 的線程打開入口前,所有調(diào)用 await 的線程都一直在入口處等待。用 N 初始化的 CountDownLatch 可以使一個(gè)線程在 N 個(gè)線程完成某項(xiàng)操作之前一直等待,或者使其在某項(xiàng)操作完成 N 次之前一直等待。CountDownLatch 的一個(gè)有用特性是,它不要求調(diào)用 countDown 方法的線程等到計(jì)數(shù)到達(dá)零時(shí)才繼續(xù),而在所有線程都能通過之前,它只是阻止任何線程繼續(xù)通過一個(gè) await。
public class Test { public static void main(String[] args) throws Exception { VideoConference conference = new VideoConference(10); Thread threadConderence = new Thread(conference); threadConderence.start(); for (int i = 0; i < 10; i++) { Participant participant = new Participant(conference, "Participant " + i); Thread thread = new Thread(participant); thread.start(); } }}class VideoConference implements Runnable { private final CountDownLatch mCountDownLatch; public VideoConference(int number) { mCountDownLatch = new CountDownLatch(number); } public void arrive(String name) { System.out.println(name + " has arrived."); mCountDownLatch.countDown(); System.out.println("VideoConference:Waiting for " + mCountDownLatch.getCount()); } @Override public void run() { System.out.println("VideoConference:Initialization:" + mCountDownLatch.getCount()); try { mCountDownLatch.await(); System.out.printf("VideoConference: All the participants have come/n"); System.out.printf("VideoConference: Let's start.../n"); } catch (InterruptedException e) { e.printStackTrace(); } }}class Participant implements Runnable { private VideoConference mVideoConference; private String name; public Participant(VideoConference conference, String name) { this.mVideoConference = conference; this.name = name; } @Override public void run() { long duration = (long) (Math.random() * 10); try { TimeUnit.SECONDS.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } mVideoConference.arrive(name); }}運(yùn)行結(jié)果如下
VideoConference:Initialization:10Participant 0 has arrived.VideoConference:Waiting for 9Participant 9 has arrived.VideoConference:Waiting for 8Participant 3 has arrived.Participant 8 has arrived.VideoConference:Waiting for 7VideoConference:Waiting for 6Participant 7 has arrived.VideoConference:Waiting for 5Participant 4 has arrived.VideoConference:Waiting for 4Participant 5 has arrived.VideoConference:Waiting for 3Participant 2 has arrived.VideoConference:Waiting for 2Participant 6 has arrived.VideoConference:Waiting for 1Participant 1 has arrived.VideoConference:Waiting for 0VideoConference: All the participants have comeVideoConference: Let's start...CountDownLatch最重要的方法是countDown()和await(),前者主要是倒數(shù)一次,后者是等待倒數(shù)到0,如果沒有到達(dá)0,就只有阻塞等待了。
一個(gè)同步輔助類,它允許一組線程互相等待,直到到達(dá)某個(gè)公共屏障點(diǎn) (common barrier point)。在涉及一組固定大小的線程的程序中,這些線程必須不時(shí)地互相等待,此時(shí) CyclicBarrier 很有用。因?yàn)樵?barrier 在釋放等待線程后可以重用,所以稱它為循環(huán) 的 barrier。CyclicBarrier 支持一個(gè)可選的 Runnable 命令,在一組線程中的最后一個(gè)線程到達(dá)之后(但在釋放所有線程之前),該命令只在每個(gè)屏障點(diǎn)運(yùn)行一次。若在繼續(xù)所有參與線程之前更新共享狀態(tài),此屏障操作 很有用。 示例用法:下面是一個(gè)在并行分解設(shè)計(jì)中使用 barrier 的例子,很經(jīng)典的旅行團(tuán)例子:
public class Test { // 徒步需要的時(shí)間: Shenzhen, Guangzhou, Shaoguan, Changsha, Wuhan private static int[] timeWalk = {5, 8, 15, 15, 10}; // 自駕游 private static int[] timeSelf = {1, 3, 4, 4, 5}; // 旅游大巴 private static int[] timeBus = {2, 4, 6, 6, 7}; static String now() { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); return sdf.format(new Date()) + ": "; } static class Tour implements Runnable { private int[] times; private CyclicBarrier barrier; private String tourName; public Tour(CyclicBarrier barrier, String tourName, int[] times) { this.times = times; this.tourName = tourName; this.barrier = barrier; } @Override public void run() { try { Thread.sleep(times[0] * 1000); System.out.println(now() + tourName + " Reached Shenzhen"); barrier.await(); Thread.sleep(times[1] * 1000); System.out.println(now() + tourName + " Reached Guangzhou"); barrier.await(); Thread.sleep(times[2] * 1000); System.out.println(now() + tourName + " Reached Shaoguan"); barrier.await(); Thread.sleep(times[3] * 1000); System.out.println(now() + tourName + " Reached Changsha"); barrier.await(); Thread.sleep(times[4] * 1000); System.out.println(now() + tourName + " Reached Wuhan"); barrier.await(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { // 三個(gè)旅行團(tuán) CyclicBarrier barrier = new CyclicBarrier(3); ExecutorService exec = Executors.newFixedThreadPool(3); exec.submit(new Tour(barrier, "WalkTour", timeWalk)); exec.submit(new Tour(barrier, "SelfTour", timeSelf)); //當(dāng)我們把下面的這段代碼注釋后,會(huì)發(fā)現(xiàn),程序阻塞了,無(wú)法繼續(xù)運(yùn)行下去。 exec.submit(new Tour(barrier, "BusTour", timeBus)); exec.shutdown(); }}運(yùn)行結(jié)果如下:
15:29:38: SelfTour Reached Shenzhen15:29:38: BusTour Reached Shenzhen15:29:41: WalkTour Reached Shenzhen15:29:44: SelfTour Reached Guangzhou15:29:45: BusTour Reached Guangzhou15:29:49: WalkTour Reached Guangzhou15:29:53: SelfTour Reached Shaoguan15:29:55: BusTour Reached Shaoguan15:30:04: WalkTour Reached Shaoguan15:30:08: SelfTour Reached Changsha15:30:10: BusTour Reached Changsha15:30:19: WalkTour Reached Changsha15:30:24: SelfTour Reached Wuhan15:30:26: BusTour Reached Wuhan15:30:29: WalkTour Reached WuhanCyclicBarrier最重要的屬性就是參與者個(gè)數(shù),另外最重要方法是await()。當(dāng)所有線程都調(diào)用了await()后,就表示這些線程都可以繼續(xù)執(zhí)行,否則就會(huì)等待。
Future 表示異步計(jì)算的結(jié)果。它提供了檢查計(jì)算是否完成的方法,以等待計(jì)算的完成,并檢索計(jì)算的結(jié)果。計(jì)算完成后只能使用 get 方法來檢索結(jié)果,如有必要,計(jì)算完成前可以阻塞此方法。取消則由 cancel 方法來執(zhí)行。還提供了其他方法,以確定任務(wù)是正常完成還是被取消了。一旦計(jì)算完成,就不能再取消計(jì)算。如果為了可取消性而使用 Future但又不提供可用的結(jié)果,則可以聲明 Future
一個(gè) ExecutorService,可安排在給定的延遲后運(yùn)行或定期執(zhí)行的命令。schedule 方法使用各種延遲創(chuàng)建任務(wù),并返回一個(gè)可用于取消或檢查執(zhí)行的任務(wù)對(duì)象。scheduleAtFixedRate 和 scheduleWithFixedDelay 方法創(chuàng)建并執(zhí)行某些在取消前一直定期運(yùn)行的任務(wù)。用Executor.execute(java.lang.Runnable) 和 ExecutorService 的 submit 方法所提交的命令,通過所請(qǐng)求的 0 延遲進(jìn)行安排。schedule 方法中允許出現(xiàn) 0 和負(fù)數(shù)延遲(但不是周期),并將這些視為一種立即執(zhí)行的請(qǐng)求。所有的 schedule 方法都接受相對(duì) 延遲和周期作為參數(shù),而不是絕對(duì)的時(shí)間或日期。將以 Date 所表示的絕對(duì)時(shí)間轉(zhuǎn)換成要求的形式很容易。例如,要安排在某個(gè)時(shí)刻以后的日期運(yùn)行,可以使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。但是要注意,由于網(wǎng)絡(luò)時(shí)間同步協(xié)議、時(shí)鐘漂移或其他因素的存在,因此相對(duì)延遲的期滿日期不必與啟用任務(wù)的當(dāng)前 Date 相符。Executors 類為此包中所提供的 ScheduledExecutorService 實(shí)現(xiàn)提供了便捷的工廠方法。
public class Test { public static void main(String[] args) { final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); final Runnable beeper = new Runnable() { int count = 0; @Override public void run() { System.out.println(new Date() + " beep " + (++count)); } }; // 1秒鐘后運(yùn)行,并每隔2秒運(yùn)行一次 final ScheduledFuture beeperHandle = scheduler.scheduleWithFixedDelay(beeper, 2, 5, TimeUnit.SECONDS); // 2秒鐘后運(yùn)行,并每次在上次任務(wù)運(yùn)行完后等待5秒后重新運(yùn)行 final ScheduledFuture beeperHandle2 = scheduler.scheduleWithFixedDelay(beeper, 2, 5, TimeUnit.SECONDS); // 30秒后結(jié)束關(guān)閉任務(wù),并且關(guān)閉Scheduler scheduler.schedule(new Runnable() { @Override public void run() { beeperHandle.cancel(true); beeperHandle2.cancel(true); scheduler.shutdown(); } }, 30, TimeUnit.SECONDS); }}運(yùn)行結(jié)果:
Sat Feb 04 16:02:20 CST 2017 beep 2Sat Feb 04 16:02:20 CST 2017 beep 1Sat Feb 04 16:02:25 CST 2017 beep 3Sat Feb 04 16:02:25 CST 2017 beep 4Sat Feb 04 16:02:30 CST 2017 beep 5Sat Feb 04 16:02:30 CST 2017 beep 6Sat Feb 04 16:02:35 CST 2017 beep 7Sat Feb 04 16:02:35 CST 2017 beep 8Sat Feb 04 16:02:40 CST 2017 beep 9Sat Feb 04 16:02:40 CST 2017 beep 10Sat Feb 04 16:02:45 CST 2017 beep 11Sat Feb 04 16:02:45 CST 2017 beep 12以上轉(zhuǎn)自: (http://blog.csdn.net/wulei_longhe/article/details/30032031)(blog.csdn.net/tsyj810883979/article/details/6956290) (http://hyxw5890.VEvb.com/blog/1578597) (https://my.oschina.net/jielucky/blog/158839)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注