併發編程基礎之生產者消費者模式

一:概念java

生產者消費者模式是java併發編程中很經典的併發狀況,首先有一個大的容器,生產者put元素到編程

容器中,消費者take元素出來,若是元素的數量超過容器的容量時,生產者不能再往容器中put元素併發

,處於阻塞狀態,若是元素的數量等於0,則消費者不能在從容器中take數據,處於阻塞狀態。ide

 

二:示例ui

/**
 * 
 */
package com.hlcui.main;

import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Administrator
 *
 */
public class MyQueue {
	
	private LinkedList<String> list = new LinkedList<String>();
	
	private AtomicInteger auto = new AtomicInteger();
	
	private  int MAX_SIZE;
	
	private final int MIN_SIZE = 0;
	
	private final Object lock = new Object();
	
	public MyQueue(int mAX_SIZE) {
		super();
		MAX_SIZE = mAX_SIZE;
	}
	
	public void put(String good) {
		synchronized (lock) {
			while(list.size()==MAX_SIZE) {
				try {
					//若是容器中物品的數量達到上限,那麼就阻塞線程
					System.out.println("容器已滿");
					lock.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			list.add(good);
			auto.incrementAndGet();
			lock.notify();
		}
		
	}
	
	public String take() {
		String s = null;
		synchronized (lock) {
			while(list.size()==MIN_SIZE) {
				try {
					System.out.println("容器已空");
					lock.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			s = list.removeFirst();
			auto.decrementAndGet();
			lock.notify();
		}
		return s;
	}
	
	public int size() {
		return list.size();
	}

	public static void main(String[] args) {
		final MyQueue queue = new MyQueue(5);
		for(int i=0;i<5;i++) {
			queue.put(i+"");
		}
		System.out.println("目前容器中元素數量:"+queue.size());
		ExecutorService executors = Executors.newFixedThreadPool(2);
		executors.execute(new Runnable() {
			@Override
			public void run() {
				for(int i=10;i<20;i++) {
					queue.put(i+"");
					System.out.println("添加元素:"+i+"");
				}
			}
		});
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		executors.execute(new Runnable() {
			@Override
			public void run() {
				for(int i=0;i<50;i++) {
					System.out.println("取出元素:"+queue.take());
				}
			}
		});
	}
}
相關文章
相關標籤/搜索