生产者消费者模型的实现(线程通信)

云惠网小编 2022年1月15日13:17:46
评论
2638字阅读8分47秒
摘要

目录实现一:实现二:实现一:package com.atguigu.juc;import java.util.LinkedList;public class ProductConsumerTest2 { public static void main(String[] args) { MessageQueue messageQueue = new MessageQueue(3); for (int i = 0; i <= 3…

广告也精彩

实现一:


 实现一:

package com.atguigu.thread;
/**
* 线程通信的应用:经典例题: 生产者消费者问题
*/
public class ProduceConsumerTest {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Producer p1 = new Producer(clerk);
Consumer c1 = new Consumer(clerk);
p1.start();
c1.start();
}
}
class Clerk{
private int product = 0;
public void produce() {
while(true){
synchronized (this){
//                this.notify();
if(product < 20){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
product++;
System.out.println(Thread.currentThread().getName()+"生产者生产产品"+product);
this.notify();
}else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public void consume() {
while (true) {
synchronized (this) {
//                this.notify();
if (product > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
product--;
System.out.println(Thread.currentThread().getName() + "消费者消费产品" + product);
this.notify();
} else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class Producer extends Thread{
private Clerk clerk;
public Producer(Clerk clerk){
this.clerk=clerk;
}
@Override
public void run() {
clerk.produce();
}
}
class Consumer extends Thread{
private Clerk clerk;
public Consumer(Clerk clerk){
this.clerk=clerk;
}
@Override
public void run() {
clerk.consume();
}
}

 

 实现二: 

 实现二: 

目录

package com.atguigu.juc;
import java.util.LinkedList;
public class ProductConsumerTest2 {
public static void main(String[] args) {
MessageQueue messageQueue = new MessageQueue(3);
for (int i = 0; i <= 3; i++) {
int id=i;
new Thread(()->{
while(true){
messageQueue.produce(new Message(id,id));
}
},"生产者").start();
}
new Thread(()->{
while(true){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
messageQueue.take();
}
},"消费者").start();
}
}
class MessageQueue{
private LinkedList<Message> list=new LinkedList<>();
private int capCity;
public MessageQueue(int capCity) {
this.capCity = capCity;
}
public void take(){
synchronized (list){//消费
if(list.isEmpty()){
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
Message message = list.removeFirst();
System.out.println("消费者消费"+message.getId()+" "+message.getVal());
list.notify();//这里注意要用list调用notify  不然锁的对象不同
}
}
}
public void produce(Message message){//生产
synchronized (list){
if(list.size() == capCity){
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("生产者已生产满仓");
}else {
list.add(new Message(message.getId(),message.getVal()));
System.out.println("生产者生产"+message.getId()+" "+message.getVal());
list.notify();//这里注意要用list
}
}
}
}
class Message{
private int id;
private int val;
@Override
public String toString() {
return "Message{" +
"id=" + id +
", val=" + val +
'}';
}
public Message(int id, int val) {
this.id = id;
this.val = val;
}
public int getId() {
return id;
}
public int getVal() {
return val;
}
}

本文转自 https://blog.csdn.net/m0_60264772/article/details/122481207

腾讯云618
云惠网小编
SpringCloud -- Config、Bus解析 java

SpringCloud — Config、Bus解析

1、Config1.1、概述简介1. 分布式面临的问题微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要...
Java数据结构-了解复杂度 java

Java数据结构-了解复杂度

2.实例分析与计算  四.写在最后  // 计算斐波那契递归fibonacci的时间复杂度 int fibonacci(int N) { return N < 2 ? N : fibonacci...
Java数据结构-认识顺序表 java

Java数据结构-认识顺序表

目录二.顺序表1.概念及结构2.顺序表的实现打印顺序表获取顺序表的有效长度在pos位置新增元素判断是否包含某个元素查找某个元素对应的位置获取/查找pos位置的元素给pos位置的元素...
腾讯云618

发表评论