【Java 线程通信】

78 篇文章 0 订阅

synchronized

package com.yuzhenc.thread;

/**
 * @author: yuzhenc
 * @date: 2022-04-05 22:07:08
 * @desc: com.yuzhenc.thread
 * @version: 1.0
 */
public class Test12 {
    public static void main(String[] args) {
        Product p = new Product();
        ProducerThread producerThread = new ProducerThread(p);
        CustomerThread customerThread = new CustomerThread(p);
        producerThread.start();
        customerThread.start();
    }
}
class Product {//商品类
    //品牌
    private String brand;
    //名字
    private String name;
    //引入一个灯:true:红色  false 绿色
    boolean flag = false;//默认情况下没有商品 让生产者先生产  然后消费者再消费
    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 synchronized void setProduct(String brand, String name) {
        if(flag == true) {//灯是红色,证明有商品,生产者不生产,等着消费者消费
            try {
                wait();
            } 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();
    }
    public synchronized void getProduct(){
        if(!flag){//flag == false没有商品,等待生产者生产:
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //有商品,消费:
        System.out.println("消费者消费了:" + this.getBrand() + "---" + this.getName());
        //消费完:灯变色:
        flag = false;
        //通知生产者生产:
        notify();
    }
}
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++) {//生产十个商品 i:生产的次数
            if(i % 2 == 0){
                p.setProduct("费列罗","巧克力");
            }else{
                p.setProduct("哈尔滨","啤酒");
            }
        }
    }
}

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++) {//i:消费次数
            p.getProduct();;
        }
    }
}

Lock

  • Condition是在Java 1.5中才出现的,它用来替代传统的Object的wait()、notify()实现线程间的协作,相比使用Object的wait()、notify(),使用Condition1的await()、signal()这种方式实现线程间协作更加安全和高效
  • 在Object的监视器模型上,一个对象拥有一个同步队列和等待队列,而Lock(同步器)拥有一个同步队列和多个等待队列
  • Object中的wait(),notify(),notifyAll()方法是和"同步锁"(synchronized关键字)捆绑使用的;而Condition是需要与"互斥锁"/"共享锁"捆绑使用的\
  • 调用Condition的await()、signal()、signalAll()方法,都必须在lock保护之内,就是说必须在lock.lock()和lock.unlock之间才可以使用
  • void await() throws InterruptedException
    • 造成当前线程在接到信号或被中断之前一直处于等待状态,与此 Condition 相关的锁以原子方式释放,并且出于线程调度的目的,将禁用当前线程,且在发生以下四种情况之一 以前,当前线程将一直处于休眠状态
      • 其他某个线程调用此 Condition 的 signal() 方法,并且碰巧将当前线程选为被唤醒的线程
      • 其他某个线程调用此 Condition 的 signalAll() 方法
      • 其他某个线程中断当前线程,且支持中断线程的挂起
      • 发生“虚假唤醒”
  • void signal()
    • 唤醒一个等待线程,如果所有的线程都在等待此条件,则选择其中的一个唤醒。在从 await 返回之前,该线程必须重新获取锁
  • void signalAll()
    • 唤醒所有等待线程,如果所有的线程都在等待此条件,则唤醒所有线程。在从 await 返回之前,每个线程都必须重新获取锁
package com.yuzhenc.thread;

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

/**
 * @author: yuzhenc
 * @date: 2022-04-05 22:07:08
 * @desc: com.yuzhenc.thread
 * @version: 1.0
 */
public class Test12 {
    public static void main(String[] args) {
        Product p = new Product();
        ProducerThread producerThread = new ProducerThread(p);
        CustomerThread customerThread = new CustomerThread(p);
        producerThread.start();
        customerThread.start();
    }
}
class Product {//商品类
    //品牌
    private String brand;
    //名字
    private String name;
    //声明一个Lock锁:
    Lock lock = new ReentrantLock();
    //搞一个生产者的等待队列:
    Condition produceCondition = lock.newCondition();
    //搞一个消费者的等待队列:
    Condition consumeCondition = 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();
            consumeCondition.signal();
        }finally {
            lock.unlock();
        }
    }
    //消费商品:
    public void getProduct(){
        lock.lock();
        try{
            if(!flag){//flag == false没有商品,等待生产者生产:
                try {
                    // wait();
                    //消费者等待,消费者线程进入等待队列:
                    consumeCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //有商品,消费:
            System.out.println("消费者消费了:" + this.getBrand() + "---" + this.getName());
            //消费完:灯变色:
            flag = false;
            //通知生产者生产:
            //notify();
            produceCondition.signal();
        }finally {
            lock.unlock();
        }
    }
}
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++) {//生产十个商品 i:生产的次数
            if(i % 2 == 0){
                p.setProduct("费列罗","巧克力");
            }else{
                p.setProduct("哈尔滨","啤酒");
            }
        }
    }
}

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++) {//i:消费次数
            p.getProduct();;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sqlboy-yuzhenc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值