public static void Main(String[] args)
        {
            // Count of tests.
            const int count = 10;

            // Per event is used for each Fibonacci object.
            ManualResetEvent[] doneEvents = new ManualResetEvent[count];
            Fibonacci[]        array      = new Fibonacci[count];

            // Random number generator.
            Random rand = new Random();

            // Work with threads with MyThreadPool.
            Console.WriteLine("Starting {0} tasks.", count);

            for (int i = 0; i < count; i++)
            {
                doneEvents[i] = new ManualResetEvent(false);
                array[i]      = new Fibonacci(rand.Next(25, 40), doneEvents[i]);
                MyThreadPool.QueueUserWorkItem(array[i].ThreadPoolCallback);
            }

            Console.WriteLine("Main thread does some work than sleep.");
            Thread.Sleep(7000);

            //Wait for all threads in pool to calculation.
            WaitHandle.WaitAll(doneEvents);
            Console.WriteLine("All calculations are complete.");

            // Results.
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("Fibonacci[{0}] = {1}", array[i].Number, array[i].Result);
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            MyThreadPool tp = new MyThreadPool();

            for (int i = 0; i < 1000; i++)
            {
                tp.QueueWork(i, new WorkDelegate(PerformWork));
            }

            Console.ReadLine();
            tp.Shutdown();
        }
示例#3
0
        static void Main(string[] args)
        {
            MyThreadPool pool = new MyThreadPool();


            for (int i = 0; i < 8; i++)
            {
                var temp = i;
                Thread.Sleep(300);
                pool.Execute(new MyTask(() =>
                {
                    Thread.Sleep(1000);
                    Console.WriteLine("Running thread ID =  " + Thread.CurrentThread.Name + "  " + temp);
                }));
            }

            pool.Stop();
            Console.WriteLine();
        }
示例#4
0
 public MyTask(MyThreadPool pool, Func <TResult> supplier)
 {
     this.threadPool = pool;
     this.supplier   = supplier;
 }