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();
}
}
|