阻塞队列生产者消费者

package com.reentrant;

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

public class ArrayBlockingQueue<E> {
	private Lock lock;
	private Condition NotNull;  //队列不为空的waiting room  消费者
	private Condition NotFull;  //队列不满的waiting room		生产者
	private int capacity;//总容量
	private int rear;//队尾
	private int front;//队头
	private int size;//当前容量
	private Object[] items;
	public int size(){
		lock.lock();
		try {
			return this.size;
		} finally {
			lock.unlock();
		}
	}
	
	public ArrayBlockingQueue(int capacity){
		this.capacity = capacity;
		lock = new ReentrantLock();
		NotNull = lock.newCondition();
		NotFull = lock.newCondition();
		items = new Object[capacity];
		size = rear = front = 0;
	}
	//队列满,阻塞;队列不满,插入元素;
	public E put(E e) throws InterruptedException{
		lock.lock();
		try{
			//if
			while
			(capacity == size){
				NotFull.await();
			}
			items[rear] = e;
			if(++rear == items.length){
				rear = 0;
			}
			size++;
			System.out.printf("当前put线程=%s,生产的元素=%s,生产之后队列长度=%d,%n",Thread.currentThread().getName(),e,size());
			Thread.sleep(1000);
			NotNull.signalAll();
			return e;
		}finally{
			lock.unlock();
		}
	}
	//队列空,阻塞;队列不空,取出元素;
	public E take() throws InterruptedException{
		lock.lock();
		try{
			//if
			while
			(0 == size){
				NotNull.await();
			}
			Object x = items[front];
			items[front] = null;
			if(++front == items.length){ 
				front = 0;
			}
			size--;
			System.out.printf("当前take线程=%s,消费的元素=%s,消费之后队列长度=%d,%n",Thread.currentThread().getName(),x,size());
			Thread.sleep(1000);
			NotFull.signalAll();
			return (E)x;
		}finally{
			lock.unlock();
		}
	}
}

package com.reentrant;


public class ArrayBlockingQueueApp {
	public static void main(String[] args) throws InterruptedException {
		ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(5);
		Runnable taketask1 = ()->{
			while(true){
				try {
					queue.take();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		Runnable taketask2 = ()->{
			while(true){
				try {
					queue.take();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		Runnable puttask1 = ()->{
			int i = 0;
			while(true){
				try {
					queue.put(i+"");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				//System.out.printf("当前put线程=%s,生产的元素=%s,生产之后队列长度=%d,%n",Thread.currentThread().getName(),i,queue.size());
				i++;
			}
	};
		Thread take1 = new Thread(taketask1,"[消费者1]");
		Thread take2 = new Thread(taketask2,"[消费者2]");
		Thread put1 = new Thread(puttask1,"[生产者1]");
	
		take2.start();
		//Thread.sleep(10000);
		put1.start();
		take1.start();
		
	}
}

在这里插入图片描述

发布了39 篇原创文章 · 获赞 3 · 访问量 3352

猜你喜欢

转载自blog.csdn.net/administratorJWT/article/details/101032080