C # synchronization of multiple producers and consumers,

Source: Internet
Author: User

C # synchronization of multiple producers and consumers,

// When multiple producers and 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; // public HoldIntegerSynchronized (int capacity) {buffer = new int [capacity];} public int BufferSize {get {return buffer. length ;}} public int Buffer {get {int bufferCopy; // Lock (this) {while (occupiedBufferCount = 0) {// multiple consumers, so use the while Console here. writeLine (Thread. currentThread. name + "tries to read. "); DisplayState (" Buffer Empty. "+ Thread. currentThread. name + "waits. "); Monitor. wait (this); // this is the release of the producer waiting outside the critical section, so that the producer can "produce" // until the end of the producer's production, the Monitor is called. pauseAll () // can continue execution. At this time, the consumer automatically obtains the lock for this again} -- occupiedBufferCount; bufferCopy = buffer [readPosition]; readPosition = (r EadPosition + 1) % buffer. length; DisplayState (Thread. currentThread. name + "reads" + bufferCopy); // notifies the waiting producer thread to enter the Started State. If the producer is out of the critical section, after this statement is executed, the Monitor is still out of the critical section. pulseAll (this); // release lock} // lock return bufferCopy;} set {// lock (this) {while (occupiedBufferCount = buffer. length) {Console. writeLine (Thread. currentThread. name + "tries to write. "); DisplayState (" Buffer Full. "+ Thread. current Thread. name + "waits. "); Monitor. wait (this); // Wait for the consumer to release outside the critical section, and let the consumer "consume" until the consumer calls Monitor. pause () // can continue execution. At this time, the producer automatically reacquires the lock for this} buffer [writePosition] = value; ++ occupiedBufferCount; writePosition = (writePosition + 1) % buffer. length; DisplayState (Thread. currentThread. name + "writes" + value); // notification, which enables consumers in the Wait status to enter the Started status. If a consumer is out of the critical section, after this statement is executed, the Monitor is still out of the critical section. pulseAll (this); // release the lock} p Ublic void DisplayState (string operation) {Console. write ("{0,-35}", operation); for (int I = 0; I <BufferSize; I ++) {int a = readPosition; int B = writePosition; if (a <= I & I <B) {Console. write ("{0,-9}", buffer [I]);} else if (B <&&! (B <= I & I <a) {Console. write ("{0,-9}", buffer [I]);} else if (occupiedBufferCount = BufferSize) {Console. write ("{0,-9}", buffer [I]);} else {Console. write ("{0,-9}", "") ;}} Console. writeLine ("{0}/r/n", occupiedBufferCount) ;}} class Producer {private HoldIntegerSynchronized sharedLocation; private Random randomSleepTime; public Producer) {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 (Random shared, random) {sharedLocation = shared; randomSleepTime = Random ;} public void Consume () {int sum = 0; for (int count = 0; count <4; count ++) {Thread. sleep (randomSleepTime. next (); sum + = sharedLocation. buffer;} Console. writeLine (Thread. currentThread. name + "read values 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 

 


& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

Related Article

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.