java 多线程 消费者_【多线程】java多线程实现生产者消费者模式

一对一的生产者消费者模式:

早餐类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package com.sxd.swapping.test.ProducerAndConsumerTest;

/**

* 早餐基础类

*

* wait()

* notify()

* notifyAll()

* 三个方法 需要放在同步代码块中执行 因为要获取对象锁

*/

public class Breakfast{

private String food;

private String drink;

private boolean flag = false;//flag = false 表示需要生产 flag = true 表示需要消费

public synchronized void makeBreakfast(String food,String drink){

System.out.println("生产者进入--->标志值为:"+flag);

if (flag){

try {

System.out.println("make---wait()暂停,释放对象锁");

wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

this.food = food;

try {

System.out.println("make---sleep()休眠,不释放对象锁");

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

this.drink = drink;

System.out.println("make---生产者制造东西完成----");

this.flag = true;

System.out.println("make---notify()唤醒,标志值为"+flag);

notify();

}

public synchronized void eatBreakfast(){

System.out.println("消费者进入--->标志值为:"+flag);

if(!flag){

try {

System.out.println("eat---wait()");

wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

try {

System.out.println("eat---sleep()");

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("吃东西---"+this.food+";喝东西---"+this.drink);

this.flag = false;

System.out.println("eat---notify()唤醒,标志值为"+flag);

notify();

}

}

View Code

生产者类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package com.sxd.swapping.test.ProducerAndConsumerTest;

public class Producer implements Runnable{

private Breakfast breakfast;

public Producer(Breakfast breakfast) {

this.breakfast = breakfast;

}

@Override

public void run() {

int i = 7;

for (int i1 = 0; i1 < i; i1++) {

if (i1 %2 == 0){

this.breakfast.makeBreakfast("馒头","豆浆");

}else {

this.breakfast.makeBreakfast("面包","冷饮");

}

}

}

}

View Code

消费者类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package com.sxd.swapping.test.ProducerAndConsumerTest;

public class Consumer implements Runnable{

private Breakfast breakfast;

public Consumer(Breakfast breakfast) {

this.breakfast = breakfast;

}

@Override

public void run() {

int i = 7;

for (int i1 = 0; i1 < i; i1++) {

System.out.println("星期"+(i1+1)+"---消费者要来吃东西了");

this.breakfast.eatBreakfast();

}

}

}

View Code

线程启动主测试类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package com.sxd.swapping.test.ProducerAndConsumerTest;

public class Test {

public static void main(String[] args) {

Breakfast breakfast = new Breakfast();

new Thread(new Producer(breakfast)).start();

new Thread(new Consumer(breakfast)).start();

}

}

View Code

展示结果:

4cd09b60728310e271942a392f363657.png

思考问题:

1.为什么用wait()+notify()实现

2.为什么wait()、notify()、notifyAll()方法需要放在同步代码块中执行?

3.wait()是暂停的哪个线程?notify()唤醒的是哪个线程?

标签:java,生产者,System,flag,println,breakfast,多线程,public,out

来源: https://www.cnblogs.com/sxdcgaq8080/p/10654201.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值