Java thread (14): Concurrent collaboration-producer consumer model

Source: Internet
Author: User

Java thread: Concurrent collaboration-producer consumer model is the most classic model for multi-threaded programs, regardless of any programming language. Just like learning every programming language, Hello World! Are the most classic examples. In fact, it should be accurate to say that it is the "producer-consumer-warehouse" model. After leaving the warehouse, the producer-consumer model is unconvincing. For this model, the following points should be clarified: 1. The producer only stops production when the Warehouse is full and the warehouse is full. 2. Consumers can only consume data when there are products in the warehouse, while empty warehouses are waiting. 3. When a consumer finds that there is no product in the warehouse to be consumed, the producer will be notified of production. 4. When the producer generates a consumable product, it should notify the waiting consumer to consume it. This model will be combined with the wait, y, and yyall methods of java. lang. Object to achieve the above requirements. This is very important. /*** Java thread: Concurrent collaboration-producer consumer model ** @ author leizhimin 14:54:36 */public class Test {public static void main (String [] args) {Godown godown = new Godown (30); Consumer c1 = new Consumer (50, godown); Consumer c2 = new Consumer (20, godown); Consumer c3 = new Consumer (30, godown); Producer p1 = new Producer (10, godown); Producer p2 = new Producer (10, godown); Producer p3 = new Producer (10, godow N); Producer p4 = new Producer (10, godown); Producer p5 = new Producer (10, godown); Producer p6 = new Producer (10, godown ); producer p7 = new Producer (80, godown); c1.start (); c2.start (); c3.start (); p1.start (); p2.start (); p3.start (); p4.start (); p5.start (); p6.start (); p7.start () ;}/ *** repository */class Godown {public static final int max_size = 100; // maximum inventory public int curnum; // current inventory Godown (){} Godown (int curnum) {this. curnum = curnum;}/*** produce a specified number of products ** @ param neednum */public synchronized void produce (int neednum) {// test whether the while (neednum + curnum> max_size) {System. out. println ("number of products to be produced" + neednum + "excess inventory" + (max_size-curnum) + ", cannot execute production tasks for the moment! "); Try {// The current production thread waits for wait ();} catch (InterruptedException e) {e. printStackTrace () ;}/// if the production conditions are met, the production is performed. Here, the current inventory curnum + = neednum; System is simply changed. out. println ("has produced" + neednum + "products, the current storage volume is" + curnum); // wake up all threads waiting on this object monitor notifyAll ();} /*** consume a specified number of products ** @ param neednum */public synchronized void consume (int neednum) {// test whether the while (curnum <neednum) can be consumed) {try {// The current production thread waits for wait ();} catch (Interru PtedException e) {e. printStackTrace () ;}/// if the consumption condition is met, the consumption is performed. Here, the current inventory curnum-= neednum; System is simply changed. out. println ("consumed" + neednum + "products, the current storage volume is" + curnum); // wake up all threads waiting on this object monitor yyall ();}} /*** Producer */class Producer extends Thread {private int neednum; // number of production products private Godown godown; // repository Producer (int neednum, Godown godown) {this. neednum = neednum; this. godown = godown;} public void run () {// Produce a specified number of products godown. produce (neednum) ;}}/*** Consumer */class Consumer extends Thread {private int neednum; // number of production products private Godown godown; // warehouse Consumer (int neednum, Godown godown) {this. neednum = neednum; this. godown = godown;} public void run () {// consume a specified number of products godown. consume (neednum) ;}} has produced 10 products. The current storage volume is 40 and 10 products have been produced. The current storage volume is 50 and 50 products have been consumed, the current storage volume is 0 and 80 products have been produced. The current storage volume is 80 and 30 products have been consumed. The current storage volume is 50 and 10 products have been produced. Product: The current storage volume is 60 and 20 products have been consumed. The current storage volume is 40 and 10 products have been produced. The current storage volume is 50 and 10 products have been produced, the current storage volume is 60 and 10 products have been produced. The current storage volume is 70 Process finished with exit code 0. Note: For this example, it should be noted that when the production or consumption conditions cannot be met, the wait method of the object is called. The wait method is used to release the lock obtained by the current thread, and call the notifyAll () method of the object to notify (wake up) Other waiting threads on the object so that it can continue execution. In this way, the entire producer and consumer thread can be correctly executed collaboratively. The yyall () method serves as a notification, neither releasing or obtaining a lock. I just told the waiting thread on this object that "the thread can compete for execution, and I will wake up to execute it ". This example is only the simplest expression in the producer-consumer model. In this example, if the storage volume consumed by a consumer does not meet the requirement and no producer exists, the program remains in the waiting state, of course, this is not true. In fact, this example can be modified to produce based on consumption, while both the production and the warehouse are taken into account. If the warehouse is not satisfied, the production will be produced, and the maximum consumption will be limited each time, in this way, this problem does not exist. Of course, such an example is more complex and it is more difficult to describe such a simple model. I like simple examples.

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.