using System.Threading;
namespace ThreadTrap1
{
    /// <summary>
    /// This example shows a threading Trap, you simply can't
    /// rely on threads executing in the order in which they
    /// are started.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Thread T1 = new Thread(new ThreadStart(Increment));
            Thread T2 = new Thread(new ThreadStart(Increment));
            T1.Name = "T1";
            T2.Name = "T2";
            T1.Start();
            T2.Start();
            Console.ReadLine();
        }
        private static void Increment()
        {
            for (int i = 0; i < 100000;i++ )
                if (i % 10000 == 0)
                    Console.WriteLine("Thread Name {0}",
                        Thread.CurrentThread.Name);
            WriteDone(Thread.CurrentThread.Name);
        }
        private static void WriteDone(string threadName)
        {
            switch (threadName)
            {
                case "T1" :
                    Console.WriteLine("T1 Finished");
                    break;
                case "T2":
                    Console.WriteLine("T2 Finished");
                    break;
            }
        }
    }
}
No comments:
Post a Comment