当前位置:网站首页>Producer consumer mode (multithreading, use of shared resources)
Producer consumer mode (multithreading, use of shared resources)
2022-07-03 20:20:00 【Chen4852010】
1.BlockingQueue( Blocking queues ) yes java A common container in , It is widely used in multithreaded programming . When the queue container is full, the producer thread is blocked , Do not continue until the queue is not full put; When the queue container is empty , Consumer thread blocked , You cannot continue until the queue is not empty take.
We use blocking queues as shared resources
public class Producer implements Runnable {
private final BlockingQueue<KouZhao> blockingQueue;
public Producer(BlockingQueue<KouZhao> blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(200);
if (blockingQueue.remainingCapacity() > 0) {
KouZhao kouZhao = new KouZhao(UUID.randomUUID().toString(), "N95");
blockingQueue.add(kouZhao);
System.out.println(" I'm making masks , The current inventory is :" + blockingQueue.size());
}
else {
System.out.println(" My warehouse is full " + blockingQueue.size() + " Masks , Come and buy a mask !");
}
} catch (InterruptedException e) {
e.printStackTrace(); }
}
}
}
public class Consumer implements Runnable {
private final BlockingQueue<KouZhao> blockingQueue;
public Consumer(BlockingQueue<KouZhao> blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
long startTime = System.currentTimeMillis(); // Get start time
KouZhao kouZhao = blockingQueue.take();
long endTime = System.currentTimeMillis(); // Get the end time
System.out.println(" I consumed masks :" + kouZhao + ", When the goods arrive, I'll stop It's jammed " + (endTime - startTime) + "ms");
} catch (InterruptedException e) {
e.printStackTrace(); }
}
}
}
Now the point is , How to be in producer and comsumer In two threads Have shared resources
First of all, these two run Threads , Resources available now ,producer and consumer Inside the object blockingQueue,
Because although two threads are opened (run Is the thread ID ), But the first method stack of thread , Also called Of these two objects , Example method run( In the instance method, you can get , Of the instance properties of the object )
be equal to run Threads ( It's just a sign , A stack space ) and run In the thread producer Of example run Method The difference between
therefore as long as producer and consumer Inside the object blockingQueue, Point to the same blockingQueue that will do
So as long as it's in establish producer and consumer When the object , Pass them the same blockingQueue that will do
public class App {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<KouZhao> queue = new ArrayBlockingQueue<>(20);
Producer producer = new Producer(queue);
Consumer consumer = new Consumer(queue);
// To start a run Threads ( The identification of the thread is run), The main method stack is producer Of run Method
new Thread(producer).start();
Thread.sleep(20000);
// To start a run Threads ( The identification of the thread is run), The main method stack is producer Of run Method
new Thread(consumer).start();
}
}
Obviously, the above code cannot be put into the production environment , For example, there is no cluster , No distribution , The playing method is too single , Cannot satisfy the enterprise
Requirements for class a Applications ...
such as :
Is the message persistent ?
How to make sure that the message will be sent successfully ?
How to make sure that the message will be consumed successfully ?
How about the performance of high-speed parallel delivery ?
Is the system reliable ?
Is there any Pub/Sub Pattern ?
Has current limiting been considered ?
...
边栏推荐
- [Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
- unittest框架基本使用
- Exercises of function recursion
- Point cloud data denoising
- Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
- Typora charges, WTF? Still need support
- Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
- Native table - scroll - merge function
- Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
- What is the difference between a kill process and a close process- What are the differences between kill process and close process?
猜你喜欢

BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu

Detailed and not wordy. Share the win10 tutorial of computer reinstallation system

强基计划 数学相关书籍 推荐

你真的知道自己多大了吗?

Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520

Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla

It is discussed that the success of Vit lies not in attention. Shiftvit uses the precision of swing transformer to outperform the speed of RESNET

IP address is such an important knowledge that it's useless to listen to a younger student?

Native table - scroll - merge function

BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
随机推荐
Microservice framework - frequently asked questions
Acquisition and transmission of parameters in automatic testing of JMeter interface
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
Unittest framework is basically used
How to set the system volume programmatically- How to programmatically set the system volume?
Instructions for common methods of regular expressions
Ruby replaces gem Alibaba image
Based on laravel 5.5\5.6\5 X solution to the failure of installing laravel ide helper
Global and Chinese markets of cast iron diaphragm valves 2022-2028: Research Report on technology, participants, trends, market size and share
4. Data splitting of Flink real-time project
The 29th day of force deduction (DP topic)
Explore the internal mechanism of modern browsers (I) (original translation)
Parental delegation mechanism
Deep search DFS + wide search BFS + traversal of trees and graphs + topological sequence (template article acwing)
Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
2.5 conversion of different data types (2)
FPGA 学习笔记:Vivado 2019.1 工程创建
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
Nacos usage of micro services
Fingerprint password lock based on Hal Library