java多线程,等待唤醒机制,生产者消费者问题

**# java多线程等待唤醒机制,生产者消费者问题
涉及的类:
包子铺类线程,消费者线程,包子类,测试类。
包子铺线程:

import java.util.Random;
/**
 * @ClassName

> 

 - [ ] 
 - ~~这里是引用~~ 

: SteamedBunRestaurant
 * @Description 本类功能:包子铺线程,生产包子
 * @Author lzl
 * @Date 2020.10.03
 * @Time 20:24
 */
public class SteamedBunRestaurant implements Runnable {
    private BaoZi baoZi;
    @Override
    public void run ( ) {
        while ( true ) {
            synchronized ( baoZi ) {
                if ( baoZi.isStatus () == false ) {
                    String filling;
                    System.out.println ( "生产一个" + ( filling = new Random ().nextInt ( 2 ) > 0 ? "猪肉白菜" : "蛋黄" ) + "馅的包子" );
                    baoZi.setFilling ( filling );
                    try {
                        System.out.println ( "等待5秒生产包子........" );
                        Thread.sleep ( 5000 );
                    } catch ( InterruptedException e ) {
                        e.printStackTrace ();
                    }
                    System.out.println ( "生产完成,重设包子状态,唤醒消费者线程=========" );
                    baoZi.setStatus ( true );
                    baoZi.notify ();
                } else {
                    try {
                        baoZi.wait ();
                    } catch ( InterruptedException e ) {
                        e.printStackTrace ();
                    }
                }
            }
        }
    }
    public SteamedBunRestaurant ( BaoZi baoZi ) {
        this.baoZi = baoZi;
    }
    public SteamedBunRestaurant ( ) {
    }
    public BaoZi getBaoZi ( ) {
        return baoZi;
    }
    public void setBaoZi ( BaoZi baoZi ) {
        this.baoZi = baoZi;
    }
}

消费者线程,用于吃包子:

/**
 * @ClassName: Consumers
 * @Description 本类功能:消费者线程,用于吃包子
 * @Author lzl
 * @Date 2020.10.03
 * @Time 19:56
 */
public class Consumers implements Runnable {
    private BaoZi baoZi;
    @Override
    public void run ( ) {
        while ( true ) {
            synchronized ( baoZi ) {
                if ( baoZi.isStatus () == true ) {
                    System.out.println ( "包子做好了,吃掉" + baoZi.getFilling () + "馅的包子" );
                    try {
                        System.out.println ( "等待5秒吃掉包子........" );
                        Thread.sleep ( 5000 );
                    } catch ( InterruptedException e ) {
                        e.printStackTrace ();
                    }
                    System.out.println ( "吃掉包子,重设包子状态,唤醒包子铺线程-----------------" );
                    System.out.println ( "==================================================" );
                    System.out.println ();
                    System.out.println ();
                    baoZi.setStatus ( false );
                    baoZi.notify ();
                } else {
                    try {
                        baoZi.wait ();
                    } catch ( InterruptedException e ) {
                        e.printStackTrace ();
                    }
                }
            }
        }
    }
    public Consumers ( BaoZi baoZi ) {
        this.baoZi = baoZi;
    }
    public Consumers ( ) {
    }
    public BaoZi getBaoZi ( ) {
        return baoZi;
    }
    public void setBaoZi ( BaoZi baoZi ) {
        this.baoZi = baoZi;
    }
}

包子类,保存包子的状态和馅,状态status,false为有包子,true为没有包子

/**
 * @ClassName: BaoZi
 * @Description 本类功能:包子类,储存包子状态和馅
 * @Author lzl
 * @Date 2020.10.03
 * @Time 20:24
 */
public class BaoZi {
    private boolean status=false;
    private String filling;
    public BaoZi ( boolean status , String filling ) {
        this.status = status;
        this.filling = filling;
    }
    public boolean isStatus ( ) {
        return status;
    }
    public void setStatus ( boolean status ) {
        this.status = status;
    }
    public String getFilling ( ) {
        return filling;
    }
    public void setFilling ( String filling ) {
        this.filling = filling;
    }
    public BaoZi ( ) {
    }
}

测试类,实现生产者消费者问题:

public class TestBaoZi {
    public static void main ( String[] args ) {
        BaoZi baoZi = new BaoZi ();
        SteamedBunRestaurant steamedBunRestaurant = new SteamedBunRestaurant ( baoZi );
        Consumers consumers = new Consumers ( baoZi );
        Thread producers = new Thread ( steamedBunRestaurant );
        Thread customer = new Thread ( consumers );
        producers.start ();
        customer.start ();
    }
}

程序截图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生产者消费者问题是一种经典的多线程同步问题,主要涉及到生产者消费者之间的数据交换和同步。 在Java中,可以使用多种方式来实现生产者消费者问题的解决方案,其中最常用的方式是使用wait()和notify()方法来实现线程间的同步。 下面是一个简单的示例,演示了如何使用wait()和notify()方法来实现生产者消费者问题的解决方案: ``` import java.util.LinkedList; import java.util.Queue; public class ProducerConsumer { private Queue<Integer> buffer = new LinkedList<>(); private final int capacity = 5; public void produce() throws InterruptedException { int value = 0; while (true) { synchronized (this) { while (buffer.size() == capacity) wait(); System.out.println("Producer produced-" + value); buffer.add(value++); notify(); Thread.sleep(1000); } } } public void consume() throws InterruptedException { while (true) { synchronized (this) { while (buffer.size() == 0) wait(); int val = buffer.poll(); System.out.println("Consumer consumed-" + val); notify(); Thread.sleep(1000); } } } } ``` 在上面的示例中,ProducerConsumer类表示生产者消费者问题的解决方案。这个类有一个buffer队列,它是一个FIFO队列,用于存储生产者生产的数据。 produce()方法表示生产者的行为,它使用一个while(true)循环来不断地生产数据。在每次生产之前,它首先检查buffer队列是否已满。如果buffer队列已满,则调用wait()方法来等待消费者线程消费数据。如果buffer队列未满,则将生产的数据添加到buffer队列中,并调用notify()方法通知消费者线程可以消费数据了。 consume()方法表示消费者的行为,它使用一个while(true)循环来不断地消费数据。在每次消费之前,它首先检查buffer队列是否为空。如果buffer队列为空,则调用wait()方法来等待生产者线程生产数据。如果buffer队列不为空,则从buffer队列中取出一个数据,并调用notify()方法通知生产者线程可以生产数据了。 在上面的示例中,使用了synchronized关键字来实现线程的同步。synchronized关键字可以确保在同一时间只有一个线程可以执行被synchronized关键字包裹的代码块。wait()方法可以让线等待,直到被notify()方法唤醒。notify()方法可以唤醒一个等待线程。如果有多个线等待,则只有一个线程会被唤醒

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值