✅作者简介:我是18shou,一名即将秋招的java实习生
🔥系列专栏:牛客刷题专栏
📃推荐一款模拟面试、刷题神器👉 [在线刷题面经模拟面试](在线面试刷题)
目录
实现一:
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;
}
}
实现二:
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();
}
}