Java-runnable Lock to realize multi-threading problem of producer and consumer

Source: Internet
Author: User

Case:

There is a commodity sales organization, only one producer, two consumers, please use a multi-threaded approach to this case implementation.

//inventory function, which holds the information of the stock Storage.java Public classStorage {//Simulated Inventory     PublicInteger Num=1;}//producer function Product.java/**     Copyright  (C),  1988-1999 , huawei tech. co., ltd.   filename: customer.java author:      Light   Version :    version1.0       Date:    2018/7/11description:     producers for production, Producers need to be in consumer production after consumption, simulation of only one piece of memory, when no consumption, will remind consumers to spend, wake up the consumer process//  Module Description        & nbsp; version:       1.0//  version Information    function list:    //  main functions and their functions      1. History:   //  History Change Records         <author>  <time>   < version >   <desc>        Light     2018/7/11     1.0     build this moudle    ***********************************************************/ Public classProductImplementsrunnable{PrivateStorage Storage;  PublicProduct (Storage Storage) {Super();  This. Storage =storage; } @Override Public voidrun () {//TODO auto-generated Method Stub         while(true){            synchronized(storage) {if( This. storage.num<1){                    //Analog Production                     This. storage.num++; System.out.println ("Production of One"); //wake all processes in queue                     This. Storage.notifyall (); }Else{                    Try {                        //Enter the waiting queue                         This. storage.wait (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                }            }        }    }}//Consumer function Customer.java/**   Copyright  (C),  1988-1999 , huawei tech. co., ltd.   filename: customer.java author:      Light   Version :    version1.0       Date:    2018/7/11description:     consumers to consume, Consumers need to produce in the producer and then consume, when there is no production, will remind producers to produce, wake up the producer Process//  module Description &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB Sp version:       1.0//  Version Info    Function List:    //  main functions and their functions      1. History:   //  historical modification Records          <author>  <time>   <version  >   <desc>        light    2018/7/11     1.0     build this moudle    ***********************************************************/ Public classCustomerImplementsRunnable {PrivateStorage Storage;  PublicCustomer (Storage Storage) {Super();  This. Storage =storage; }    /*** Rewrite run () *@author[Light] (required) *@see[Run ()] (optional) *@since[version 1.0] (required)*/@Override Public voidrun () {//TODO auto-generated Method Stub         while(true){            //Locking            synchronized(storage) {//determine if the producer has made the production                if( This. storage.num>=1){                    //simulated consumption                     This. storage.num--; System.out.println ("Consumption of One"); //Wake up queue All processes                     This. Storage.notifyall (); }Else{                    Try {                        //wait                         This. storage.wait (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                }            }        }    }}//main function Mytest.java/**   Copyright  (C),  1988-1999 , huawei tech. co., ltd.   filename: mytest.java author:      Light   Version :    version1.0       Date:    2018/7/11description:    Consumption production process simulation//  module description          Version:       1.0//  Version Info    Function List:   //  main function and its function      1.  History:   //  history Modification Records         <author>   <time>   <version >   <desc>         light    2018/7/11     1.0      build this moudle   ********************************************************** */ Public classMyTest { Public Static voidMain (string[] args) {
Stock Storage Storage=NewStorage ();
Producer Product P1=NewProduct (storage);
Two consumers customer C1=NewCustomer (storage); Customer C2=NewCustomer (storage); Thread T1=NewThread (p1); Thread T2=NewThread (C1); Thread T3=NewThread (C2); T1.start (); T2.start ();  T3.start (); }}

Final execution Result:

By locking and awakening the process, the producer and consumer cases are finally realized.

Java-runnable Lock to realize multi-threading problem of producer and consumer

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.