Monday, 13 January 2014

C# ThreadPool Program-----------|||||||||||||||||||||||||

C# Code:

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

namespace ThreadPoolProgram
{
    public class ThreadPoolTest
    {
        public static object showThread = new object();
        public static int runThread = 20;
        public static void Main()
        {
            for (int i = 0; i < runThread; i++)
            {
                ThreadPool.QueueUserWorkItem(Display, i);
            }
            Console.WriteLine("Running 20 Threads to one by one and stopping them after 3 seconds.\n");
            lock (showThread)
            {
                while (runThread > 0)
               
                    Monitor.Wait(showThread);
               
            }
            Console.WriteLine("All threads stopped succesfully!");
            Console.ReadLine();
        }
        public static void Display(object objThread)
        {
            Console.WriteLine("Started Thread:" + objThread);
            Thread.Sleep(3000);
            Console.WriteLine("Ended Thread:" + objThread);
            lock (showThread)
            {
                Monitor.Pulse(showThread);
            }



        }

    }
}

Output:


No comments:

Post a Comment