2
Reply

Write A program To Print "INDIA" "CHINA" "INDIA",CHINA using Two Threads in C#

Arman Kumar

Arman Kumar

11y
2.3k
0
Reply

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

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