java並發編程之Semaphore

來源:互聯網
上載者:User

標籤:semaphore   java並發編程   

訊號量(Semaphore),有時被稱為號誌,是在多線程環境下使用的一種設施, 它負責協調各個線程, 以保證它們能夠正確、合理的使用公用資源。


一個計數訊號量。從概念上講,訊號量維護了一個許可集。如有必要,在許可可用前會阻塞每一個 acquire(),然後再擷取該許可。每個 release() 添加一個許可,從而可能釋放一個正在阻塞的擷取者。但是,不使用實際的許可對象,Semaphore 只對可用許可的號碼進行計數,並採取相應的行動。拿到訊號量的線程可以進入代碼,否則就等待。通過acquire()和release()擷取和釋放訪問許可。

package com.lala.shop;import java.time.Duration;import java.time.Instant;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;import java.util.concurrent.TimeUnit;public class SemaphoreDemo {/** * 這裡示範了一個例子:十個人一起上廁所 * 如果一次只能一個人上廁所,則總共需要時間:1+2+3+4+5+6+7+8+9+10=55分鐘 * 如果一次能10個人一起上廁所,則總共需要時間:10分鐘 */static void demonstration(int num) throws Exception{String[] users = {"劉梅","夏東海","夏雪","劉星","夏雨","爺爺","姥姥","瑪麗","胡一統","林寧"};CountDownLatch cdl = new CountDownLatch(users.length);Integer[] times = {1,2,3,4,5,6,7,8,9,10};List<Integer> timeList = Arrays.asList(times);Collections.shuffle(timeList);Semaphore sph = new Semaphore(num);ExecutorService runner = Executors.newCachedThreadPool();Instant start = Instant.now();for(int i=0; i<users.length; i++){final int index = i;runner.submit(() -> {try {//拿到號誌,準備上廁所sph.acquire();long time = timeList.get(index);TimeUnit.SECONDS.sleep(time);System.out.println(users[index] + "如廁完畢,共花費時間:" + time);cdl.countDown();//事情已經辦完,釋放號誌sph.release();} catch (Exception e) {e.printStackTrace();}});}runner.shutdown();cdl.await();Instant end = Instant.now();Duration speed = Duration.between(start, end);long seconds = speed.getSeconds();//秒錶示   System.out.println("全部上完廁所(一次只能有" + num + "人如廁),總共花了時間:" + seconds);}public static void main(String[] args) throws Exception{demonstration(5);}}

如果調用方式為:

demonstration(5);

則,輸出為:

夏雪如廁完畢,共花費時間:1
夏雨如廁完畢,共花費時間:3
劉星如廁完畢,共花費時間:4
爺爺如廁完畢,共花費時間:6
夏東海如廁完畢,共花費時間:8
劉梅如廁完畢,共花費時間:9
胡一統如廁完畢,共花費時間:2
瑪麗如廁完畢,共花費時間:7
林寧如廁完畢,共花費時間:5
姥姥如廁完畢,共花費時間:10
全部上完廁所(一次只能有5人如廁),總共花了時間:13


如果調用方式為:

demonstration(1);

則,輸出為:

劉梅如廁完畢,共花費時間:10
夏東海如廁完畢,共花費時間:6
夏雪如廁完畢,共花費時間:3
劉星如廁完畢,共花費時間:9
夏雨如廁完畢,共花費時間:8
爺爺如廁完畢,共花費時間:4
姥姥如廁完畢,共花費時間:1
瑪麗如廁完畢,共花費時間:5
胡一統如廁完畢,共花費時間:7
林寧如廁完畢,共花費時間:2
全部上完廁所(一次只能有1人如廁),總共花了時間:55


如果調用方式為

demonstration(10); 則輸出為:

夏雨如廁完畢,共花費時間:1
夏東海如廁完畢,共花費時間:2
爺爺如廁完畢,共花費時間:3
劉星如廁完畢,共花費時間:4
劉梅如廁完畢,共花費時間:5
胡一統如廁完畢,共花費時間:6
林寧如廁完畢,共花費時間:7
姥姥如廁完畢,共花費時間:8
瑪麗如廁完畢,共花費時間:9
夏雪如廁完畢,共花費時間:10
全部上完廁所(一次只能有10人如廁),總共花了時間:10

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java並發編程之Semaphore

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.