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

版权声明:知识共享 https://blog.csdn.net/liyaowen505/article/details/78137690
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();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/liyaowen505/article/details/78137690