Producer and consumer implementation (Java)

Source: Internet
Author: User

Producers and consumers often encounter problems. Today, we are taking the time to write the implementation of this scenario. The so-called producer is an object (usually a thread) that generates data. The data produced by the producer is put into a warehouse, and the consumer can extract data directly from the warehouse. The so-called consumer is the object that extracts data from the warehouse, usually another thread. The following example shows how the producer produces bread and puts it in the warehouse for consumers to use.

1. Object Description:

Bread: Bread produced by the producer

Breadcache: The store of the bread produced by the producer, that is, a cache address.

Producer: The thread responsible for producing the breadcache and the breadcache.

Consumer: The thread responsible for consuming the breadcache and taking the breadcache from the breadcache warehouse for consumption.

2. ExampleCode

 

Package com. csdn. algorithm. Producer. Consumer;/*** sample producer and consumer. <Br> * bread: the bread that the producer is responsible for producing. <br> * breadcache: the storage of the bread produced by the producer, that is, a cache address. <br> * producer: the thread responsible for producing the breadcache and the breadcache. <br> * Consumer: the thread responsible for consuming the breadcache, consumes the breadcache from the breadcache warehouse. <br> ** @ author administrator **/public class producerconsumer {/*** main method ** @ Param ARGs */public static void main (string [] ARGs) {// call the internal class. It can also be used as a class file producerconsumer ainstance = new producerconsumer (); breadcache currbreadcache = ainstance. new breadcache (); producer P = ainstance. new producer (currbreadcache); consumer c = ainstance. new consumer (currbreadcache); // thread new thread (P ). start (); New thread (c ). start () ;}// define a bread class. Only the idclass bread {int m_nid; public bread (INT _ id) {super (); this. m_nid = _ id;} Public String tostring () {return this. m_nid + "" ;}}// defines the storage of the bread produced by the producer, that is, the cache class breadcache of the breadcache {private int m_nindex = 0; bread [] m_obreadcache = new bread [10]; Public breadcache () {super ();}/*** put a bread into the Warehouse. If the Warehouse is full, wait for ** @ Param _ newbread */Public synchronized void push (bread _ newbread) {While (m_nindex = m_obreadcache.length) {try {This. wait ();} catch (interruptedexception e) {e. printstacktrace () ;}/// put the bread m_obreadcache [m_nindex] = _ newbread; m_nindex ++; // notify all threads waiting to get the bread this. policyall ();}/*** get a loaf of bread from the warehouse. If there is no bread, wait ** @ return */Public synchronized bread POP () {While (m_nindex = 0) {try {This. wait ();} catch (interruptedexception e) {e. printstacktrace () ;}m_nindex --; this. yyall (); Return m_obreadcache [m_nindex] ;}// producer thread. One thread only produces 20 class producer implements runnable {breadcache m_ocache = NULL; public producer (breadcache _ cache) {m_ocache = _ cache;} @ overridepublic void run () {for (INT I = 0; I <20; I ++) {bread abread = new bread (I); m_ocache.push (abread); system. out. println ("production [" + abread + "]"); try {thread. currentthread (). sleep (INT) (math. random () * 200);} catch (interruptedexception e) {e. printstacktrace () ;}}}// consumption thread. One thread consumes 20 class Consumer implements runnable {breadcache m_ocache = NULL; public consumer (breadcache _ cache) {m_ocache = _ cache;} @ overridepublic void run () {for (INT I = 0; I <20; I ++) {bread abread = m_ocache.pop (); system. out. println ("consumption [" + abread + "]"); try {thread. currentthread (). sleep (INT) (math. random () * 200);} catch (interruptedexception e) {e. printstacktrace ();}}}}}

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.