来两发生产者消费者多线程的问题

使用wait和notify/notifyAll来实现

/**
 * 固定容量同步容器,拥有put和get方法,能够支持2个生产者线程以及10个消费者线程的阻塞调用
 *  使用wait和notify/notifyAll来实现 
 * @author buag
 */
package com.buag.demo;

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

public class MyBox<T> {
	final private LinkedList<T> lists = new LinkedList<>();
	final private int MAX = 10; //这里定义10个元素
	private int count = 0;
	public synchronized void put(T t) {
		while(lists.size() == MAX) { //想想为什么用while而不是用if?因为if判断一次lists.size()就不再判断,而while反之
			try {
				this.wait(); //阻塞生产者
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		lists.add(t);
		count++;
		this.notifyAll(); //解塞,通知消费者线程进行消费
	}
	
	public synchronized T get() {
		T t = null;
		while(lists.size() == 0) {
			try {
				this.wait();//阻塞消费者
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		t = lists.removeFirst();//返回此列表的第一个元素
		count --;
		this.notifyAll(); //通知生产者进行生产
		return t;//返回第一个元素
	}
	
	public static void main(String[] args) {
		MyBox<String> m = new MyBox<>();
		
		for(int i=0; i<10; i++) {//启动消费者线程
			new Thread(()->{
				for(int j=0; j<5; j++) System.out.println(m.get());
			}, "c" + i).start();
		}
		
		try {
			TimeUnit.SECONDS.sleep(1);//睡一下
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//启动生产者的线程
		for(int i=0; i<2; i++) {
			new Thread(()->{
				for(int j=0; j<25; j++) m.put(Thread.currentThread().getName() + " " + j);
			}, "p" + i).start();
		}
	}
}

使用Lock和Condition来实现

/**
 * 使用Lock和Condition来实现
 * @author buag
 */
package com.buag.demo;

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

public class MyBox_2<T> {
	final private LinkedList<T> lists = new LinkedList<>();
	final private int MAX = 10; //定义10个元素
	private int count = 0;
	
	private Lock lock = new ReentrantLock();
	private Condition producer = lock.newCondition();
	private Condition consumer = lock.newCondition();
	
	public void put(T t) {
		try {
			lock.lock();
			while(lists.size() == MAX) { //想想为什么用while而不是用if?producer.await();醒了后必须再判断下lists.size()
				producer.await();
			}
			
			lists.add(t);
			count++;
			consumer.signalAll(); //通知消费者线程进行消费
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public T get() {
		T t = null;
		try {
			lock.lock();
			while(lists.size() == 0) {
				consumer.await();
			}
			t = lists.removeFirst();//返回此列表的第一个元素
			count --;
			producer.signalAll(); //通知所有生产者进行生产
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//必须要释放锁
		}
		return t;
	}
	
	public static void main(String[] args) {
		MyBox_2<String> m2 = new MyBox_2<>();
		//启动消费者线程
		for(int i=0; i<10; i++) {
			new Thread(()->{
				for(int j=0; j<5; j++) System.out.println(m2.get());
			}, "c" + i).start();
		}
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//启动生产者线程
            for(int i=0; i<2; i++) {
			new Thread(()->{
				for(int j=0; j<25; j++) m2.put(Thread.currentThread().getName() + " " + j);
			}, "p" + i).start();
		}
	}
}

总结:对比这两种方式,Condition的方式可以更加精确的指定哪些线程被唤醒。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值