C # Multi-Threading Usage 5-collaboration between Threads monitor

Source: Internet
Author: User

Previously we used the lock shortcut to enable multi-threaded sharing of the same resource. In C #, lock is actually a simplified version of the monitor operation.

Use monitor below to complete the previous lock function, you can do a comparison here:

private static void Multithreadsynergicwithmonitor () {int[] array = new INT[3];                Thread producer = new Thread (() = {int count = 0;                Random random = new random ();                    while (true) {if (= = count) break;                    Monitor.Enter (array); Array[0] = random.                    Next (10); ARRAY[1] = random.                    Next (10); ARRAY[2] = random.                    Next (10);                    count++; Console.WriteLine (String.Format ("{0} work Count:{1}.                    {2}-{3}-{4} ", Thread.CurrentThread.Name, Count, Array[0], array[1], array[2]));                Monitor.Exit (array);            }}) {Name = "producer"};                Thread customer = new Thread (() = = {int count = 0; while (true) {if (10 = = count) break;                    Monitor.Enter (array);                    count++; Console.WriteLine (String.Format ("{0} work Count:{1}.                    {2}-{3}-{4} ", Thread.CurrentThread.Name, Count, Array[0], array[1], array[2]));                    Array[0] = 0;                    ARRAY[1] = 0; ARRAY[2] = 0;                Monitor.Exit (array);            }}) {Name = "customer"}; Producer.            Start (); Customer.        Start (); }

By contrast, you can surely find that lock (xx) {} is equivalent to Monitor.Enter (x ' x) and Monitor.Exit (XX), in fact lock is the syntax sugar of Monitor.

Therefore, monitor is more powerful than lock in controlling thread collaboration, as follows:

<summary>///multi-threaded collaboration-monitor////successfully solve multi-threaded sharing of a single resource///and resolve multiple thread synchronization issues//</summary&        Gt            private static void Multithreadsynergicwithmonitor () {int[] array = new INT[3];                Thread producer = new Thread (() = {int count = 0;                Random random = new random ();                    while (true) {if (= = count) break;                    Monitor.Enter (array); Array[0] = random.                    Next (10); ARRAY[1] = random.                    Next (10); ARRAY[2] = random.                    Next (10);                    count++; Console.WriteLine (String.Format ("{0} work Count:{1}.                    {2}-{3}-{4} ", Thread.CurrentThread.Name, Count, Array[0], array[1], array[2]));                    Monitor.pulse (array);                Monitor.Wait (array);            } monitor.exit (array);            }){Name = "producer"};                Thread customer = new Thread (() = = {int count = 0;                    while (true) {if (= = count) break;                    Monitor.Enter (array);                    count++; Console.WriteLine (String.Format ("{0} work Count:{1}.                    {2}-{3}-{4} ", Thread.CurrentThread.Name, Count, Array[0], array[1], array[2]));                    Array[0] = 0;                    ARRAY[1] = 0;                    ARRAY[2] = 0;                    Monitor.pulse (array);                Monitor.Wait (array);            } monitor.exit (array);            }) {Name = "customer"}; Producer.            Start (); Customer.        Start (); }

The above code is similar to the previous lock code but is not the same, it implements the producer thread and the customer thread alternately run (compared to the lock mode control more fine), it is recommended that you execute the actual code, you will easily find the two do not.

Description

1, Monitor.pulse (XX) to realize that a thread waiting for XX resource is changed to ready state (ready queue) by waiting state (waiting queue), so as to be ready to call Monitor.pulse (x ' x) The freed resource is locked as soon as the feature's thread frees resources.

2. Monitor.Wait (XX) implements the thread that called the method to temporarily release the locked resource and let the thread go into the waiting thread queue. So the thread temporarily interrupts the execution of subsequent code after calling the method, and when the thread gets the resource again,

will return to the interrupt to continue execution.

3, Monitor.pulseall (XX) is Monitor.pulse (XX) Extended version, if you understand Monitor.pulse (XX) and know the change of thread state (thread belongs to the queue change), then understand Monitor.pulseall is a lot easier .

The Monitor.pulseall implementation turns all threads waiting for resources into a ready state, and then, if the resource is freed, all ready threads have access to the resource and execute it.

C # Multi-Threading Usage 5-collaboration between Threads monitor

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.