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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

J.U.C系列(三)Semaphore的使用

發(fā)布時(shí)間:2025/3/19 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 J.U.C系列(三)Semaphore的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Semaphore類似于操作系統(tǒng)中的信號量,可以控制值對互斥資源的訪問線程數(shù)。
舉個(gè)例子:比如一共3把傘,9個(gè)人來借傘,傘借出去了就不能借給其他人,直到有人還傘。參考以下小demo理解以下。

package com.leo.demo.juctest;import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.*;/*** @ClassName: SemaphoreExample* @Description: 關(guān)于Semaphore測試的小demo* @Author: leo825* @Date: 2020-05-10 15:35* @Version: 1.0*/ public class SemaphoreExample {public static void main(String[] args) {//假如有3把雨傘final int umbrella = 3;//假如有10個(gè)人借傘final int persons = 9;//設(shè)置信號量Semaphore semaphore = new Semaphore(umbrella);//定義一個(gè)線程池模擬借傘ExecutorService executorService = Executors.newCachedThreadPool();for (int i = 0; i < persons; i++) {executorService.execute(() -> {try {//借傘semaphore.acquire();getThreadLog("借傘成功,當(dāng)前可接傘剩余:" + semaphore.availablePermits() + " 把");//模擬借用時(shí)長int times = ThreadLocalRandom.current().nextInt(10);getThreadLog("借用時(shí)長為:" + times + " 天");TimeUnit.MILLISECONDS.sleep(times);} catch (InterruptedException e) {e.printStackTrace();} finally {semaphore.release();getThreadLog("還傘成功,當(dāng)前可接傘剩余:" + semaphore.availablePermits() + " 把");}});}executorService.shutdown();}/*** 獲取線程名和時(shí)間** @return*/public static void getThreadLog(String logContent) {StringBuffer stringBuffer = new StringBuffer();stringBuffer.append("[");stringBuffer.append(Thread.currentThread().getName());stringBuffer.append(" ");stringBuffer.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));stringBuffer.append("]");stringBuffer.append(logContent);System.out.println(stringBuffer.toString());} }

打印結(jié)果如下:

[pool-1-thread-3 2020-05-10 16:14:31.563]借傘成功,當(dāng)前可接傘剩余:0[pool-1-thread-2 2020-05-10 16:14:31.564]借傘成功,當(dāng)前可接傘剩余:1[pool-1-thread-1 2020-05-10 16:14:31.563]借傘成功,當(dāng)前可接傘剩余:2[pool-1-thread-2 2020-05-10 16:14:31.566]借用時(shí)長為:1[pool-1-thread-3 2020-05-10 16:14:31.566]借用時(shí)長為:4[pool-1-thread-1 2020-05-10 16:14:31.566]借用時(shí)長為:6[pool-1-thread-4 2020-05-10 16:14:31.567]借傘成功,當(dāng)前可接傘剩余:0[pool-1-thread-2 2020-05-10 16:14:31.567]還傘成功,當(dāng)前可接傘剩余:1[pool-1-thread-4 2020-05-10 16:14:31.567]借用時(shí)長為:9[pool-1-thread-5 2020-05-10 16:14:31.570]借傘成功,當(dāng)前可接傘剩余:0[pool-1-thread-3 2020-05-10 16:14:31.570]還傘成功,當(dāng)前可接傘剩余:1[pool-1-thread-5 2020-05-10 16:14:31.570]借用時(shí)長為:0[pool-1-thread-5 2020-05-10 16:14:31.571]還傘成功,當(dāng)前可接傘剩余:1[pool-1-thread-6 2020-05-10 16:14:31.571]借傘成功,當(dāng)前可接傘剩余:0[pool-1-thread-6 2020-05-10 16:14:31.571]借用時(shí)長為:0[pool-1-thread-6 2020-05-10 16:14:31.572]還傘成功,當(dāng)前可接傘剩余:1[pool-1-thread-7 2020-05-10 16:14:31.572]借傘成功,當(dāng)前可接傘剩余:0[pool-1-thread-1 2020-05-10 16:14:31.572]還傘成功,當(dāng)前可接傘剩余:1[pool-1-thread-8 2020-05-10 16:14:31.572]借傘成功,當(dāng)前可接傘剩余:0[pool-1-thread-7 2020-05-10 16:14:31.572]借用時(shí)長為:8[pool-1-thread-8 2020-05-10 16:14:31.573]借用時(shí)長為:8[pool-1-thread-4 2020-05-10 16:14:31.576]還傘成功,當(dāng)前可接傘剩余:1[pool-1-thread-9 2020-05-10 16:14:31.576]借傘成功,當(dāng)前可接傘剩余:0[pool-1-thread-9 2020-05-10 16:14:31.577]借用時(shí)長為:9[pool-1-thread-8 2020-05-10 16:14:31.581]還傘成功,當(dāng)前可接傘剩余:2[pool-1-thread-7 2020-05-10 16:14:31.581]還傘成功,當(dāng)前可接傘剩余:2[pool-1-thread-9 2020-05-10 16:14:31.587]還傘成功,當(dāng)前可接傘剩余:3

可以從日志中看出,當(dāng)有傘的時(shí)候才可以借,也就是說誰還了傘,下一個(gè)人才可以借用,這就是信號量,主要控制互斥資源的訪問。

總結(jié)

以上是生活随笔為你收集整理的J.U.C系列(三)Semaphore的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。