Implementation of Java producer consumer model

Source: Internet
Author: User

Spring Festival back to the hometown, and experienced a water table, due to more tables, resulting in slow serving, so in the waiting room, summed up a few characteristics of the process of the vegetables:

1. There are many cooking stoves, many cooking stoves are at the same time cooking out.

2. Cooked dishes, there will be a person with a tray end out, each end of the dish (is the same dish) the number of different.

3. Because the end dish may not meet all the table number, so, the end dish people may choose a few tables (usually the nearest principle, or the main table in the past) to serve, the rest of the number of tables continue to wait for the end of the side of the food people out.

The above 3 conditions, is completely a producer consumer model of the scene, so, the producer consumer model first to achieve, and then analyze how to quickly serve the dishes:)

First, we put the tray to virtual into a resource pool, indicating that the tray is to put vegetables, when the tray dish is more than 1 o'clock, that is, a dish is produced, the end of the serve something out of the dish, when there is no food in the tray, all the table outside to wait:

(It is important to note that this resource pool can have only one instanced object, just as the number of pallets is fixed.) )

public class Resourcepool {private int number = 0;public synchronized void producer () {try {while (number==3) {this.wait ();} number++; System.out.println ("producer:" +number); This.notifyall ();} catch (Interruptedexception e) {e.printstacktrace ();}} Public synchronized void consumer () {try {while (number==0) {this.wait ();} number--; System.out.println ("Consumer:" +number); This.notifyall ();} catch (Interruptedexception e) {e.printstacktrace ();}}}

In fact, we have to have a stove, this stove is specialized in cooking, make out the dishes, of course, are all placed in the resource pool (that is, the tray), the stove will have a number of, so to inherit the thread class:

public class Resourceproduce extends Thread{private resourcepool rp;public resourceproduce (Resourcepool rp) {THIS.RP = RP ;} public void Run () {rp.producer ();}}

Tray has a dish, you have to end out, to the outside table, because the table is multi-table, so, also inherit the thread class:

public class Resourceconsumer extends Thread{private resourcepool rp;public Resourceconsumer (Resourcepool rp) {THIS.RP = RP;} public void Run () {Rp.consumer ();}}

After these basic facilities are ready, our end-dish man comes out:

public class Resourceutil {public void resource () {Resourcepool RP = new Resourcepool (), for (int i = 0; i < 3; i++) {New Resourceproduce (RP). Start ();} for (int i = 0; i < 5; i++) {new Resourceconsumer (RP). Start ();}} public static void Main (string[] args) {resourceutil ru = new Resourceutil (); Ru.resource ();}}

Let's take a look at the final output:

When there are only three cooking hobs, and the table has 5 tables, the program waits, so when we change the stove number to 5, the result of the operation:


Through the above program to run, if you want to serve fast, or have to add a stove, more cooks, of course, this is just a simple analysis of this scenario, there may be more complex factors not considered, the main meaning of this example is to let a lot of understanding of the producer consumer model, This mode we can usually use less native, but actually use the scene has been used, such as thread pool, connection pool, and so on. Therefore, it is also necessary to know the reason why, and we then explain the code to illustrate the focus of this implementation code:

1. There is only one resource pool.

2.synchronized, is the lock object, simply say: An object has and only a lock, when there are more than one synchronized method or block of code to request a lock on the object, at the same time, only one thread will get the lock and run, the others are blocked.

3.wait, refers to the thread waits, wait has a very important point, is to release the lock, it also said that synchronized at the same time only one thread will get the lock and run, so once wait, will release the lock, but when the front thread waits, the other threads compete for this lock.

4.notifyAll refers to all the waiting threads that wake up the current object.

5. All awakened threads compete for the lock at the same time, but the JVM randomly chooses a thread and assigns the lock to that thread.

6. The above synchronized wait Notifyall all operate on an object, but these three are used in the resource pool class, so this is also the resource pool has and only one reason.

Thread: As for the producer consumer model can give us a test of what kind of help, I haven't thought for a while, but to understand, go out to interview, there is a great possibility to be asked, interested, as a kind of knowledge reserves.

Implementation of Java producer consumer model

Related Article

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: 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.