多线程-生产者消费者lock锁

package com.hui.生产消费;

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

public class indexLock {
	public static void main(String[] args) {
		Pack p = new Pack();
		producer producer = new producer(p);
		Consumer consumer = new Consumer(p);
		
		Thread thread = new Thread(producer,"生产者1");
		Thread thread2 = new Thread(producer,"生产者2");
		Thread thread3 = new Thread(consumer,"消费者1");
		Thread thread4 = new Thread(consumer,"消费者2");
		thread.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}
}
//面包
class bread{
	private int id;
	public bread(int id) {
		this.id = id;
	}
	public String toString() {
		return "id = "+id;
	}
}
/**
 * Lock 实现提供了比使用 synchronized 方法和语句可获得的更广泛的锁定操作。
 * 	此实现允许更灵活的结构,可以具有差别很大的属性,可以支持多个相关的 Condition 对象。
 * Condition 将 Object 监视器方法(wait、notify 和 notifyAll)分解成截然不同的对象,
 * 	以便通过将这些对象与任意 Lock 实现组合使用,为每个对象提供多个等待 set(wait-set)。
 * 	其中,Lock 替代了 synchronized 方法和语句的使用,Condition 替代了 Object 监视器方法的使用。 
 * @author Hui
 */
class Pack{
	private bread []arr = new bread[10];
	private int count = 0;
	private boolean Empty = true;
	private Lock  lock  = new ReentrantLock();  //同步锁
	private Condition con_pro = lock.newCondition();  //生产者监视器
	private Condition con_cus = lock.newCondition(); // 消费者监视器
	/**
	 * 放入面包
	 * @throws InterruptedException
	 */
	public  void Input() throws InterruptedException {
		lock.lock();
		try {           //这里的try{}finally{}  写法
			while(!Empty){  //为空判断
				System.out.println(Thread.currentThread().getName()+"正在等待");
				con_pro.await();  //生产者等待
			}
			arr[count] = new bread(count+1);
			count++;
			System.out.println(Thread.currentThread().getName()+"正在生产第"+count+"个面包"+arr[count-1]);
			if(count == arr.length){
				Empty = false;
				con_cus.signalAll();  //唤醒消费者所有线程,否者每次只唤醒首先等待的消费者
			}
		} finally {
			lock.unlock();
		}
	}
	/**
	 * 取面包
	 * @throws InterruptedException
	 */
	public void Output() throws InterruptedException{
		lock.lock();
		try {   //这里的try{}finally{}  写法
			while(Empty){ //为空判断
				System.out.println(Thread.currentThread().getName()+"---正在等待");
				con_cus.await();  //消费者等待
			}
			System.out.println(Thread.currentThread().getName()+"---正在消费第"+(11-count)+"个面包"+arr[count-1]);
			count--;
			if(count == 0){
				Empty = true;
				con_pro.signalAll();  //唤醒生产者所有线程,
			}
		} finally {
			lock.unlock();
		}
	}
}
/**
 * 生产者
 * @author Hui
 */
class producer implements Runnable{
	private Pack pack = null;
	private boolean go_on = true;
	public producer(Pack p) {    //传入单个共享资源对象
		this.pack = p;
	}
	public void run(){
		while(go_on){
			try {
				pack.Input();
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
/**
 * 消费者
 * @author Hui
 */
class Consumer implements Runnable{
	private Pack pack = null;
	private boolean go_on = true;
	public Consumer(Pack p) {   //传入单个共享资源对象
		this.pack = p;
	}
	public void run(){
		while(go_on){
			try {
				pack.Output();
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值