Java Pipeline solves producer consumer issues

Source: Internet
Author: User

The same is the experimental archive. 。。

The issue of producer consumers is still a backdrop.

Enhancement (= "Resource Manager") encapsulates the resource and the operation of the resource, and the resource user is OK to manipulate the resource through the interface without having to consider the problem of process synchronization.

Enhancement

 PackageEntity.producerconsumer; Public classmonition {Privatebuffer buffer;  PublicMonition (intbuffersize) {Buffer=NewBuffer (buffersize); }    /*** If the product is put into a successful return true *@return     */     Public synchronized Booleanput () {if(Buffer.notfull ()) {Buffer.putitem (); return true; }        return false; }    /*** If the product is removed successfully returns True *@return     */     Public synchronized Booleanget () {if(Buffer.notempty ()) {Buffer.getitem (); return true; }        return false; }     PublicString GetStatus () {return"" +buffer; }}

Producers & Consumers:

 PackageEntity.producerconsumer; Public classProducImplementsRunnable {/*** Statistics of producers*/    Private Static intTotal = 0; /*** ID of the producer individual*/    Private intID; Privatemonition PC;  Publicproduc (monition monition) {ID= ++Total ; PC=monition; } @Override Public voidrun () { while(true) {            if(Pc.put ()) {//If the operation succeeds in printing the buffer stateSystem.out.println (ID + "number producer:" +pc.getstatus ()); }        }    }}

/

 PackageEntity.producerconsumer; Public classConsuImplementsRunnable {/*** Statistics of the number of consumers*/    Private Static intTotal = 0; /*** ID of the consumer individual*/    Private intID; Privatemonition PC;  PublicConsu (monition monition) {ID= ++Total ; PC=monition; } @Override Public voidrun () { while(true) {            if(Pc.get ()) {//If the operation succeeds in printing the buffer stateSystem.out.println (ID + "Number of consumers:" +pc.getstatus ()); }        }    }}

/

Buffer.java

Test:

 Packagetest;ImportEntity.producerconsumer.Consu;Importentity.producerconsumer.Monition;ImportEntity.producerconsumer.Produc; Public classMonitiontest { Public Static voidMain (string[] args) {monition PC=NewMonition (10); //creation of 5 producers and 5 consumers         for(inti = 0; I! = 5; ++i) {NewThread (Newproduc (PC)). Start (); NewThread (NewConsu (PC)). Start (); }    }}

Java Pipeline solves producer consumer issues

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.