Lock锁情况下的线程通信

 

 

 

更改 代码:

复制代码
package com.msb.test12;

import jdk.nashorn.internal.objects.annotations.Constructor;

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

/**
 * @author : liu
 * 日期:15:42:06
 * 描述:IntelliJ IDEA
 * 版本:1.0
 */
public class Product {//商品类
    //品牌
    private String brand;
    //名字
    private String name;
    //声明一个Lock锁
    Lock lock =new ReentrantLock();
    //搞一个生产者的等待队列
    Condition produceCondition=lock.newCondition();
    //搞一个消费者的等待队列
    Condition customerCondition=lock.newCondition();
    //引入一个标记“灯”true 红色 false绿色
    boolean flag=false; //默认情况下没有商品,让生产者先生产再消费
    //setter,getter方法:
    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //生产商品
    public  void setProduct(String brand ,String name){
        //打开锁
        lock.lock();
        try{
            if(flag==true){
                try {
                    //生产者阻塞,生产者进入等待队列'
                    //wait();
                    produceCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.setBrand(brand);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.setName(name);

            System.out.println("生产者生产了:"+this.getBrand() + "---"+ this.getName());
            //生产完毕:灯变色
            flag=true;
            //告诉消费者赶紧来消费
            //notify();
            customerCondition.signalAll();
        }finally {
            //关闭锁
            lock.unlock();
        }

    }
    //消费商品
    public  void getProduct(){
        //打开锁
        lock.lock();
        try{
            if (flag==false){//没有商品等待生产者生产
                try {
                    //消费者进入阻塞队列
                    //wait();
                    customerCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //有商品,消费:
            System.out.println("消费者消费了:"+ this.getBrand()+"---"+ this.getName()+Thread.currentThread().getName());
            //消费完:灯变色
            flag=false;
            //告诉生产者生产
            //notify();
            produceCondition.signal();

        }finally {
            //关闭锁
            lock.unlock();
        }

    }
}
复制代码
复制代码
package com.msb.test12;

/**
 * @author : liu
 * 日期:16:02:52
 * 描述:IntelliJ IDEA
 * 版本:1.0
 */
public class CustomerThread extends Thread{//消费者线程
    //共享商品资源
    private Product p;

    public CustomerThread(Product p) {
        this.p = p;
    }

    @Override
    public  void run() {
       for (int i = 1; i <= 10 ; i++) {//消费次数
                p.getProduct();
            }
       

    }
}
复制代码
复制代码
package com.msb.test12;

/**
 * @author : liu
 * 日期:15:45:09
 * 描述:IntelliJ IDEA
 * 版本:1.0
 */
public class ProducerThread extends Thread{//生产者线程
    //共享商品
    private Product p;

    public ProducerThread(Product p) {
        this.p = p;
    }
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {//生产十个商品
            if (i%2==0){
                p.setProduct("费列罗","巧克力");
            }else{
                p.setProduct("哈尔滨","啤酒");
            }
        }
    }
}
复制代码
复制代码
package com.msb.test12;

/**
 * @author : liu
 * 日期:16:07:36
 * 描述:IntelliJ IDEA
 * 版本:1.0
 */
public class Test {
    //这是一个main方法:是程序的入口
    public static void main(String[] args) {
        //共享商品
        Product p=new Product();
        ProducerThread pt=new ProducerThread(p);
        CustomerThread ct=new CustomerThread(p);
        //CustomerThread ct1=new CustomerThread(p);
        pt.start();
        ct.start();
        //ct1.start();
    }
}
复制代码

 

原理:

 

posted @   爱的加勒比  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示