Javaスレッド通信-生産者/消費者モデル(パイプライン方式)

マルチスレッド-スレッド通信-生産者-消費者モデル

マルチスレッドの使用は、多くの場合、単一のスレッドの実現だけでなく、論理サービスを実現するための複数のスレッドの連携です。

スレッド通信とは何ですか?
複数のスレッドが連携して共有リソースの使用を完了し、スレッドセーフを実現し、スレッドがデッドロックしません。

スレッド通信の実装メソッド:waitwait())| notify notify()。notifyall()
wait()、notify()、notifyall()メソッドはすべてObjectクラスのメソッドです。

wait():現在のスレッドを待機状態にして、ロックリソースを解放します。
notify():待機中のスレッドとランダムなスレッドをウェイクアップします。
notifyall():待機中のすべてのスレッドをウェイクアップします。

生産者/消費者モデル-パイプライン法

プロデューサーの消費者は、モデルではなく、シーンとシーンです。パイプライン方式:キャッシングメカニズムと同様に、生産者は生産された食品を容器に入れ、消費者は容器から食品を取り出します。

事例:
生産者:食料を生産する。
消費者:食物を消費します。
食品容器:食品が10個未満の場合、生産者は食品を生産して容器に入れる必要があります。消費者は容器から食品を取り出します。食品がない場合、生産者が食品を生産するのを待つしかありません。
ここに画像の説明を挿入
成し遂げる:

  // 食物实体类
   static class Food{
    
    
   }
// 食物容器类(公共资源)
static class FoodContainer{
    
    
       int count = 0;
       // 定义一个容器大小为10
       Food[] foods = new Food[10];

       // 往容器放食物
   public synchronized void  push(Food food){
    
    
       // 如果容器满了,等待,通知消费者消费
       if(count==foods.length){
    
    
           try {
    
    
               wait();
           } catch (InterruptedException e) {
    
    
               e.printStackTrace();
           }
       }
       // 没有满,生产食物,放到容器中。
       foods[count] = food;
       count++;
       // 通知消费者消费
       notifyAll();
   }
   
   // 从容器中取食物
    public synchronized  void Take(){
    
    
       if(count<=0){
    
    
           try {
    
    
               wait();
           } catch (InterruptedException e) {
    
    
               e.printStackTrace();
           }
       }
       // 有食物则取食物
        count--;
       // 通知生产者生产
        notifyAll();
    }

    }
  // 生产者
  static class ProductMan implements Runnable{
    
    
      // 定义容器
      FoodContainer foodContainer;
      public ProductMan(FoodContainer foodContainer){
    
    
          this.foodContainer = foodContainer;
      }


      // 生产者生产食物
      public void run() {
    
    
          //生产食物
          for (int i = 0; i <20 ; i++) {
    
    
              foodContainer.push(new Food());
              System.out.println(Thread.currentThread().getName()+": 生产了"+i+"只鸡");
          }
      }
  }
  // 消费者
  static class Consumman implements Runnable{
    
    
      // 定义容器
      FoodContainer foodContainer;
      public Consumman(FoodContainer foodContainer){
    
    
          this.foodContainer = foodContainer;
      }

        // 消费者消费食物
        public void run() {
    
    
            //消费食物
            for (int i = 0; i <20 ; i++) {
    
    
                System.out.println(Thread.currentThread().getName()+": 消费了"+i+"只鸡");
                foodContainer.Take();
            }
        }
    }
    public static void main(String[] args) {
    
    
         // 启动生产者和消费者线程
         FoodContainer foodContainer =new FoodContainer();
         new Thread(new ProductMan(foodContainer)).start();
         new Thread(new Consumman(foodContainer)).start();
    }

結果:生産者が10羽目の鶏肉に到達すると、生産を継続する前にそれを消費するように消費者に指示します。
ここに画像の説明を挿入
注:コンテナーはパブリックリソースであるため、コンテナーの操作にスレッド同期を追加する必要があります。
次に、生産者と消費者に光の方法を実現する別の方法を紹介します。

おすすめ

転載: blog.csdn.net/qq_31142237/article/details/115285293