zoukankan      html  css  js  c++  java
  • 多线程同步控制 ManualResetEvent AutoResetEvent MSDN

    代码
    using System;
    using System.Threading;

    class CalculateTest
    {
        
    static void Main()
        {
            Calculate calc 
    = new Calculate();
            Console.WriteLine(
    "Result = {0}."
                calc.Result(
    234).ToString());
            Console.WriteLine(
    "Result = {0}."
                calc.Result(
    55).ToString());
        }
    }

    class Calculate
    {
        
    double baseNumber, firstTerm, secondTerm, thirdTerm;
        AutoResetEvent[] autoEvents;
        ManualResetEvent manualEvent;

        
    // Generate random numbers to simulate the actual calculations.
        Random randomGenerator;

        
    public Calculate()
        {
            autoEvents 
    = new AutoResetEvent[]
            {
                
    new AutoResetEvent(false),
                
    new AutoResetEvent(false),
                
    new AutoResetEvent(false)
            };

            manualEvent 
    = new ManualResetEvent(false);
        }

        
    void CalculateBase(object stateInfo)
        {
            baseNumber 
    = randomGenerator.NextDouble();

            
    // Signal that baseNumber is ready.
            manualEvent.Set();
        }

        
    // The following CalculateX methods all perform the same
        
    // series of steps as commented in CalculateFirstTerm.

        
    void CalculateFirstTerm(object stateInfo)
        {
            
    // Perform a precalculation.
            double preCalc = randomGenerator.NextDouble();

            
    // Wait for baseNumber to be calculated.
            manualEvent.WaitOne();

            
    // Calculate the first term from preCalc and baseNumber.
            firstTerm = preCalc * baseNumber * 
                randomGenerator.NextDouble();

            
    // Signal that the calculation is finished.
            autoEvents[0].Set();
        }

        
    void CalculateSecondTerm(object stateInfo)
        {
            
    double preCalc = randomGenerator.NextDouble();
            manualEvent.WaitOne();
            secondTerm 
    = preCalc * baseNumber * 
                randomGenerator.NextDouble();
            autoEvents[
    1].Set();
        }

        
    void CalculateThirdTerm(object stateInfo)
        {
            
    double preCalc = randomGenerator.NextDouble();
            manualEvent.WaitOne();
            thirdTerm 
    = preCalc * baseNumber * 
                randomGenerator.NextDouble();
            autoEvents[
    2].Set();
        }

        
    public double Result(int seed)
        {
            randomGenerator 
    = new Random(seed);

            
    // Simultaneously calculate the terms.
            ThreadPool.QueueUserWorkItem(
                
    new WaitCallback(CalculateBase));
            ThreadPool.QueueUserWorkItem(
                
    new WaitCallback(CalculateFirstTerm));
            ThreadPool.QueueUserWorkItem(
                
    new WaitCallback(CalculateSecondTerm));
            ThreadPool.QueueUserWorkItem(
                
    new WaitCallback(CalculateThirdTerm));

            
    // Wait for all of the terms to be calculated.
            WaitHandle.WaitAll(autoEvents);

            
    // Reset the wait handle for the next calculation.
            manualEvent.Reset();

            
    return firstTerm + secondTerm + thirdTerm;
        }
    }


  • 相关阅读:
    LightOJ 1344 Aladdin and the Game of Bracelets
    CF 1132A,1132B,1132C,1132D,1132E,1132F(Round 61 A,B,C,D,E,F)题解
    CF 1130A 1130B 1130C1129A1 1129A2 1129B(Round542A B C D1 D2 E)题解
    CF 1131A,1131B,1131C,1131D,1131F(Round541 A,B,C,D,F)题解
    CoderForces-Round60D(1117) Magic Gems
    CoderForces Round60-(1117A,1117B,1117C题解)
    LightOJ 1038 Race To 1 Again(概率DP)
    XHXJ'S LIS(数位DP)
    CF 55D Beautiful Numbers(数位DP)
    LightOJ 1229 Tablecross
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1933374.html
Copyright © 2011-2022 走看看