using System;
using System.Threading;
class MessageBoard {
private String messages = "no messages";
public void Reader() {
try {
Monitor.Enter(this);
if (messages == "no messages") {
Console.WriteLine("{0} {1}",Thread.CurrentThread.Name, messages);
Console.WriteLine("{0} waiting",Thread.CurrentThread.Name);
Monitor.Wait(this);
}
Console.WriteLine("{0} {1}",Thread.CurrentThread.Name, messages);
} finally {
Monitor.Exit(this);
}
}
public void Writer() {
try {
Monitor.Enter(this);
messages = "Greetings!";
Console.WriteLine("{0} Done writing message",Thread.CurrentThread.Name);
Monitor.Pulse(this);
} finally {
Monitor.Exit(this);
}
}
public static void Main() {
MessageBoard myMessageBoard = new MessageBoard();
Thread reader = new Thread(new ThreadStart(myMessageBoard.Reader));
reader.Name = "ReaderThread:";
Thread writer = new Thread(new ThreadStart(myMessageBoard.Writer));
writer.Name = "WriterThread:";
reader.Start();
writer.Start();
}
}