当前位置:网站首页>Producer consumer model of concurrent programming
Producer consumer model of concurrent programming
2022-07-27 01:31:00 【Coffee without ice or sugar】
What problems do producers and consumers solve ?
- Synchronization between multiple threads problem .
- Solve the problems of producers and consumers Strong coupling problem .
Reference code :
package Top;
import java.util.LinkedList;
import java.util.Random;
/** * @Author 86180 * @Date 2020/9/7 21:50 * @Version 1.0 **/
public class ProductorConsumer {
public static void main(String[] args) {
MessageQueue messageQueue = new MessageQueue(2);
for(int i = 0; i < 3; i++){
new Thread(new Productor(messageQueue, new Message(i, " news i")), " Producer thread "+i).start();
}
for(int i = 0; i < 1; i++){
new Thread(new Consumer(messageQueue), " Consumer thread "+i).start();
}
}
}
class MessageQueue{
private int capacity;
LinkedList<Message> list;
MessageQueue(int capacity){
list = new LinkedList<>();
this.capacity = capacity;
}
public synchronized void put(Message message){
while (list.size() == capacity) {
try {
System.out.println(" The queue is full , Producer waiting .");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// If you use if To judge , Pay attention to the order : Put it in addLast front
if(list.isEmpty())this.notifyAll();
list.addLast(message);
System.out.println(Thread.currentThread().getName()+" Produced " + message.getId());
}
public synchronized Message get(){
while(list.isEmpty()){
try {
System.out.println(" The queue is empty , Producer thread waiting .");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(list.size() == capacity)this.notifyAll();// If you use if To judge , Pay attention to the order : Put it in removeFirst front
Message message = list.removeFirst();
System.out.println(Thread.currentThread().getName()+" The consumption "+ message.getId());
return message;
}
}
class Productor implements Runnable{
MessageQueue ms;
Message message;
Productor(MessageQueue messageQueue, Message message){
this.ms = messageQueue;
this.message = message;
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(new Random().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
ms.put(message);
}
}
}
class Consumer implements Runnable{
MessageQueue ms;
Consumer(MessageQueue messageQueue){
this.ms = messageQueue;
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(new Random().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
ms.get();
}
}
}
class Message{
private int id;
private Object value;
Message(int id, Object value){
this.id = id;
this.value = value;
}
public int getId(){
return id;
}
}
Define a Message class , Indicates the messages stored in the message queue .
Define a MessageQueue class , contain put() and get().
Define a producer class Productor.
Define a consumer class Consumer.
边栏推荐
- 做题笔记1
- 4. European Champions League
- Navicat operation database
- Unity screenshot widget
- The difference between if and else if
- ESP8266 STA_ UDP_ Client
- 【unity】Unity界面scene视图[1]
- Adding, deleting, checking and modifying dynamic sequence table with C language
- 5. Xshell connection server denied access, password error
- Esp8266 access to cloud platform ----- DNS domain name connection server
猜你喜欢

软件测试面试题之软件基础

Cannot find a valid baseurl for repo: HDP-3.1-repo-1

Unity screenshot widget

Some simple extension methods commonly used by unity

ESP8266 AP_TCP_Server

ESP8266 AP_ UDP_ Server

4.root用户登录

MySQL closes the problem of automatic submission of connection transactions

What is the digital economy and how does it change the business model?

iptables防火墙(二)
随机推荐
Doris or starrocks JMeter pressure measurement
Jenkins--基础--04--安装中文插件
Unity twitter login access
大四老学长的自我批评记录
Promoting practice with competition - Reflection on the 303th weekly match
十三、命令小工具
[untitled]
Scoring system based on 485 bus
Li Kou brushes 300 record posts
Some simple extension methods commonly used by unity
The setup of KEIL development environment is delivered to the installation package
Unity Twitter登录接入
Esp8266 --- JSON data exchange format
ESP8266 AP_TCP_Client
顺序表之OJ题
Basic syntax of Verilog
RS485 signal measurement
c语言实现三子棋游戏
Unity screenshot widget
Six ways for the Internet of things to improve our lives