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

Detailed explanation of producer-Consumer Model and how to implement it through java Code

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

Share

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

Producer-consumer model and how to achieve through the java code, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Producer-consumer model description:

1. The producer only produces when the warehouse is not full, and the producer process is blocked when the warehouse is full.

two。 Consumers only spend when the warehouse is not empty, and the consumer process is blocked when the warehouse is empty.

3. When consumers find that the warehouse is empty, they will notify the producers of production.

4. When the producer finds that the warehouse is full, he will inform the consumer of consumption.

The key to implementation:

The two synchronization methods in the shared memory and the call of the wait () method in the synchronization method ensure that the object can only be occupied by one thread. Wait ensures that when the thread releases the lock while waiting, other objects have a chance to acquire the lock.

In an object, the method declared with synchonized is a synchronous method. There is a synchronization model in Java, the monitor, which is responsible for managing thread access to synchronization methods in an object. Its principle is that the object is given a unique 'key'. When multiple threads enter the object, only the thread that acquires the object key can access the synchronization method, and other threads wait in the object until the thread abandons the key with the wait () method, and other waiting threads seize the key. The thread that preempts the key can be executed, while the thread that does not get the key is still blocked and waiting in the object. All in all, synchonized allows only one thread to enter the critical code area.

Code implementation:

Package com.thread;public class ProducerConsumer {public static void main (String [] args) {ShareData sd = new ShareData (); new Producer (sd). Start (); new Consumer (sd). Start ();} class Producer extends Thread {private ShareData sd; public Producer (ShareData sd) {this.sd = sd } @ Override public void run () {for (int I = 0; I)

< 20; i++){ int product = (int)(Math.random()*1000); sd.setArray(product); try { Thread.sleep((int)(Math.random()*200)); } catch (InterruptedException e) { e.printStackTrace(); } } }}class Consumer extends Thread{ private ShareData sd; public Consumer(ShareData sd){ this.sd = sd; } @Override public void run() { for(int i = 0; i < 20; i++){ sd.getArray(); try { Thread.sleep((int)(Math.random()*200)); } catch (InterruptedException e) { e.printStackTrace(); } } }}class ShareData{ private static int shareArray[] = new int[10]; private int count; private int in; private int out; ShareData(){ this.count = 0; this.in = 0; this.out = 0; } public synchronized void setArray(int product){ try{ while(count >

= shareArray.length) {System.out.println ("array full."); this.wait ();} this.notify ();} catch (Exception e) {e.printStackTrace () } shareArray [in] = product; count++; System.out.println ("produce:" + product); in = (in + 1)% shareArray.length;} public synchronized int getArray () {try {while (count)

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