// Multiple producers and multiple consumers can produce N products
Using system; using system.threading;
Public class holdintegersynchronized {
Private int [] buffer; // buffer
private int occupiedBufferCount = 0; private int readPosition = 0, writePosition = 0; // next write position and a read position public HoldIntegerSynchronized (int capacity) {buffer = new int [capacity];} public int BufferSize {get {Return buffer.Length;}}
Public int buffer {get {int buffercopy; // Plock Lock (this) {while (occupiedbuffercount == 0) {// Multiple consumers, so this is done with while console.writeline (thread.currentthread.name " Tries to read. "); DisplayState (" Buffer Empty. " thread.currentthread.name " Waits. "); Monitor.wait (this); // For the producers waiting for the critical area, let him come "Production" // has been completed until producers production, calling Monitor.Pauseall () // to continue, at this time, consumers automatically re-get the lock}
--Occupiedbuffercount;
Buffercopy = buffer [readPosition];
Readposition = (ReadPosition 1)% Buffer.Length; DISPLAYSTATE (Thread.currentthRead.Name "Reads" BufferCopy;
// Notice, let the waiting producer thread enters the Started state, if the producer is in the critical area, he is still outside the critical region outside of the critical area. (释}} // Lock Return buffercopy;
set {// lock lock (this) {while (occupiedBufferCount == buffer.Length) {Console.WriteLine (Thread.CurrentThread.Name "tries to write."); DisplayState ( Thread.CurrentThread "Buffer Full." .Name "Waits."); Monitor.wait (this); // Waiting for consumers outside the critical area to release, let him "consumption" // until the consumer calls Monitor.pause () // Continue to execute, at this time, the producer automatically re-get the lock}
Buffer [WritePosition] = Value; OccupiedBufferCount; WritePosition = (WritePosition 1)% buffer.Length
DisplayState (thread.currentthread.name "Writes" value);
// Notice, let the consumer of the WAIT state enters the Started state. If the consumer is in the critical area, he is still outside the critical area, he is still outside the critical area.
// Release lock}}}}}
Public void displayState (String Operation) {Console.Write ("{0, -35}", Operation); for (int i = 0; i CONSOLE.WRITELINE ("{0} / r / n", occupiedbuffercount);}} Class producer {producture; privateLocation; private random randomsleptime public Producer (HoldIntegerSynchronized shared, Random random) {sharedLocation = shared; randomSleepTime = random;} public void Produce () {for (int count = 0; count <3; count ) {Thread.Sleep (randomSleepTime.Next (1,2000 ))); Sharedlocation.buffer = randomsleeptime.next (5, 10);} console.writeline (thread.currentthread.name "Done Producing./r/nterminating" thread.currentthread.name "./R/N" }} Class Consumer {Private HoldIntegersynchronized SharedLocation; Private Random RandomsleepTime public Consumer (HoldIntegerSynchronized shared, Random random) {sharedLocation = shared; randomSleepTime = random;} public void Consume () {int sum = 0; for (int count = 0; count <4; count ) {Thread.Sleep (randomSleepTime. Next (1,2000)); SUM = sharedlocation.buffer;} console.writeline (thread.currentthread.name "}}}}}}}}}}}}} Read value content Totaling:" Sum "/ R / NTERMINATING" Thread.currentthRead.Name ". ");}} class SharedCell {static void Main (string [] args) {HoldIntegerSynchronized holdInteger = new HoldIntegerSynchronized (5); Random random = new Random (); Thread [] producerThreads = new Thread [4]; Thread [] consumerThreads = new Thread [3 ]; Console.write ("{0, -35}", "Operation"); for (int i = 0; i for (int i = 0; i for (int i = 0; i For (int i = 0; i