Producer and consumer implementation (Java) __java

Source: Internet
Author: User

Producers and consumers are the problems that we will often encounter, and today we have time to write the implementation of this scenario. The producer is the one object (usually a thread) that produces some kind of data, and the producer's data is put into a storehouse, the consumer extracts the data directly from the storehouse. The so-called consumer is the object of extracting data from the warehouse, usually another thread. Below is an example of a producer producing bread and putting it in a warehouse for use by consumers.

1. Description of the object:

Bread: producer responsible for the production of bread

Breadcache: A warehouse in which a producer's bread is stored, which is a cache address.

Producer: The thread responsible for producing bread, the bread produced is put into the Breadcache warehouse

Consumer: A thread responsible for consuming bread, taking bread from a breadcache warehouse for consumption.

2. Sample Code

Package Com.csdn.algorithm.producer.consumer; /** * Examples of producers and consumers. <BR> * Bread: producer responsible for the production of bread <BR> * Breadcache: The producer of bread stored in the warehouse, that is, a cache address <br> *producer: Responsible for the production of bread thread, The bread produced is put into the Breadcache warehouse <BR> *consumer: The thread responsible for consuming bread, taking bread from Breadcache warehouse for consumption <BR> * @author Administrator * /public class Producerconsumer {/** * Main method * * @param args/public static void main (string[] args) {//Call
		The inner class can also be separate 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);
		Threads new Thread (p). Start ();
		
	New Thread (c). Start ();

		}//define a bread class, bread only ID class Bread {int m_nid;
			Public Bread (int _id) {super ();
		This.m_nid = _id;
		Public String toString () {return this.m_nid + "";
		}//define the bread that the producer produces, that is, the cache class Breadcache {private int m_nindex = 0; bread[] M_obreadcache = New BREAD[10];
		Public Breadcache () {super ();
			/** * Put a loaf of bread into the warehouse and if the warehouse is full, wait * * @param _newbread/public synchronized void push (Bread _newbread) {
				while (M_nindex = = m_obreadcache.length) {try {this.wait ();
				catch (Interruptedexception e) {e.printstacktrace ();
			}//Put bread m_obreadcache[m_nindex] = _newbread;

			m_nindex++;
		Notify all threads waiting to fetch bread this.notifyall (); 
				/** * from the warehouse to take a loaf of bread, if there is no bread, then wait * * @return/Public synchronized Bread POPs () {while (M_nindex = = 0) {
				try {this.wait ();
				catch (Interruptedexception e) {e.printstacktrace ();
			}} m_nindex--;
			This.notifyall ();
		return M_obreadcache[m_nindex];

		}//producer thread, one thread produces only 20 bread class Producer implements Runnable {Breadcache m_ocache = null;
		Public Producer (Breadcache _cache) {m_ocache = _cache; @Override public void Run () {(int i = 0; i < 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 ();

		}}//consumer thread, one thread consumes 20 loaves of bread class Consumer implements Runnable {Breadcache m_ocache = null;
		Public Consumer (Breadcache _cache) {m_ocache = _cache;
				@Override public void Run () {for (int i = 0; i < 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.