Java _ multithreading _ producer and consumer (concurrent collaboration), java producer

Source: Internet
Author: User

Java _ multithreading _ producer and consumer (concurrent collaboration), java producer

For multi-threaded programs, regardless of any programming language, producer and consumer models are the most classic. 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 produces when the warehouse is not full, and stops producing when 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.

Package cn. thread; import java. util. concurrent. blockingQueue; import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. linkedBlockingQueue;/*** java Multithread simulation of Producer Consumer problems ** ProducerConsumer is the main class, Producer, Consumer, Product, storage warehouse ** @ author Lin ji qin * @ version 1.0 04:49:02 */public class ProducerConsumer {public static void main (String [] Args) {ProducerConsumer pc = new ProducerConsumer (); Storage s = pc. new Storage (); ExecutorService service = Executors. newCachedThreadPool (); Producer p = pc. new Producer ("James", s); Producer p2 = pc. new Producer ("", s); Consumer c = pc. new Consumer ("Wang Wu", s); Consumer c2 = pc. new Consumer ("Liu", s); Consumer c3 = pc. new Consumer ("laolin", s); service. submit (p); // service. submit (p2); service. submit (c ); Service. submit (c2); service. submit (c3);}/*** Consumer ** @ author Lin ji qin * @ version 1.0 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 to consume the product. "); Product product = s. pop (); System. out. println (name + "already Consumption ("+ product. toString () + "). "); System. out. println ("===================="); Thread. sleep (500) ;}} catch (InterruptedException e) {e. printStackTrace () ;}}/ *** Producer ** @ author Lin ji qin * @ version 1.0 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 Product (int) (Math. random () * 10000); // generate 0 ~ 9999 random integer System. out. println (name + "prepare for production (" + product. toString () + "). "); s. push (product); System. out. println (name + "produced (" + product. toString () + "). "); System. out. println ("===================="); Thread. sleep (500) ;}} catch (InterruptedException e1) {e1.printStackTrace () ;}}/ *** repository, used to store the Product ** @ author Lin ji qin * @ version 1.0 04:54:16 */public class Storage {BlockingQueue <Product> queues = new LinkedBlockingQueue <Product> (10 ); /*** production ** @ param p * Product * @ throws InterruptedException */public void push (Product p) throws InterruptedException {queues. put (p);}/*** consumption ** @ return Product * @ throws InterruptedException */public Product pop () throws InterruptedException {return queues. take () ;}}/*** Product ** @ author Lin ji qin * @ version 1.0 04:54:04 */public class Product {private int id; public Product (int id) {this. id = id;} public String toString () {// rewrite toString method return "product:" + this. id ;}}}
 

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.