Coordinate two threads using Monitor : Monitor « Thread « C# / CSharp Tutorial






using System;
using System.Threading;

public class MainClass
{
    static private int counter = 0;

    static private object theLock = new Object();

    static private void ThreadFunc1() {
        lock( theLock ) {
            for( int i = 0; i < 50; ++i ) {
                Monitor.Wait( theLock, Timeout.Infinite );
                Console.WriteLine( "{0} from Thread {1}", ++counter, Thread.CurrentThread.GetHashCode() );
                Monitor.Pulse( theLock );
            }
        }
    }

    static private void ThreadFunc2() {
        lock( theLock ) {
            for( int i = 0; i < 50; ++i ) {
                Monitor.Pulse( theLock );
                Monitor.Wait( theLock, Timeout.Infinite );
                Console.WriteLine( "{0} from Thread {1}", ++counter, Thread.CurrentThread.GetHashCode() );
            }
        }
    }

    static void Main() {
        Thread thread1 = new Thread( new ThreadStart(ThreadFunc1) );
        Thread thread2 = new Thread( new ThreadStart(ThreadFunc2) );
        thread1.Start();
        thread2.Start();
    }
}
1 from Thread 3
2 from Thread 4
3 from Thread 3
4 from Thread 4
5 from Thread 3
6 from Thread 4
7 from Thread 3
8 from Thread 4
9 from Thread 3
10 from Thread 4
11 from Thread 3
12 from Thread 4
13 from Thread 3
14 from Thread 4
15 from Thread 3
16 from Thread 4
17 from Thread 3
18 from Thread 4
19 from Thread 3
20 from Thread 4
21 from Thread 3
22 from Thread 4
23 from Thread 3
24 from Thread 4
25 from Thread 3
26 from Thread 4
27 from Thread 3
28 from Thread 4
29 from Thread 3
30 from Thread 4
31 from Thread 3
32 from Thread 4
33 from Thread 3
34 from Thread 4
35 from Thread 3
36 from Thread 4
37 from Thread 3
38 from Thread 4
39 from Thread 3
40 from Thread 4
41 from Thread 3
42 from Thread 4
43 from Thread 3
44 from Thread 4
45 from Thread 3
46 from Thread 4
47 from Thread 3
48 from Thread 4
49 from Thread 3
50 from Thread 4
51 from Thread 3
52 from Thread 4
53 from Thread 3
54 from Thread 4
55 from Thread 3
56 from Thread 4
57 from Thread 3
58 from Thread 4
59 from Thread 3
60 from Thread 4
61 from Thread 3
62 from Thread 4
63 from Thread 3
64 from Thread 4
65 from Thread 3
66 from Thread 4
67 from Thread 3
68 from Thread 4
69 from Thread 3
70 from Thread 4
71 from Thread 3
72 from Thread 4
73 from Thread 3
74 from Thread 4
75 from Thread 3
76 from Thread 4
77 from Thread 3
78 from Thread 4
79 from Thread 3
80 from Thread 4
81 from Thread 3
82 from Thread 4
83 from Thread 3
84 from Thread 4
85 from Thread 3
86 from Thread 4
87 from Thread 3
88 from Thread 4
89 from Thread 3
90 from Thread 4
91 from Thread 3
92 from Thread 4
93 from Thread 3
94 from Thread 4
95 from Thread 3
96 from Thread 4
97 from Thread 3
98 from Thread 4
99 from Thread 3
100 from Thread 4








20.19.Monitor
20.19.1.Use Wait() and Pulse() to create a ticking clock
20.19.2.Use Monitors
20.19.3.Monitor: Enter and Exit
20.19.4.Monitor: try to enter
20.19.5.Coordinate two threads using Monitor
20.19.6.Use Monitor to control more than one Threads
20.19.7.Increment Monitor
20.19.8.Monitor Pool
20.19.9.Throw exception between Monitor.Enter and Montor.Exit
20.19.10.Using A Monitor
20.19.11.Use lock and Monitor to coordinate Producer and Consumer