home *** CD-ROM | disk | FTP | other *** search
- using System;
- using System.Threading;
-
- public class Alpha
- {
-
- // The method that will be called when the thread is started
- public void Beta()
- {
- int iThreadID = Thread.CurrentThread.GetHashCode( );
- int cLoop = 100;
- while((cLoop--)>0)
- {
- // Obtain the lock
- Monitor.Enter(this);
- Console.Write("{0} Thread, ",Thread.CurrentThread.Name);
- Console.WriteLine("Hash: {0}, Hello!",iThreadID);
- // Release the lock
- Monitor.Exit(this);
- }
- }
- };
-
- public class Simple
- {
- public static int Main(String[] args)
- {
- Console.WriteLine("Thread Sync One Sample");
- Alpha oAlpha = new Alpha( );
-
- // Create the 1st thread object, passing in the Alpha.Beta method
- // via a ThreadStart delegate
- Thread oThread1 = new Thread(new ThreadStart(oAlpha.Beta));
- oThread1.Name = "Yellow";
-
- // Start the 1st thread
- oThread1.Start( );
-
- // Spin waiting for the started thread to become alive
- while(!oThread1.IsAlive) ;
-
- // Create the 2nd thread object, passing in the Alpha.Beta method
- // via a ThreadStart delegate, on the same oAlpha instance
- Thread oThread2 = new Thread(new ThreadStart(oAlpha.Beta) );
- oThread2.Name = " Green";
-
- // Start the 2nd thread
- oThread2.Start( );
-
- // Spin waiting for the started thread to Start
- while(oThread2.ThreadState==ThreadState.Unstarted) ;
-
- return 0;
- }
- };