JAVAマルチスレッド(5)生産者消費者モデルをlock,synchronized,ブロックキューの3つの方法で実現

16838 ワード

このブログは前のJAVAマルチスレッド(三)生産者の消費者モデルと実現方法の補充と言える.生産者消費者モードは、3つの方法(lock、synchronized、ブロックキュー)で実現される.具体的には、生産者が乱数を生成し(結果を読みやすくするために、乱数を10以内の整数に限定します)、消費者が読み取り、印刷します.

1ブロックキューによる生産者消費者モデルの実現


ブロックキューは最も簡単な実装方法です
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Random;

public class BlockingQueuePattern {

    public static void main(String args[]){

     // 
     BlockingQueue sharedQueue = new LinkedBlockingQueue();

     // 
     Thread prodThread = new Thread(new Producer(sharedQueue));
     Thread consThread = new Thread(new Consumer(sharedQueue));

     // 
     prodThread.start();
     consThread.start();
    }
}

// 
class Producer implements Runnable {

    private final BlockingQueue sharedQueue;

    public Producer(BlockingQueue sharedQueue) {
        this.sharedQueue = sharedQueue;
    }

    @Override
    public void run() {
        for(int i=0; i<10; i++){
            try {
                // 10 
                Random random = new Random();
                int ProdRandom=random.nextInt(10);
                System.out.println("Produced: " + ProdRandom);
                sharedQueue.put(ProdRandom);
            } catch (InterruptedException ex) {
                Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

// 
class Consumer implements Runnable{

    private final BlockingQueue sharedQueue;

    public Consumer (BlockingQueue sharedQueue) {
        this.sharedQueue = sharedQueue;
    }

    @Override
    public void run() {
        while(true){
            try {
                System.out.println("Consumed: "+ sharedQueue.take());
            } catch (InterruptedException ex) {
                Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
} 
Output:
Produced: 4
Produced: 7
Produced: 8
Consumed: 4
Consumed: 7
Produced: 6
Consumed: 8
Consumed: 6
Produced: 1
Produced: 7
Consumed: 1
Consumed: 7
Produced: 3
Produced: 5
Consumed: 3
Consumed: 5
Produced: 9
Produced: 7
Consumed: 9
Consumed: 7

2 lock生産者消費者モデルの実現


JAVAが提供してくれた既存のブロックキューを使わない以上、私たちは自分でキューを作成したほうがいいです.コードは以下の通りです.
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class OptimisticLockPattern {
    public static void main(String[] args){     
         SelfQueue selfqueue = new SelfQueue();

         // 
         Thread prodThread = new Thread(new Producer(selfqueue));
         Thread consThread = new Thread(new Consumer(selfqueue));

         // 
         prodThread.start();
         consThread.start();
    }
}


class SelfQueue{
    int max = 5;
    LinkedList ProdLine = new LinkedList();
    Lock lock = new ReentrantLock(); 
    Condition full = lock.newCondition();  
    Condition empty = lock.newCondition();

    public void produce(int ProdRandom){       
        try {
            lock.lock();
            while(max == ProdLine.size()){
                System.out.println(" , ");
                full.await();
            }
            ProdLine.add(ProdRandom);
            empty.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            lock.unlock();
        }
    } 

    public int consume(){  
        int m = 0;
        try {
            lock.lock();
            while(ProdLine.size() == 0){
                System.out.println(" , ");
                empty.await();
            }
            m = ProdLine.removeFirst();
            full.signal(); 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            lock.unlock();
            return m;
        }
    }
}

// 
class Producer implements Runnable{
    private final SelfQueue selfqueue;

    public Producer(SelfQueue selfqueue) {
          this.selfqueue = selfqueue;
    }

    public void run() {
      for (int i = 0; i < 10; i++) {
            Random random = new Random();
            int ProdRandom=random.nextInt(10);
            System.out.println("Produced: " + ProdRandom);
            selfqueue.produce(ProdRandom);          
      }
  }
}

// 
class Consumer implements Runnable{
    private final SelfQueue selfqueue;

    public Consumer(SelfQueue selfqueue) {
          this.selfqueue = selfqueue;
    }

    public void run() {
      while(true) {
              System.out.println("Consumed: "+ selfqueue.consume());
      }
  }
}
Output:
Produced: 1
Produced: 9
Consumed: 1
Consumed: 9
 , 
Produced: 9
Produced: 1
Consumed: 9
Produced: 8
Consumed: 1
Consumed: 8
 , 
Produced: 6
Produced: 8
Consumed: 6
Produced: 4
Consumed: 8
Produced: 4
Consumed: 4
Produced: 0
Consumed: 4
Consumed: 0

3 synchronized生産者消費者モデルの実現


synchronizedは自分で手動でロックを解除する必要はありません.ここでは前述したwait()¬ify()メソッドを使用します.
import java.util.Random;

public class PessimisticLockPattern {
    public static void main(String[] args){     
         SelfQueue selfqueue = new SelfQueue();

         // 
         Thread prodThread = new Thread(new Producer(selfqueue));
         Thread consThread = new Thread(new Consumer(selfqueue));

         // 
         prodThread.start();
         consThread.start();
    }
}


class SelfQueue{
    int index = 0; 
    int[] ProdLine = new int[6];

    public synchronized void produce(int ProdRandom){
        while(index == ProdLine.length){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        ProdLine[index] = ProdRandom;
        index++;
    }

    public synchronized int consume(){
        while(index == 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        index--;
        return ProdLine[index];
    }
}

// 
class Producer implements Runnable{
    private final SelfQueue selfqueue;

    public Producer(SelfQueue selfqueue) {
        this.selfqueue = selfqueue;
  }

  public void run() {
    for (int i = 0; i < 10; i++) {
            Random random = new Random();
            int ProdRandom = random.nextInt(10);
        System.out.println("Produced: " + ProdRandom);
            selfqueue.produce(ProdRandom);          
    }
}
}

// 
class Consumer implements Runnable{
    private final SelfQueue selfqueue;

  public Consumer(SelfQueue selfqueue) {
        this.selfqueue = selfqueue;
  }

  public void run() {
    while(true) {
          System.out.println("Consumed: "+ selfqueue.consume());
    }
}
}
Output:
Produced: 3
Produced: 3
Consumed: 3
Produced: 8
Produced: 3
Consumed: 3
Produced: 2
Produced: 6
Consumed: 3
Produced: 7
Produced: 8
Produced: 1
Produced: 9
Consumed: 6
Consumed: 9
Consumed: 1
Consumed: 8
Consumed: 7
Consumed: 2
Consumed: 8