zoukankan      html  css  js  c++  java
  • ThreadPool,线程池等待多个子线程完成.

    from: http://msdn.microsoft.com/en-us/library/3dasc8as(VS.80).aspx

    using System;
    using System.Threading;
    
    public class Fibonacci
    {
        public Fibonacci(int n, ManualResetEvent doneEvent)
        {
            _n = n;
            _doneEvent = doneEvent;
        }
    
        // Wrapper method for use with thread pool.
        public void ThreadPoolCallback(Object threadContext)
        {
            int threadIndex = (int)threadContext;
            Console.WriteLine("thread {0} started...", threadIndex);
            _fibOfN = Calculate(_n);
            Console.WriteLine("thread {0} result calculated...", threadIndex);
            _doneEvent.Set();
        }
    
        // Recursive method that calculates the Nth Fibonacci number.
        public int Calculate(int n)
        {
            if (n <= 1)
            {
                return n;
            }
    
            return Calculate(n - 1) + Calculate(n - 2);
        }
    
        public int N { get { return _n; } }
        private int _n;
    
        public int FibOfN { get { return _fibOfN; } }
        private int _fibOfN;
    
        private ManualResetEvent _doneEvent;
    }
    
    public class ThreadPoolExample
    {
        static void Main()
        {
            const int FibonacciCalculations = 10;
    
            // One event is used for each Fibonacci object
            ManualResetEvent[] doneEvents = new ManualResetEvent[FibonacciCalculations];
            Fibonacci[] fibArray = new Fibonacci[FibonacciCalculations];
            Random r = new Random();
    
            // Configure and launch threads using ThreadPool:
            Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
            for (int i = 0; i < FibonacciCalculations; i++)
            {
                doneEvents[i] = new ManualResetEvent(false);
                Fibonacci f = new Fibonacci(r.Next(20,40), doneEvents[i]);
                fibArray[i] = f;
                ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
            }
    
            // Wait for all threads in pool to calculation...
            WaitHandle.WaitAll(doneEvents);
            Console.WriteLine("All calculations are complete.");
    
            // Display the results...
            for (int i= 0; i<FibonacciCalculations; i++)
            {
                Fibonacci f = fibArray[i];
                Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN);
            }
        }
    }
    
  • 相关阅读:
    1370
    1336
    1298
    1289
    Buy Tickets(poj2828)
    The Balance(poj2142)
    1067
    Lightoj1011
    1319
    Back to Underworld(lightoj 1009)
  • 原文地址:https://www.cnblogs.com/wucg/p/2001345.html
Copyright © 2011-2022 走看看