School Experiment Archive//.
Take the classical producer consumer question as the carrier.
Process Synchronization Mode Interface:
Package method; /** * */ Public Interface Method { void P (); void V ();}
The analog Swap instruction implements the interface:
Packagemethod; Public classSwapImplementsMethod {Private BooleanLock =false; /*** Create a special instance variable (it has to be an object) to act as a lock*/ Private byte[] Objectlock =New byte[0]; Public voidp () {BooleanKey =true; Booleantemp; Do { synchronized(objectlock) {temp=key; Key=lock; Lock=temp; } } while(Key! =false); } Public voidV () {synchronized(objectlock) { This. Lock =false; } }}
Producers & Consumers:
PackageEntity.producerconsumer;Importmethod. Method;/*** Producer entity class*/ Public classProducerImplementsRunnable {/*** Statistics of producers*/ Private Static intTotal = 0; /*** ID of the producer individual*/ Private intID; /*** Analog Buffers*/ Privatebuffer buffer; /*** Allows dynamic change of synchronization mechanism*/ Privatemethod method; /*** Incoming buffer address, synchronization mechanism *@paramBuffer *@paramMethod*/ PublicProducer (buffer buffer, method) { This. ID = + +Total ; This. Buffer =buffer; This. method =method; } /*** Print producer information *@return */@Override PublicString toString () {returnID + "number producer"; } @Override Public voidrun () { while(true) {METHOD.P (); //critical Section Code if(Buffer.notfull ()) {//Production ProductsBuffer.putitem (); System.out.println ( This+ ": " +buffer); } method.v (); } }}
/
PackageEntity.producerconsumer;Importmethod. Method;/*** Consumer entity class*/ Public classConsumerImplementsRunnable {/*** Statistics of the number of consumers*/ Private Static intTotal = 0; /*** ID of the consumer individual*/ Private intID; /*** Analog Buffers*/ Privatebuffer buffer; /*** Allows dynamic change of synchronization mechanism*/ Privatemethod method; /*** Incoming buffer address, synchronization mechanism *@paramBuffer *@paramMethod*/ PublicConsumer (buffer buffer, method) { This. ID = + +Total ; This. Buffer =buffer; This. method =method; } /*** Print consumer information *@return */@Override PublicString toString () {returnID + "Number of consumers"; } @Override Public voidrun () { while(true) {METHOD.P (); //critical Section Code if(Buffer.notempty ()) {//Consumer ProductsBuffer.getitem (); System.out.println ( This+ ": " +buffer); } method.v (); } }}
/
PackageEntity.producerconsumer;/*** Buffer entity, used to simulate buffers*/ Public classBuffer {/*** Current Product quantity*/ Private intCount = 0; /*** Maximum allowable quantity*/ Private intMax; PublicBuffer (intmax) { This. Max =Max; } /*** Determine if the buffer is full *@return */ Public BooleanNotfull () {return(Count <max); } /*** Determine if the buffer is empty *@return */ Public BooleanNotempty () {return(Count > 0); } /*** Production Products*/ Public voidPutItem () {count++; } /*** Consumer Products*/ Public voidGetItem () {count--; } /*** Print buffer information *@return */@Override PublicString toString () {return"In buffer" + count + "product"; }}
The main function to test the SWAP instruction:
Packagetest;ImportEntity.producerconsumer.Buffer;ImportEntity.producerconsumer.Consumer;ImportEntity.producerconsumer.Producer;Importmethod. Method;Importmethod. Swap; Public classMain { Public Static voidMain (string[] args) {//buffer size is tenBuffer buffer =NewBuffer (10); //allow dynamic change of synchronization mechanismMethod synchronizationmechanism =NewSwap (); //creation of 5 producers and 5 consumers for(inti = 0; I! = 5; ++i) {NewThread (NewProducer (buffer, synchronizationmechanism)). Start (); NewThread (NewConsumer (buffer, synchronizationmechanism)). Start (); } }}
Re-implement the interface with a recorded semaphore (this code comes from the courseware):
Packagemethod;/*This class is used for analog semaphores and their P,V operations using the following methods: Method. Semaphore Mutex = new method. Semaphore (1); The initial value of the semaphore is assigned to 1 MUTEX.P (); Critical section Code MUTEX.V ();*/ Public classSemaphoreImplementsMethod {Private intSemvalue; PublicSemaphore (intsemvalue) { This. Semvalue =Semvalue; } Public synchronized voidp () {Semvalue--; if(Semvalue < 0) { Try { This. Wait (); } Catch(interruptedexception e) {e.printstacktrace (); } } } Public synchronized voidV () {Semvalue++; if(Semvalue <= 0) { This. Notify (); } }}
Java hardware synchronization mechanism SWAP instruction simulation + record-type signal volume simulation