1. 程式人生 > >Java之CountDownLatch(倒數鎖存器)使用

Java之CountDownLatch(倒數鎖存器)使用

<span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">public</span> <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">class</span> CountDownLatchTest {

    <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// 模擬了100米賽跑,10名選手已經準備就緒,只等裁判一聲令下。當所有人都到達終點時,比賽結束。</span>
    <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">public</span> <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">static</span> <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">void</span> main(String[] args) <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">throws</span> InterruptedException {

        <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// 開始的倒數鎖 </span>
        <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">final</span> CountDownLatch begin = <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">new</span> CountDownLatch(1);  

        <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// 結束的倒數鎖 </span>
        <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">final</span> CountDownLatch end = <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">new</span> CountDownLatch(10);  

        <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// 十名選手 </span>
        <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">final</span> ExecutorService exec = Executors.newFixedThreadPool(10);  

        <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">for</span> (<span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">int</span> index = 0; index < 10; index++) {
            <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">final</span> <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">int</span> NO = index + 1;  
            Runnable run = <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">new</span> Runnable() {
                <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">public</span> <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">void</span> run() {  
                    <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">try</span> {  
                        <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// 如果當前計數為零,則此方法立即返回。</span>
                        <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// 等待</span>
                        begin.await();  
                        Thread.sleep((<span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">long</span>) (Math.random() * 10000));  
                        System.out.println(<span class="bh_code_a_Java_string" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(42, 0, 255); ">"No."</span> + NO + <span class="bh_code_a_Java_string" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(42, 0, 255); ">" arrived"</span>);  
                    } <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">catch</span> (InterruptedException e) {  
                    } <span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(127, 0, 85); ">finally</span> {  
                        <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// 每個選手到達終點時,end就減一</span>
                        end.countDown();
                    }  
                }  
            };  
            exec.submit(run);
        }  
        System.out.println(<span class="bh_code_a_Java_string" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(42, 0, 255); ">"Game Start"</span>);  
        <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// begin減一,開始遊戲</span>
        begin.countDown();  
        <span class="bh_code_a_Java_comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(63, 127, 95); ">// 等待end變為0,即所有選手到達終點</span>
        end.await();  
        System.out.println(<span class="bh_code_a_Java_string" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; background-color: transparent; color: rgb(42, 0, 255); ">"Game Over"</span>);  
        exec.shutdown();  
    }
}
5、輸出結果

相關推薦

JavaCountDownLatch(倒數)使用

<span class="bh_code_a_Java_keywords" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right:

java執行緒中CountDownLatch的使用

CountDownLatch類是一個同步計數器,構造時傳入int引數,該引數就是計數器的初始值,每呼叫一次countDown()方法,計數器減1,計數器大於0 時,await()方法會阻塞程式繼續執行。CountDownLatch可以看作是一個倒計數的鎖存器,當計數減至0

java併發程式設計(十)執行緒倒計數CountDownLatch

一、定義一個同步輔助類,在完成一組正在其他執行緒中執行的操作之前,它允許一個或多個執行緒一直等待。用給定的計數 初始化 CountDownLatch。由於呼叫了 countDown() 方法,所以在當前計數到達零之前,await 方法會一直受阻塞。之後,會釋放所有等待的執行緒

【從零開始自制CPU學習篇03】與觸發器

優化 cpu 規範 height 一次 簡單 eight 速度 忽略 本篇學習了兩種鎖存器:SR Latch和D Latch,一種觸發器:D flip flop SR Latch:SR—鎖存器 初始狀態下,S和R都為0,Q和Q‘隨機有一個為1

CountDownLatch倒計數(Latch:門閂[shuan])

CountDownLatch 是一個通用同步工具,它有很多用途。 將計數 1 初始化的 CountDownLatch 用作一個簡單的開/關鎖存器,或入口:在通過呼叫 countDown() 的執行緒開啟入口前,所有呼叫 await 的執行緒都一直在入口處等待。 用 N 初始

FPGA基礎知識(四)、觸發器、寄存和緩沖的區別

高端 指示器 領域 串行 方法 register 緩沖區 計算機 字節 一、鎖存器鎖存器(latch)---對脈沖電平敏感,在時鐘脈沖的電平作用下改變狀態鎖存器是電平觸發的存儲單元,數據存儲的動作取決於輸入時鐘(或者使能)信號的電平值,僅當鎖存器處於使能狀態時,輸出才會隨著

JavaCountDownLatch使用

操作 java代碼 catch sdf none 同步輔助類 date() latch 調用 CountDownLatch,一個同步輔助類,在完成一組正在其他線程中執行的操作之前,它允許一個或多個線程一直等待。 主要方法 public CountDownLatch(int

硬件——STM32 , SN74HC573

高速 blog stm32 color -s mage 技術 body 電平 74HC573是一款高速CMOS器件: 上圖中:輸出使能為:OE 鎖存使能為:LE 典型電路: 上圖中:PWR-AL-0,PWR-AL-1,PWR-AL-2:是單片機

用VHDL設計多路選擇和全加

end style ces cin 實現 std spa sum component 1.2選1多路選擇器 1 library IEEE; 2 use IEEE.STD_LOGIC_1164.ALL; 3 ENTITY mux21 IS 4 PORT ( a,

UVM暫三:暫模型的整合(上)

本文轉自:http://www.eetop.cn/blog/html/28/1561828-6266220.html 我們在上一節大致瞭解了與暫存器相關的流程,包括暫存器描述檔案和UVM暫存器模型生成。從上節給的暫存器模型流程圖中我們可以看到,接下來需要考慮選擇與DUT暫存器介面一致的匯流排UV

UVM暫二:暫模型概覽(下)

本文轉自:http://www.eetop.cn/blog/html/28/1561828-6266219.html 暫存器模型構建 在構建UVM暫存器模型的過程中,讀者需要了解下面這些與模型構建相關的類和它們的功能:     簡化後的MCDF暫存器模

UVM暫七:暫模型的常規方法(下)

本文轉自:http://www.eetop.cn/blog/html/28/1561828-6266224.html mem與reg的聯絡和差別 UVM暫存器模型也可以用來對儲存建模。uvm_mem類可以用來模擬RW(讀寫)、RO(只讀)和WO(只寫)型別的儲存,並且可以配置其模型的資料寬度

UVM暫六:暫模型的常規方法(上)

本文轉自:http://www.eetop.cn/blog/html/28/1561828-6266223.html mirrored、desired和actual value 在我們應用暫存器模型的時候,除了利用它的暫存器檔案資訊,也會利用它來跟蹤暫存器的值。跟蹤暫存器的值,一方面是建立m

UVM暫五:暫模型的整合(下)

本文轉自:http://www.eetop.cn/blog/html/28/1561828-6266222.html 前門訪問 利用暫存器模型,我們可以更方便地對暫存器做操作。接下來我們分別兩種訪問暫存器的方式,即前門訪問(front-door)和後門訪問(back-door)。前門訪問,顧

UVM暫四:暫模型的整合(中)

本文轉自:http://www.eetop.cn/blog/html/28/1561828-6266221.html MCDF暫存器模組程式碼 下面我們給出實現後的MCDF暫存器RTL設計程式碼:     上面的設計中採取了巨集的

UVM暫八:暫模型的場景應用(終)

本文轉自:http://www.eetop.cn/blog/html/28/1561828-6266225.html 如何檢查暫存器模型 在瞭解了暫存器模型的常規方法之後,我們需要考慮如何利用這些方法來檢查暫存器、以及協助檢查硬體設計的邏輯和資料比對。要知道,在軟體實現硬體驅動和韌體層時,也

關於和觸發器的一點記錄

一,鎖存器與暫存器的區別: 鎖存器與觸發器最大的區別在於,鎖存器是電平觸發,而觸發器是邊沿觸發。鎖存器在不鎖存資料時,輸出隨輸入變化;但一旦資料鎖存時,輸入對輸出不產生任何影響。 首先應該明確鎖存器和觸發器也是由與非門之類的東西構成。尤其是鎖存器,雖說數位電路定義含有鎖存器或觸發器的

基礎——(4)D Latch(D)

S-R Latch   Put a inverter there:     Invertor的組成: tie both of the inputs together加上一個nor gate 就能組成一個invertor 按下就能亮 鬆開就會滅,這個東

[原創]FPGA編譯生成原因與解決辦法

遇到的問題: 在quartus ii中,編譯工程,報出如下警告: Warning (335093): TimeQuest Timing Analyzer is analyzing 160 combinational loops as latches. Quartus 官方給出

與觸發器的差異

鎖存器與暫存器的區別: 1.暫存器是同步時鐘控制,而鎖存器是電位訊號控制。鎖存器一般由電平訊號控制,屬於電平敏感型。暫存器一般由時鐘訊號訊號控制,屬於邊沿敏感型。 2.暫存器的輸出端平時不隨輸入端的變化而變化,只有在時鐘有效時才將輸入端的資料送輸出端(打入暫存器),而鎖存