Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Consumer producer Mode in Java Multithreading

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize the consumer producer model in Java multithreading". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the consumer producer model in Java multithreading".

/ * @ author shijin * in the producer and consumer model To ensure the following points: * 1 there can only be one producer production method locked sychronized * 2 at the same time, there can only be one consumer consumption method locked sychronized * 3 producer production while consumers cannot consume production method locked sychronized * 4 consumer consumption at the same time producers cannot produce consumption method locked sychronized * 5 consumers cannot continue to consume in empty space to judge whether the pre-consumption cycle is empty. If it is empty, wait the thread and release the lock to allow other synchronization methods to execute * 6 when the shared space is full, the producer cannot continue to produce the pre-production cycle to determine whether it is full, if it is full, the thread is wait, and releasing the lock allows other synchronization methods to execute * / / main class class ProducerConsumer {public static void main (String [] args) {StackBasket s = new StackBasket () Producer p = new Producer (s); Consumer c = new Consumer (s); Thread tp = new Thread (p); Thread tc = new Thread (c); tp.start (); tc.start ();}} / / class Mantou {private int id; Mantou (int id) {this.id = id } public String toString () {return "Mantou:" + id;}} / / shared stack space class StackBasket {Mantou sm [] = new Mantou [6]; int index = 0; / * show production method. * show this method is a synchronous method and holds the method lock; * show first loops to determine whether it is full or not, and if it is full, make the thread wait, release the synchronous method lock, and allow consumption. * show first wakes up the waiting consumption method when it is dissatisfied, but it can only get it into a ready state. * consumption can hold synchronous method lock only after show releases synchronous method lock at the end of production * @ param m element * @ return does not return a value * / public synchronized void push (Mantou m) {try {while (index = = sm.length) {System.out.println ("! Production is full! "); this.wait ();} this.notify ();} catch (InterruptedException e) {e.printStackTrace ();} catch (IllegalMonitorStateException e) {e.printStackTrace ();} sm [index] = m; index++ System.out.println ("produced:" + m + "total" + index + "steamed bread");} / * * show consumption method * show this method is a synchronous method, holding the method lock * show first cycle to determine whether it is empty or not, if empty, make the thread wait, release the synchronous method lock, and allow production. * show first wakes up the waiting production method when it is not empty, but it can only get it into the ready state * show and other consumption ends to release the synchronous method lock before the production can hold the lock for production * @ param b true indicates that the display False means hidden * @ return does not return a value * / public synchronized Mantou pop () {try {while (index = = 0) {System.out.println ("! Consumption has run out!); this.wait ();} this.notify ();} catch (InterruptedException e) {e.printStackTrace ();} catch (IllegalMonitorStateException e) {e.printStackTrace ();} index-- System.out.println ("consumption: -" + sm [index] + "total" + index + "steamed bread"); return sm [index];} class Producer implements Runnable {StackBasket ss = new StackBasket (); Producer (StackBasket ss) {this.ss = ss;} / * show production process. * / public void run () {for (int I = 0 + I < 20 +) {Mantou m = new Mantou (I); ss.push (m); / / System.out.println ("produced:" + m + "total" + ss.index + "steamed bread") / / it is inappropriate to test on the above line. Access to index should be in atomic operation, because it may be consumed before this output after push, resulting in output confusion try {Thread.sleep ((int) (Math.random () * 500));} catch (InterruptedException e) {e.printStackTrace () }} class Consumer implements Runnable {StackBasket ss = new StackBasket (); Consumer (StackBasket ss) {this.ss = ss;} / * show consumption process. * / public void run () {for (int I = 0 + I < 20 +) {Mantou m = ss.pop (); / / System.out.println ("consumption: -" + m + "total" + ss.index + "steamed bun") / / it is also inappropriate to test on the above line. Access to index should be in atomic operation, because it may be produced before this output after pop, resulting in output confusion try {Thread.sleep ((int) (Math.random () * 1000));} catch (InterruptedException e) {e.printStackTrace () }} Thank you for your reading, the above is the content of "how to realize the consumer producer mode in Java multithreading". After the study of this article, I believe you have a deeper understanding of how to realize the consumer producer mode in Java multithreading, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report