Java_ Multithreading _ producer and Consumer (concurrent collaboration)

Source: Internet
Author: User

For multi-threaded programs, both the producer and consumer models are the most classic, regardless of the programming language. Just like learning every programming language, Hello world! Are the most classic examples.

In fact, it should be "producer-consumer-warehousing" model, leaving the warehouse, the producer consumer model is not convincing.
For this model, you should be clear about the points:
1, the producer only in the storage is not full time production, warehouse full stop production.
2, consumers only in storage products can be consumed, Cang is waiting.
3, when the consumer found that the storage of products can be consumed when the producers will be informed of production.
4, producers in the production of consumable products, should inform the waiting consumers to spend.

Package Cn.thread;import Java.util.concurrent.blockingqueue;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.linkedblockingqueue;/** * Java multithreaded simulation producer consumer issues * * Producerconsumer is the main class, producer producer, consumer consumer, product products, storage warehouse * * @author Lin Yi-chin * @version 1.0 2013-7-24 pm 04:49:02 */pu Blic class Producerconsumer {public static void main (string[] args) {Producerconsumer pc = new Producerconsume        R ();        Storage s = pc.new Storage ();        Executorservice service = Executors.newcachedthreadpool ();        Producer p = pc.new Producer ("Zhang San", s);        Producer P2 = pc.new Producer ("John Doe", s);        Consumer C = pc.new Consumer ("Harry", s);        Consumer C2 = pc.new Consumer ("Lao Liu", s);        Consumer C3 = Pc.new Consumer ("Lim", s);        Service.submit (P);        Service.submit (p2);        Service.submit (c);        Service.submit (C2);            Service.submit (C3); }/** * Consumer * * @author Lin Yi-chin * @Version 1.0 2013-7-24 PM 04:53:30 */class Consumer implements Runnable {private String name;        Private Storage s = null;            Public Consumer (String name, Storage s) {this.name = name;        THIS.S = s; public void Run () {try {while (true) {System.out.println (name + "                    Prepare consumer products. ");                    Product Product = S.pop ();                    SYSTEM.OUT.PRINTLN (name + "Consumed" ("+ product.tostring () +"). ");                    System.out.println ("===============");                Thread.Sleep (500);            }} catch (Interruptedexception e) {e.printstacktrace ();  }}}/** * producer * * @author Lin Yi-chin * @version 1.0 2013-7-24 PM 04:53:44 */class Producer        Implements Runnable {private String name;        Private Storage s = null; Public Producer (String name, Storage s) {this.name = name;           THIS.S = s; public void Run () {try {while (true) {Product Product = new Produ CT ((int) (Math.random () * 10000));                    Generate 0~9999 random integer System.out.println (name + "Prepare production (" + product.tostring () + ").");                    S.push (product);                    SYSTEM.OUT.PRINTLN (name + "produced" ("+ product.tostring () +"). ");                    System.out.println ("===============");                Thread.Sleep (500);            }} catch (Interruptedexception E1) {e1.printstacktrace (); }}}/** * warehouse for storing products * * @author Lin Yi-chin * @version 1.0 2013-7-24 PM 04:54:16 */public C        Lass Storage {blockingqueue<product> queues = new linkedblockingqueue<product> (10);        /** * Production * * @param p * Products * @throws interruptedexception */ public void push (Product p) Throws Interruptedexception {queues.put (P); }/** * Consumption * * @return Products * @throws interruptedexception */Public        Product pop () throws interruptedexception {return queues.take ();        }}/** * Products * * @author Lin Yi-chin * @version 1.0 2013-7-24 PM 04:54:04 */public class Product {        private int id;        public Product (int id) {this.id = ID;        } public String ToString () {//Override toString Method return "Product:" + this.id; }    }}

Java_ Multithreading _ producer and Consumer (concurrent collaboration)

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.