程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Lock、Condition完成簡略的臨盆者花費者形式示例

Lock、Condition完成簡略的臨盆者花費者形式示例

編輯:關於JAVA

Lock、Condition完成簡略的臨盆者花費者形式示例。本站提示廣大學習愛好者:(Lock、Condition完成簡略的臨盆者花費者形式示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Lock、Condition完成簡略的臨盆者花費者形式示例正文



package condition;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
 * 應用Lock、Condition完成臨盆者花費者形式
 * @author will
 *
 */
public class ProducerConsumerDemo {

 public static void main(String[] args) {
  int producerCount = 10;
  int consumerCount = 15;

  final ProducerConsumerDemo pcd = new ProducerConsumerDemo(5);   // 緩沖區年夜小為5

  Thread[] producerThreads = new Thread[producerCount];
  for(int i = 0; i < producerCount; i++) {
   producerThreads[i] = new Thread("producer" + (i+1)) {

    @Override
    public void run() {
     pcd.produce();
    }
   };
  }

  Thread[] consumerThreads = new Thread[consumerCount];
  for(int j = 0; j < consumerCount; j++) {
   consumerThreads[j] = new Thread("consumer" + (j+1)) {
    @Override
    public void run() {
     pcd.consume();
    }
   };
  }

  // 啟動臨盆者花費者線程
  for(int i = 0; i < producerCount; i++) {
   producerThreads[i].start();
  }
  for(int j = 0; j < consumerCount; j++) {
   consumerThreads[j].start();
  }
 }

 private static final int DEFAULT_BUFFER_SIZE = 10;
 private int bufferSize;   // 緩沖區年夜小
 private List<Object> bufferList;

 private final Lock lock = new ReentrantLock(true);
 private final Condition condition = lock.newCondition();

 public ProducerConsumerDemo(int bufferSize) {
  this.bufferSize = bufferSize > 0 ? bufferSize : DEFAULT_BUFFER_SIZE;
  bufferList = new ArrayList<Object>(bufferSize);
 }

 // 臨盆
 public void produce() {
  lock.lock();   // 加鎖
  try {
   while(bufferList.size() == bufferSize) {   // 緩沖區滿了
    System.out.println("Producer wait, thread: " + Thread.currentThread().getName());
    condition.await();
   }

   // 臨盆
   bufferList.add(new Object());
   System.out.println("Producer produce one, now buffer size: "
     + bufferList.size() + ", and thread: " + Thread.currentThread().getName());
   condition.signalAll();   // 告訴花費者
  } catch(InterruptedException e) {
   e.printStackTrace();
  } finally {
   lock.unlock();
  }
 }

 // 花費
 public void consume() {
  lock.lock();   // 加鎖
  try {
   while(bufferList.isEmpty()) {   // 緩沖區空了
    System.out.println("Consumer wait, thread: " + Thread.currentThread().getName());
    condition.await();
   }

   // 花費
   bufferList.remove(0);   // 從鏈表頭部移除一個
   System.out.println("Consumer consumer one, now buffer size: "
     + bufferList.size() + ", and thread: " + Thread.currentThread().getName());
   condition.signalAll();
  } catch(InterruptedException e) {
   e.printStackTrace();
  } finally {
   lock.unlock();
  }
 }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved