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