In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people don't understand the knowledge points of this article "Example Analysis of Producer and Consumer Problems in Java", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "Example Analysis of Producer and Consumer Problems in Java".
application scenarios
Assuming that only one product can be stored in the warehouse, the producer puts the produced product into the warehouse, and the consumer takes the product out of the warehouse for consumption.
If there is no product in the warehouse, the producer puts the product in the warehouse, otherwise stops production and waits until the product in the warehouse is taken away by the consumer.
If there is a product in the warehouse, the consumer can take the product away for consumption, otherwise stop consuming and wait until the product is put into the warehouse again.
analysis
This is a thread synchronization problem, where producers and consumers share the same resource, and producers and consumers are interdependent and conditional on each other.
For producers, consumers should be notified to wait before they produce products, and consumers should be notified immediately after they produce products.
For consumers, after consumption, inform producers that consumption has ended and new products need to be produced for consumption
In producer-consumer problems, synchronized alone is not enough.
synchronized prevents concurrent updates to the same shared resource, enabling synchronization
synchronized cannot be used to implement message passing (communication) between threads
Java provides several ways to solve communication problems between threads
wait() means that the thread waits until notified by another thread, unlike sleep, wait() releases the lock wait(long timeout) specifies the number of milliseconds to wait notify() wakes up a waiting thread notifyAll() wakes up all threads calling the wait() method on the same object, threads with high priority are scheduled first
Note: All methods of Object class can only be used in synchronous methods or synchronous code blocks, otherwise an exception will be thrown.
Solution Management Method
Producer: Module responsible for producing data (may be method, object, thread, process)
Consumer: Module responsible for processing data (may be method, object, thread, process)
Buffer: Consumers cannot directly use producer data, there is a "buffer" between them
Producer puts produced data into buffer, consumer takes data out of buffer
Code Case
//solve producer-consumer models using buffers public class TestPC { public static void main(String[] args) { SynContainer container = new SynContainer(); new Producer(container).start(); new Consumer(container).start(); }}class Producer extends Thread{ SynContainer synContainer; public Producer(SynContainer synContainer){ this.synContainer = synContainer; } //Production @Override public void run() { for (int i = 0; i
< 100; i++) { synContainer.push(new Chicken(i)); System.out.println("生产了"+i+"只鸡"); } }}class Consumer extends Thread{ SynContainer synContainer; public Consumer(SynContainer synContainer){ this.synContainer = synContainer; } // 消费 @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("消费了-->"+synContainer.pop().id+"只鸡"); } }}//产品class Chicken{ int id; public Chicken(int id) { this.id = id; }}//缓冲区class SynContainer{ Chicken[] chickens = new Chicken[10]; int count = 0; //生产者放入产品 public synchronized void push(Chicken chicken){ //如果容器满了就要等到消费者消费 if (count==chickens.length){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } //通知消费者消费,自身进入等待 } //没有满的话则生产 chickens[count] = chicken; count++; //通知消费者消费 this.notifyAll(); } //生产者消费产品 public synchronized Chicken pop(){ if (count==0){ //等待生产者生产 try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } count--; Chicken chicken = chickens[count]; //消费了,可以通知生产者继续生产 this.notifyAll(); return chicken; }}信号灯法
通过标志位 true 或者 false 来进行判断
代码案例
//信号灯法测试生产者消费者模型 即标志位public class TestPC2 { public static void main(String[] args) { TV tv = new TV(); new Player(tv).start(); new Audiance(tv).start(); }}//生产者-->演员class Player extends Thread { TV tv; public Player(TV tv){ this.tv = tv; } @Override public void run() { for (int i = 0; i
< 20; i++) { if (i%2==0){ this.tv.play("天天向上"); }else { this.tv.play("抖音广告"); } } }}//消费者-->Audience class extends Thread{ TV tv; public Audiance(TV tv){ this.tv = tv; } @Override public void run() { for (int i = 0; i
< 20; i++) { tv.watch(); } }}//产品-->Program class TV{ //actor performing audience waiting T //Audience watching actor waiting F String show;//Show boolean flag = true; //Show public synchronized void play(String show){ if (! flag){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("actor performed: "+show); //notify the audience this.notifyAll(); this.show = show; this.flag = ! this.flag; } //Watch public synchronized void watch(){ if (flag){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("View"+show); //notify actors to perform this.notifyAll(); this.flag = ! this.flag; }} The above is the content of this article about "Example Analysis of Producer and Consumer Problems in Java". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will help everyone. If you want to know more relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.