home *** CD-ROM | disk | FTP | other *** search
- using System;
- using System.Threading;
-
- public class Gamma
- {
- };
-
- public class Alpha
- {
- public Gamma oGamma;
-
- public void Beta( )
- {
- // Obtain the lock
- Monitor.Enter(oGamma);
-
- int cLoop = 10;
- while ((cLoop--) > 0)
- {
- Console.Write(" Hello");
-
- // Release the lock and wait to be Notified
- Monitor.Wait(oGamma);
-
- // Signal for next thread in wait queue to proceed
- Monitor.Pulse(oGamma);
- }
-
- // Release the lock
- Monitor.Exit(oGamma);
- }
- };
-
- public class Delta
- {
- public Gamma oGamma;
-
- public Delta(Gamma G)
- {
- oGamma = G;
- }
-
- public void Beta( )
- {
- // Obtain the lock
- Monitor.Enter(oGamma);
-
- int cLoop = 10;
- while((cLoop--) > 0)
- {
- Console.WriteLine(" World");
- // Signal for next thread in wait queue to proceed
- Monitor.Pulse(oGamma);
-
- // Release the lock and wait to be Notified
- Monitor.Wait(oGamma);
- }
-
- // Release the lock
- Monitor.Exit(oGamma);
- }
- };
-
- public class Simple
- {
- public static int Main(String[] args)
- {
- Console.WriteLine("Thread Sync Two Sample");
-
- Gamma oGamma = new Gamma( );
-
- Alpha oAlpha = new Alpha( );
- oAlpha.oGamma = oGamma;
-
- // Create the 1st thread object, passing in the oAlphaBeta method
- // via a ThreadStart delegate
- Thread Thread1 = new Thread(new ThreadStart(oAlpha.Beta) );
-
- // Start the 1st thread
- Thread1.Start( );
- while(!Thread1.IsAlive);
-
- Delta oDelta = new Delta(oGamma);
-
- // Create the 2nd thread object, passing in the oDelta.Beta method
- // via a ThreadStart delegate, on the new oDelta instance
- Thread Thread2 = new Thread(new ThreadStart(oDelta.Beta) );
-
- // Start the 2nd thread
- Thread2.Start( );
-
- return 0;
- }
- };