最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java多線程并發(fā)JUC包ReentrantLock顯示鎖的用法

 更新時(shí)間:2025年05月16日 10:38:26   作者:二六八  
這篇文章主要介紹了Java多線程并發(fā)JUC包ReentrantLock顯示鎖的用法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java多線程并發(fā)JUC包 ReentrantLock 顯示鎖

ReentrantLock支持以下功能:

  • 支持公平和非公平的獲取鎖的方式。
  • 支持可重入。

公平鎖與非公平鎖:

  • 公平鎖:加鎖前先查看是否有排隊(duì)等待的線程,有的話優(yōu)先處理排在前面的線程,先來先得。
  • 非公平鎖:線程加鎖時(shí)直接嘗試獲取鎖,獲取不到就自動(dòng)到隊(duì)尾等待。

1 lock 和 unlock 方法說明

該demo模擬電影院的售票情況,tickets總票數(shù)。開啟了10個(gè)窗口售票,售完為止,程序代碼如下:

public class ReentrantLockDemo01 implements Runnable {

    private Lock lock = new ReentrantLock();

    private int tickets = 50;

    @Override
    public void run() {
        while (true) {
            // 獲取鎖
            if (lock.tryLock()) {
                try {
                    if (tickets > 0) {
                        TimeUnit.MILLISECONDS.sleep(100);
                        System.out.println(Thread.currentThread().getName() + " " + tickets--);
                    } else {
                        break;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock(); // 釋放所
                }
            }

        }
    }

    public static void main(String[] args) {
        ReentrantLockDemo01 reentrantLockDemo = new ReentrantLockDemo01();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(reentrantLockDemo, "thread - " + i);
            thread.start();
        }
    }
}

輸出如下 :

thread - 0 50
thread - 7 49
thread - 4 48
thread - 7 47
thread - 7 46
thread - 7 45
thread - 7 44
thread - 7 43
thread - 7 42
thread - 7 41
thread - 7 40
thread - 7 39
thread - 7 38
thread - 7 37
thread - 7 36
thread - 7 35
thread - 7 34
thread - 7 33
thread - 7 32
thread - 7 31
thread - 7 30
thread - 5 29
thread - 5 28
thread - 5 27
thread - 6 26
thread - 6 25
thread - 7 24
thread - 7 23
thread - 7 22
thread - 7 21
thread - 5 20
thread - 5 19
thread - 5 18
thread - 7 17
thread - 2 16
thread - 2 15
thread - 2 14
thread - 2 13
thread - 1 12
thread - 1 11
thread - 1 10
thread - 1 9
thread - 1 8
thread - 1 7
thread - 1 6
thread - 1 5
thread - 1 4
thread - 1 3
thread - 1 2
thread - 1 1

2 newCondition方法

Condition的作用是對鎖進(jìn)行更精確的控制。

Condition中的 await() 方法相當(dāng)于Object的 wait() 方法,Condition中的 signal() 方法相當(dāng)于Object的 notify() 方法,Condition中的 signalAll() 相當(dāng)于Object的 notifyAll() 方法。

不同的是,Object中的 wait() , notify() , notifyAll() 方法是和”同步鎖”(synchronized關(guān)鍵字)捆綁使用的;而Condition是需要與”互斥鎖”/”共享鎖”捆綁使用的。

/**
 * 生產(chǎn)者消費(fèi)者
 */
public class ProducerConsumerTest {

    private Lock lock = new ReentrantLock();

    private Condition addCondition = lock.newCondition();

    private Condition removeCondition = lock.newCondition();

    private LinkedList<Integer> resources = new LinkedList<>();

    private int maxSize;

    public ProducerConsumerTest(int maxSize) {
        this.maxSize = maxSize;
    }


    public class Producer implements Runnable {

        private int proSize;

        private Producer(int proSize) {
            this.proSize = proSize;
        }

        @Override
        public void run() {
            lock.lock();
            try {
                for (int i = 1; i < proSize; i++) {
                    while (resources.size() >= maxSize) {
                        System.out.println("當(dāng)前倉庫已滿,等待消費(fèi)...");
                        try {
                            addCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("已經(jīng)生產(chǎn)產(chǎn)品數(shù): " + i + "\t現(xiàn)倉儲(chǔ)量總量:" + resources.size());
                    resources.add(i);
                    removeCondition.signal();
                }
            } finally {
                lock.unlock();
            }

        }
    }

    public class Consumer implements Runnable {

        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            while (true) {
                lock.lock();
                try {
                    while (resources.size() <= 0) {
                        System.out.println(threadName + " 當(dāng)前倉庫沒有產(chǎn)品,請稍等...");
                        try {
                            // 進(jìn)入阻塞狀態(tài)
                            removeCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    // 消費(fèi)數(shù)據(jù)
                    int size = resources.size();
                    for (int i = 0; i < size; i++) {
                        Integer remove = resources.remove();
                        System.out.println(threadName + " 當(dāng)前消費(fèi)產(chǎn)品編號(hào)為:" + remove);
                    }
                    // 喚醒生產(chǎn)者
                    addCondition.signal();
                } finally {
                    lock.unlock();
                }
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        ProducerConsumerTest producerConsumerTest = new ProducerConsumerTest(10);
        Producer producer = producerConsumerTest.new Producer(100);
        Consumer consumer = producerConsumerTest.new Consumer();
        final Thread producerThread = new Thread(producer, "producer");
        final Thread consumerThread = new Thread(consumer, "consumer");
        producerThread.start();
        TimeUnit.SECONDS.sleep(2);
        consumerThread.start();
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java HashMap原理及實(shí)例解析

    Java HashMap原理及實(shí)例解析

    這篇文章主要介紹了Java HashMap原理及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot Controller中的常用注解

    SpringBoot Controller中的常用注解

    這篇文章主要介紹了SpringBoot Controller中的常用注解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 基于@AliasFor注解的用法及說明

    基于@AliasFor注解的用法及說明

    這篇文章主要介紹了基于@AliasFor注解的用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • spring security登錄成功后通過Principal獲取名返回空問題

    spring security登錄成功后通過Principal獲取名返回空問題

    這篇文章主要介紹了spring security登錄成功后通過Principal獲取名返回空問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 關(guān)于Java?中?Future?的?get?方法超時(shí)問題

    關(guān)于Java?中?Future?的?get?方法超時(shí)問題

    這篇文章主要介紹了Java?中?Future?的?get?方法超時(shí),最常見的理解就是,“超時(shí)以后,當(dāng)前線程繼續(xù)執(zhí)行,線程池里的對應(yīng)線程中斷”,真的是這樣嗎?本文給大家詳細(xì)介紹,需要的朋友參考下吧
    2022-06-06
  • Java使用freemarker實(shí)現(xiàn)word下載方式

    Java使用freemarker實(shí)現(xiàn)word下載方式

    文章介紹了如何使用FreeMarker實(shí)現(xiàn)Word文件下載,包括引用依賴、創(chuàng)建Word模板、將Word文件存為XML格式、更改后綴為FTL模板、處理圖片和代碼實(shí)現(xiàn)
    2025-02-02
  • spring?@Transactional注解中常用參數(shù)詳解

    spring?@Transactional注解中常用參數(shù)詳解

    這篇文章主要介紹了spring?@Transactional注解中常用參數(shù)詳解,事物注解方式:?@Transactional,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • 通過springboot發(fā)布WebService接口并調(diào)用方式

    通過springboot發(fā)布WebService接口并調(diào)用方式

    Spring Boot集成CXF需注意版本對應(yīng),配置注解并發(fā)布服務(wù),通過WSDL驗(yàn)證,結(jié)合Controller和Swagger測試,CXF支持SOAP、REST等服務(wù),提供代碼與合同優(yōu)先開發(fā)模式
    2025-09-09
  • 詳解SpringBoot可執(zhí)行Jar包運(yùn)行原理

    詳解SpringBoot可執(zhí)行Jar包運(yùn)行原理

    SpringBoot有一個(gè)很方便的功能就是可以將應(yīng)用打成可執(zhí)行的Jar,那么大家有沒想過這個(gè)Jar是怎么運(yùn)行起來的呢,本篇博客就來介紹下 SpringBoot可執(zhí)行Jar包的運(yùn)行原理,需要的朋友可以參考下
    2023-05-05
  • 淺談HttpClient、okhttp和RestTemplate的區(qū)別

    淺談HttpClient、okhttp和RestTemplate的區(qū)別

    這篇文章主要介紹了HttpClient、okhttp和RestTemplate的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評論

襄汾县| 临桂县| 利川市| 石楼县| 灵璧县| 江阴市| 彭泽县| 昔阳县| 泸西县| 海宁市| 和静县| 五家渠市| 陈巴尔虎旗| 碌曲县| 鸡泽县| 南江县| 临海市| 合江县| 布尔津县| 海淀区| 姜堰市| 会泽县| 鄂温| 延安市| 武穴市| 罗江县| 澎湖县| 宣汉县| 玉树县| 栾川县| 封开县| 芷江| 杭锦后旗| 莱阳市| 井冈山市| 邛崃市| 田阳县| 海淀区| 云林县| 蓝田县| 韩城市|