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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java并发编程中级篇(一):使用Semaphore信号量进行并发控制

發布時間:2025/6/15 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java并发编程中级篇(一):使用Semaphore信号量进行并发控制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Semaphore是一個二進制信號量,只有0和1兩個值。如果線程想要訪問一個共享資源,它必須先獲得信號量。如果信號量的內部計數器大于0,那么信號量減1,并允許訪問這個資源。否則,如果信號量計數器等于0,線程會等待直至計數器大于0。

所以說計數器大于0,說明有資源可用。計數器等于0,說明沒有資源可用。

同時Semaphore提供了一個帶有boolean參數的構造方法,true代表公平鎖,false代表非公平鎖,默認實現是非公平鎖。

我們使用Semaphore信號量來重寫PrintQueue的例子。

首先實現一個PrintQueue打印隊列,有一個Semaphore信號量用來并發訪問控制。打印之前使用acquire()方法獲取信號量,執行完畢后使用release()方法釋放信號量。每次打印等待一個隨機時間,模擬打印耗時。

public class PrintQueue {private Semaphore semaphore;public PrintQueue() {//semaphore = new Semaphore(1); //非公平的semaphore = new Semaphore(1, true); //公平的}public void printJob(Object document) {try {semaphore.acquire();long duration = (long)(Math.random() * 10000);System.out.printf("%s: Print a job duration %d seconds.\n",Thread.currentThread().getName(), duration / 1000);Thread.sleep(duration);} catch (InterruptedException e) {e.printStackTrace();} finally {semaphore.release();}} }

創建Job線程類,模擬打印請求。

public class Job implements Runnable{private PrintQueue printQueue;public Job(PrintQueue printQueue) {this.printQueue = printQueue;}@Overridepublic void run() {System.out.printf("%s: Going to print a Job.\n", Thread.currentThread().getName());printQueue.printJob(new Object());System.out.printf("%s: The Job has been printed.\n", Thread.currentThread().getName());} }

主方法類中啟動10個打印Job線程。

public class Main {public static void main(String[] args) {PrintQueue printQueue = new PrintQueue();Thread[] threads = new Thread[10];for (int i = 1; i < 10; i++) {threads[i] = new Thread(new Job(printQueue));}for (int i = 1; i < 10; i++) {threads[i].start();}} }

查看日志,每次只有一個打印Job可以執行打印程序,其他線程處于WAITING狀態。

Thread-0: Going to print a Job. Thread-8: Going to print a Job. Thread-7: Going to print a Job. Thread-6: Going to print a Job. Thread-5: Going to print a Job. Thread-4: Going to print a Job. Thread-0: Print a job duration 2 seconds. Thread-3: Going to print a Job. Thread-2: Going to print a Job. Thread-1: Going to print a Job. Thread-0: The Job has been printed. Thread-8: Print a job duration 8 seconds. Thread-8: The Job has been printed. Thread-7: Print a job duration 8 seconds. Thread-7: The Job has been printed. Thread-6: Print a job duration 9 seconds. Thread-6: The Job has been printed. Thread-5: Print a job duration 6 seconds. Thread-5: The Job has been printed. Thread-4: Print a job duration 7 seconds. Thread-4: The Job has been printed. Thread-3: Print a job duration 4 seconds. Thread-3: The Job has been printed. Thread-2: Print a job duration 1 seconds. Thread-2: The Job has been printed. Thread-1: Print a job duration 1 seconds. Thread-1: The Job has been printed.

?

轉載于:https://my.oschina.net/nenusoul/blog/794378

總結

以上是生活随笔為你收集整理的Java并发编程中级篇(一):使用Semaphore信号量进行并发控制的全部內容,希望文章能夠幫你解決所遇到的問題。

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