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

Java多種方式實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模式

 更新時(shí)間:2020年07月15日 08:34:32   作者:lkjhgfdsa123  
這篇文章主要介紹了Java多種方式實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

實(shí)現(xiàn)需求:兩個(gè)線程交替打印1,0,打印10輪

java多線程口訣:

  • 高內(nèi)聚,低耦合
  • 線程操作資源類
  • 判斷干活通知
  • 防止虛假喚醒

方式一:使用synchronized和Object的wait和notifyAll方法

wait:使當(dāng)前線程阻塞

notify,notifyAll喚醒當(dāng)前線程

/**
 * 兩個(gè)線程交替打印1,0 打印10輪
 *
 * @author Administrator
 * @version 1.0 2020年7月12日
 * @see ProdConsumerDemo1
 * @since 1.0
 *
 */
class ShareData1 {
 
  public int number = 0;
 
  public synchronized void increment() throws Exception {
    while (number != 0) {
      this.wait();
    }
    number++;
    System.out.println(Thread.currentThread().getName() + " " + number);
    this.notifyAll();
  }
 
  public synchronized void decrement() throws InterruptedException {
    while (number != 1) {
      this.wait();
    }
    number--;
    System.out.println(Thread.currentThread().getName() + " " + number);
    this.notifyAll();
  }
}
 
public class ProdConsumerDemo1 {
 
  public static void main(String[] args) {
    ShareData1 shareData = new ShareData1();
    new Thread(() -> {
      for (int i = 0; i < 10; i++) {
        try {
          shareData.increment();
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }, "A").start();
    new Thread(() -> {
      for (int i = 0; i < 10; i++) {
        try {
          shareData.decrement();
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }, "B").start();
  }
}

輸出結(jié)果

A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0

方式二:使用jdk1.8的Lock和Condition

class ShareData2 {
 
  private int number = 0;
 
  private Lock lock = new ReentrantLock();
 
  private Condition condition = lock.newCondition();
 
  public void increment() throws Exception {
    lock.lock();
    try {
      while (number != 0) {
        condition.await();
      }
      number++;
      System.out.println(Thread.currentThread().getName() + " " + number);
      condition.signalAll();
    } finally {
      lock.unlock();
    }
  }
 
  public void decrement() throws InterruptedException {
    lock.lock();
    try {
      while (number != 1) {
        condition.await();
      }
      number--;
      System.out.println(Thread.currentThread().getName() + " " + number);
      condition.signalAll();
    } finally {
      // TODO: handle finally clause
      lock.unlock();
    }
  }
}
 
public class ProdConsumerDemo2 {
 
  public static void main(String[] args) {
    ShareData2 shareData = new ShareData2();
    new Thread(() -> {
      for (int i = 0; i < 10; i++) {
        try {
          shareData.increment();
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }, "A").start();
    new Thread(() -> {
      for (int i = 0; i < 10; i++) {
        try {
          shareData.decrement();
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }, "B").start();
  }
}

輸出結(jié)果

A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0

主要是熟悉Lock和Condition的使用

Lock和Condition相比于synchronized,能夠精確喚醒

需求:三個(gè)線程A,B,C順序打印,A打印5次,B打印10次,C打印15次,10輪

class ShareData3 {
 
  private int number = 1;
 
  private Lock lock = new ReentrantLock();
 
  private Condition c1 = lock.newCondition();
 
  private Condition c2 = lock.newCondition();
 
  private Condition c3 = lock.newCondition();
 
  public void print5() throws Exception {
    lock.lock();
    try {
      while (number != 1) {
        c1.await();
      }
      number = 2;
      for (int i = 0; i < 5; i++) {
        System.out.println(Thread.currentThread().getName() + " " + i);
      }
      c2.signalAll();
    } finally {
      // TODO: handle finally clause
      lock.unlock();
    }
  }
 
  public void print10() throws InterruptedException {
    lock.lock();
    try {
      while (number != 2) {
        c2.await();
      }
      number=3;
      for (int i = 0; i < 10; i++) {
        System.out.println(Thread.currentThread().getName() + " " + i);
      }
      c3.signalAll();
    } finally {
      // TODO: handle finally clause
      lock.unlock();
    }
  }
 
  public void print15() throws InterruptedException {
    lock.lock();
    try {
      while (number != 3) {
        c3.await();
      }
      number = 1;
      for (int i = 0; i < 15; i++) {
        System.out.println(Thread.currentThread().getName() + " " + i);
      }
      c1.signalAll();
    } finally {
      // TODO: handle finally clause
      lock.unlock();
    }
  }
}
 
public class ProdConsumerDemo3 {
 
  public static void main(String[] args) {
    ShareData3 shareData3 = new ShareData3();
    new Thread(() -> {
      try {
        for (int i = 0; i < 10; i++) {
          shareData3.print5();
        }
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }, "A").start();
    new Thread(() -> {
      try {
        for (int i = 0; i < 10; i++) {
          shareData3.print10();
        }
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }, "B").start();
    new Thread(() -> {
      try {
        for (int i = 0; i < 10; i++) {
          shareData3.print15();
        }
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }, "C").start();
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java如何創(chuàng)建Annotation

    詳解Java如何創(chuàng)建Annotation

    在本文中,我們將介紹注解的基礎(chǔ)知識(shí),包括注解是什么,它們?nèi)绾卧谑纠惺褂?,以及如何處理它們。下面和小編一起來學(xué)習(xí)吧
    2019-05-05
  • java selenium教程環(huán)境搭建基于Maven

    java selenium教程環(huán)境搭建基于Maven

    本文主要介紹Java selenium 環(huán)境的安裝,這里介紹了基于Maven的環(huán)境搭建,有需要的小伙伴可以參考下
    2016-08-08
  • 使用Java代碼進(jìn)行因數(shù)分解和求最小公倍數(shù)的示例

    使用Java代碼進(jìn)行因數(shù)分解和求最小公倍數(shù)的示例

    這篇文章主要介紹了使用Java代碼進(jìn)行因數(shù)分解和求最小公倍數(shù)的示例,都是基于最基礎(chǔ)的算法原理實(shí)現(xiàn),需要的朋友可以參考下
    2015-11-11
  • Java組件commons fileupload實(shí)現(xiàn)文件上傳功能

    Java組件commons fileupload實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Java組件commons fileupload實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Spring Security注冊(cè)過濾器注意事項(xiàng)詳解

    Spring Security注冊(cè)過濾器注意事項(xiàng)詳解

    前兩天和小伙伴聊了 Spring Security+JWT 實(shí)現(xiàn)無狀態(tài)登錄,然后有小伙伴反饋了一個(gè)問題,感覺這是一個(gè)我們平時(shí)寫代碼容易忽略的問題,所以本文給大家介紹了Spring Security注冊(cè)過濾器注意事項(xiàng),需要的朋友可以參考下
    2024-06-06
  • Java代碼優(yōu)化細(xì)節(jié)

    Java代碼優(yōu)化細(xì)節(jié)

    這篇文章主要為大家詳細(xì)介紹了Java代碼優(yōu)化細(xì)節(jié),通過不同細(xì)節(jié)對(duì)java代碼進(jìn)行優(yōu)化,感興趣的小伙伴們可以參考一下
    2016-08-08
  • spring解決循環(huán)依賴的方案示例

    spring解決循環(huán)依賴的方案示例

    這篇文章主要介紹spring如何解決循環(huán)依賴,文中有相關(guān)的代碼示例給大家參考,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以借鑒閱讀
    2023-05-05
  • Spring Boot中自定義注解結(jié)合AOP實(shí)現(xiàn)主備庫(kù)切換問題

    Spring Boot中自定義注解結(jié)合AOP實(shí)現(xiàn)主備庫(kù)切換問題

    這篇文章主要介紹了Spring Boot中自定義注解+AOP實(shí)現(xiàn)主備庫(kù)切換的相關(guān)知識(shí),本篇文章的場(chǎng)景是做調(diào)度中心和監(jiān)控中心時(shí)的需求,后端使用TDDL實(shí)現(xiàn)分表分庫(kù),需要的朋友可以參考下
    2019-08-08
  • SpringBoot時(shí)區(qū)問題解決以及徹底解決時(shí)差問題

    SpringBoot時(shí)區(qū)問題解決以及徹底解決時(shí)差問題

    這篇文章主要給大家介紹了關(guān)于SpringBoot時(shí)區(qū)問題解決以及徹底解決時(shí)差問題的相關(guān)資料,spring?boot作為微服務(wù)簡(jiǎn)易架構(gòu),擁有其自身的特點(diǎn),快速搭建架構(gòu),簡(jiǎn)單快捷,需要的朋友可以參考下
    2023-08-08
  • SpringBoot3.x打包Docker容器的實(shí)現(xiàn)

    SpringBoot3.x打包Docker容器的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot3.x打包Docker容器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論

吴川市| 乐安县| 屏边| 吴川市| 西充县| 桐梓县| 天津市| 东莞市| 廉江市| 寿宁县| 清河县| 阜康市| 中牟县| 宜城市| 永城市| 淮南市| 乳源| 民丰县| 高密市| 卢龙县| 甘孜县| 神农架林区| 盐城市| 扶绥县| 通道| 沂南县| 嫩江县| 革吉县| 瑞丽市| 冀州市| 项城市| 陵川县| 西乌珠穆沁旗| 西畴县| 周至县| 招远市| 秭归县| 西充县| 全州县| 三台县| 象州县|