Multi-thread synchronization mutex model between producers and consumers implemented in Java

Source: Internet
Author: User

In the producer and consumer models, you must ensure the following:
(1) only one producer can produce data at a time.
(2) Only one consumer can consume at a time.
(3) The producer cannot consume the product at the same time.
(4) When the message queue is full, the producer cannot continue production. When the message queue is empty, the consumer cannot continue consumption. At this time, the producer must wait.

Code:

1. Message. Java

Package COM. test; </P> <p>/** <br/> * message object <br/> */<br/> public class message {</P> <p> private int ID; <br/> private string message; <br/> Public int GETID () {<br/> return ID; <br/>}< br/> Public void setid (int id) {<br/> This. id = ID; <br/>}< br/> Public String getmessage () {<br/> return message; <br/>}< br/> Public void setmessage (string message) {<br/> This. message = message; <br/>}</P> <p >}< br/>

2. queue. Java

 Package COM. test; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. list; <br/>/** <br/> * cache queue for storing messages, shared resources <br/> */<br/> public class queue {</P> <p> private list <message> List = new arraylist <message> (); </P> <p> private int maxcount = 5; </P> <p>/** <br/> * production <br/> * @ Param message <br/> */<br/> Public synchronized void product (Message message) {</P> <p> This. policyall (); <br/> while (maxcount = Getlist (). size () {<br/> system. out. println (thread. currentthread (). getname () + ", the queue is full, waiting .... "); <Br/> try {<br/> This. wait (); <br/>}catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> getlist (). add (Message); <br/> system. out. println (thread. currentthread (). getname () + ", in production... Current number of Queues: "+ getlist (). size ()); <br/>}</P> <p>/** <br/> * consumption <br/> */<br/> Public synchronized void consume () {<br/> This. policyall (); <br/> while (getlist (). size () = 0) {<br/> system. out. println (thread. currentthread (). getname () + ", the queue is empty, waiting... "); <Br/> try {<br/> This. wait (); <br/>}catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> message = getlist (). get (0); <br/> getlist (). remove (Message); <br/> system. out. println (thread. currentthread (). getname () + ", in consumption... Current number of Queues: "+ getlist (). size (); </P> <p >}</P> <p> public list <message> getlist () {<br/> return list; <br/>}</P> <p> Public void setlist (list <message> List) {<br/> This. list = List; <br/>}</P> <p> Public int getmaxcount () {<br/> return maxcount; <br/>}</P> <p> Public void setmaxcount (INT maxcount) {<br/> This. maxcount = maxcount; <br/>}< br/>

3. Producer. Java

Package COM. test; </P> <p> Import Java. util. random; </P> <p>/** <br/> * producer production <br/> */<br/> public class producer implements runnable {</ p> <p> private queue = NULL; </P> <p> Public producer (queue Queue) {<br/> This. queue = queue; <br/>}</P> <p> Public void run () {<br/> while (true) {<br/> message = new message (); <br/> message. setid (new random (). nextint (); <br/> message. setmessage ("message," + new random (). nextint (); <br/> queue. product (Message); <br/> try {<br/> thread. sleep (100); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}</P> <p >}< br/>

4. Consumer. Java

Package COM. test; </P> <p>/** <br/> * consumer consumption <br/> */<br/> public class Consumer implements runnable {</ p> <p> private queue; </P> <p> public consumer (queue Queue) {<br/> This. queue = queue; <br/>}</P> <p> Public void run () {</P> <p> while (true) {<br/> queue. consume (); <br/> try {<br/> thread. sleep (100); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}</P> <p >}< br/>

5. Test. Java

Package COM. test; </P> <p> public class test {</P> <p> Public static void main (string [] ARGs) {<br/> queue = new Queue (); <br/> // two producers <br/> producer producer1 = new producer (Queue ); <br/> producer producer2 = new producer (Queue); </P> <p> // three consumers <br/> consumer consumer1 = new consumer (Queue ); <br/> consumer consumer2 = new consumer (Queue); <br/> consumer consumer3 = new consumer (Queue ); </P> <p> thread thread1 = new thread (producer1, "producer1"); <br/> thread thread2 = new thread (producer2, "producer2 "); <br/> thread thread3 = new thread (consumer1, "consumer1"); <br/> thread thread4 = new thread (consumer2, "consumer2 "); <br/> thread thread5 = new thread (consumer3, "consumer3"); <br/> thread1.start (); <br/> thread2.start (); <br/> thread3.start (); <br/> thread4.start (); <br/> thread5.start (); </P> <p >}< br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.