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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CountDownLatch应用及原理

發布時間:2024/4/13 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CountDownLatch应用及原理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

正如每個Java文檔所描述的那樣,CountDownLatch是一個同步工具類,它允許一個或多個線程一直等待,直到其他線程的操作執行完后再執行。

CountDownLatch是在java1.5被引入的,跟它一起被引入的并發工具類還有CyclicBarrier、Semaphore、ConcurrentHashMap和BlockingQueue,它們都存在于java.util.concurrent包下。CountDownLatch這個類能夠使一個線程等待其他線程完成各自的工作后再執行。例如,應用程序的主線程希望在負責啟動框架服務的線程已經啟動所有的框架服務之后再執行。

CountDownLatch是通過一個計數器來實現的,計數器的初始值為線程的數量。每當一個線程完成了自己的任務后,計數器的值就會減1。當計數器值到達0時,它表示所有的線程已經完成了任務,然后在閉鎖上等待的線程就可以恢復執行任務。

CountDownLatch的方法

//可中斷等待 public void await() throws InterruptedException; //等待直到超時或者中斷 public boolean await(long timeout, TimeUnit unit)throws InterruptedException; //計數器減1 public void countDown();

CountDownLatch的應用

控制線程同時執行

package main.java.study;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {

?? ?public class MapOper implements Runnable {
?? ??? ?
?? ???? CountDownLatch latch ;

?? ???? public MapOper(CountDownLatch latch) {
?? ???????? this.latch = latch;
?? ???? }

?? ???? public void run() {
?? ???????? try {
?? ??????? ??? ?
?? ??????? ??? ? SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
?? ???????????? System.out.println(Thread.currentThread().getName() + "start:" + df.format(new Date()));
?? ??????????? ?
?? ???????????? latch.await();
?? ???????????? System.out.println(Thread.currentThread().getName() + "work:" + df.format(new Date()));
?? ??????????? ?
?? ???????? } catch (InterruptedException e) {
?? ???????????? // TODO Auto-generated catch block
?? ???????????? e.printStackTrace();
?? ???????? }
?? ???????? System.out.println(Thread.currentThread().getName()+" Sync Started!");
?? ???? }
?? ?}

?? ?
?? ?public static void main(String[] args) throws InterruptedException {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?CountDownLatchTest test = new CountDownLatchTest();
?? ??? ?
?? ??? ?CountDownLatch latch = new CountDownLatch(1);
?? ??? ?
?? ??? ?Thread t1 = new Thread(test.new MapOper(latch));
?? ??? ?Thread t2 = new Thread(test.new MapOper(latch));
?? ??? ?Thread t3 = new Thread(test.new MapOper(latch));
?? ??? ?Thread t4 = new Thread(test.new MapOper(latch));
?? ??? ?
?? ??? ?t1.setName("Thread1");
?? ??? ?t2.setName("Thread2");
?? ??? ?t3.setName("Thread3");
?? ??? ?t4.setName("Thread4");
?? ??? ?
?? ??? ?t1.start();
?? ??? ?Thread.sleep(1500);
?? ??? ?t2.start();
?? ??? ?Thread.sleep(1500);
?? ??? ?t3.start();
?? ??? ?Thread.sleep(1500);
?? ??? ?t4.start();
?? ??? ?
?? ??? ?
?? ??? ?System.out.println("thread already start, sleep for a while...");
?? ??? ?
?? ??? ?Thread.sleep(1000);
?? ??? ?latch.countDown();

?? ?}
?? ?
?? ?

}

執行結果:線程在不同時刻啟動,但是等待后在同一時刻工作。

Thread1start:2019-05-25 13:24:01
Thread2start:2019-05-25 13:24:02
Thread3start:2019-05-25 13:24:04
thread already start, sleep for a while...
Thread4start:2019-05-25 13:24:05
Thread1work:2019-05-25 13:24:06
Thread1 Sync Started!
Thread2work:2019-05-25 13:24:06
Thread2 Sync Started!
Thread3work:2019-05-25 13:24:06
Thread3 Sync Started!
Thread4work:2019-05-25 13:24:06
Thread4 Sync Started!

CountDownLatch的原理

調用countDownLatch.await()方法的線程,它會處于掛起狀態,直到所有的線程都執行完countDownLatch.countDown方法,最終將計數器減為0,才會被喚醒繼續執行。

CountDownLatch的源碼

private static final class Sync extends AbstractQueuedSynchronizer {private static final long serialVersionUID = 4982264981922014374L;Sync(int count) {setState(count);}int getCount() {return getState();}protected int tryAcquireShared(int acquires) {//只有state為0時,才返回1,其他返回-1,小于0則 表示acquire失敗。return (getState() == 0) ? 1 : -1;}protected boolean tryReleaseShared(int releases) {// Decrement count; signal when transition to zerofor (;;) {int c = getState();if (c == 0)return false;int nextc = c-1;//每次state減1if (compareAndSetState(c, nextc))return nextc == 0;}}} public void await() throws InterruptedException {sync.acquireSharedInterruptibly(1);}//countDown,release(1)public void countDown() {sync.releaseShared(1);}

AbstractQueuedSynchronizer相關源碼參考:https://blog.csdn.net/demon7552003/article/details/90105335

總結

以上是生活随笔為你收集整理的CountDownLatch应用及原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。