Arman Kumar
Write A program To Print "INDIA" "CHINA" "INDIA",CHINA using Two Threads in C#
By Arman Kumar in C# on Aug 25 2013
  • vipin kv
    Oct, 2015 6

    class Program{static EventWaitHandle chinaWaitHandle = new AutoResetEvent(false);static EventWaitHandle indiaWaitHandle = new AutoResetEvent(false);static void Main(string[] args){Thread t1 = new Thread(new ThreadStart(PrintIndia));t1.Start();Thread t2 = new Thread(new ThreadStart(PrintChina));t2.Start();}static void PrintChina(){for(int i=0;i <10;i++){chinaWaitHandle.WaitOne();Console.WriteLine("China");indiaWaitHandle.Set();}}static void PrintIndia(){for (int i = 0; i < 10; i++){ Console.WriteLine("India");chinaWaitHandle.Set();indiaWaitHandle.WaitOne();}}}}

    • 0
  • Arman Kumar
    Aug, 2013 25

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;

    namespace Print_Message
    {
        class Program
        {
            public void firstMessage()
            {
                for (int i = 0; i < 2; i++) 
                {
                    Console.WriteLine("INDIA");
                    Thread.Sleep(1000);
                }
            }
            public void secondMessage()
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine("CHINA");
                    Thread.Sleep(1000);
                }
            }
            static void Main(string[] args)
            {
                Program prg = new Program();
                ThreadStart thread_start1 = new ThreadStart(prg.firstMessage);
                Thread thread1 = new Thread(thread_start1);
                thread1.Start();

                Thread.Sleep(500);
                ThreadStart thraed_start2 = new ThreadStart(prg.secondMessage);
                Thread thread2 = new Thread(thraed_start2);           
                 thread2.Start(); 
                Console.ReadLine();
            }     
        }
    }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS