-
Notifications
You must be signed in to change notification settings - Fork 1
/
PingPongMonitor.cs
168 lines (130 loc) · 3.69 KB
/
PingPongMonitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Threading;
/// <summary>
/// Simple program that creates a ping and a pong thread which alternately display “Ping” and “Pong” on the console.
/// Uses lock and monitor objects to synchronise.
/// .NET Framework Version: 4.5
/// C# Compiler Version: 11.0 (Visual Studio 2012)
/// </summary>
namespace PingPongThreads
{
public class Ping
{
private Match game;
public Ping(Match game)
{
this.game = game;
}
public void Begin()
{
while (game.Ping());
}
};
public class Pong
{
private Match game;
public Pong(Match game)
{
this.game = game;
}
public void Begin()
{
while (game.Pong());
}
};
public class Match
{
enum Moves { Ping, Pong };
Moves lastMove = Moves.Pong;
int maximumTurns;
int currentTurn = 1;
public Match(int maximumTurns)
{
this.maximumTurns = maximumTurns;
}
public bool Ping()
{
lock (this)
{
if (this.currentTurn >= this.maximumTurns)
return false;
if (this.lastMove == Moves.Ping)
WaitForPong();
Console.WriteLine("Ping");
this.currentTurn++;
this.lastMove = Moves.Ping;
Monitor.Pulse(this); // Move waiting thread is moved to the ready queue.
return true;
}
}
private void WaitForPong()
{
try
{
Monitor.Wait(this);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public bool Pong()
{
lock (this)
{
if (this.currentTurn >= this.maximumTurns)
return false;
if (this.lastMove == Moves.Pong)
WaitForPing();
Console.WriteLine("Pong");
this.currentTurn++;
this.lastMove = Moves.Pong;
Monitor.Pulse(this); // Move waiting thread is moved to the ready queue.
return true;
}
}
private void WaitForPing()
{
try
{
Monitor.Wait(this);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Ready... Set... Go!");
Console.WriteLine();
var game = new Match(6);
Thread pingThread = StartPingThread(game);
Thread pongThread = StartPongThread(game);
pingThread.Join(); // Wait until pingThread finishes.
pongThread.Join(); // Wait until pongThread finishes.
Console.WriteLine("Done!");
}
private static Thread StartPongThread(Match game)
{
var pong = new Pong(game);
Thread pongThread = new Thread(new ThreadStart(pong.Begin));
pongThread.Start();
// Wait for thread to start
while (!pongThread.IsAlive);
return pongThread;
}
private static Thread StartPingThread(Match game)
{
var ping = new Ping(game);
Thread pingThread = new Thread(new ThreadStart(ping.Begin));
pingThread.Start();
// Wait for thread to start
while (!pingThread.IsAlive);
return pingThread;
}
}
}