java中常用的並發工具類

來源:互聯網
上載者:User

標籤:pre   tst   線程並發   int   title   static   交換   資料   exchanger   

· 1. 等待多線程完成的CountDownLatch

建構函式接收一個int類型的參數作為計數器,如果想等待N個點,就傳入N。當調用CountDownLatch的countDown方法時,N就會減一,直至減為零。使用await方法等待,當N的值變為零,執行await的線程繼續執行。

public class CountDownTest {    static CountDownLatch c = new CountDownLatch(2); static ExecutorService pool = Executors.newFixedThreadPool(2); public static void main(String agrs[]){ pool.execute(new Runnable() { public void run() { try { TimeUnit.MILLISECONDS.sleep(3); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("This is A"); c.countDown(); } }); pool.execute(new Runnable() { public void run() { try { TimeUnit.MILLISECONDS.sleep(3); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("This is B"); c.countDown(); } }); try { c.await(); }catch (InterruptedException e){ } System.out.println("This is main"); }}
2. 同步屏障CyclicBarrier

可迴圈使用的屏障。它要做的事情,讓一組線程到達屏障時被阻塞,直到最後一個線程到達屏障時,屏障才會開啟,所有被屏障攔截的線程才會繼續運行。

預設構造方法CyclicBarrier(int parties),其參數表示屏障攔截的線程數,每個線程調用await方法告訴CyclicBarrier已經到達屏障,然後當前線程阻塞。

public class CylicBarrierTest {    static CyclicBarrier c = new CyclicBarrier(2); static ExecutorService pool = Executors.newFixedThreadPool(2); public static void main(String args[]){ pool.execute(new Runnable() { public void run() { System.out.println("this is A"); try { c.await(); System.out.println("this is Aa"); }catch (Exception e){ e.printStackTrace(); } } }); pool.execute(new Runnable() { public void run() { System.out.println("this is B"); try { c.await(); System.out.println("this is Bb"); }catch (Exception e){ e.printStackTrace(); } } }); pool.shutdown(); }}
3. CountDownLatch vs CyclicBarrier

CountDownLatch的計數器只使用一次,而CyclicBarrier的計數器可以使用reset方法重設,重複使用

4. 控制線程並發數的訊號量 Semaphore

控制有限哥線程使用資源。使用方法 Semaphore ss = new Semaphore(N), N表示最大的並發量,每個線程使用資源前調用ss.acquire()方法擷取許可權,使用完後調用ss.release()方法釋放許可權。

5. 線程間的資料交換Exchanger

它提供一個同步點,在這個同步點,兩個線程可以交換彼此的資料。

public class ExchangeTest {    private static final Exchanger<String> exgr = new Exchanger<String>(); private static ExecutorService pool = Executors.newFixedThreadPool(2); public static void main(String args[]){ pool.execute(new Runnable() { public void run() { String A = "銀行流水A"; try { A = exgr.exchange(A); System.out.println("A 當前的值:" + A); }catch (InterruptedException e){ } } }); pool.execute(new Runnable() { public void run() { String B = "銀行流水B"; try { B = exgr.exchange(B); System.out.println("B 當前的值:" + B); }catch (InterruptedException e){ } } }); pool.shutdown(); }

java中常用的並發工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.