码迷,mamicode.com
首页 > 编程语言 > 详细

数据同步 线程 c#

时间:2017-11-07 18:06:09      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:user   task   barrier   should   div   使用   span   array   date   

手动进行线程同步

1>使用WaitHandle做数据同步或者数据等待 根据的是

AutoResetEvent() 是否阻塞 如果没有阻塞则正常 waitall是全部没有阻塞就可以通过 waitall是一个没有阻塞就通过

 

using System;
using System.Threading;

public sealed class App 
{
    // Define an array with two AutoResetEvent WaitHandles.
    static WaitHandle[] waitHandles = new WaitHandle[] 
    {
        new AutoResetEvent(false),
        new AutoResetEvent(false)
    };

    // Define a random number generator for testing.
    static Random r = new Random();

    static void Main() 
    {
        // Queue up two tasks on two different threads; 
        // wait until all tasks are completed.
        DateTime dt = DateTime.Now;
        Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
        WaitHandle.WaitAll(waitHandles);
        // The time shown below should match the longest task.
        Console.WriteLine("Both tasks are completed (time waited={0})", 
            (DateTime.Now - dt).TotalMilliseconds);

        // Queue up two tasks on two different threads; 
        // wait until any tasks are completed.
        dt = DateTime.Now;
        Console.WriteLine();
        Console.WriteLine("The main thread is waiting for either task to complete.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
        int index = WaitHandle.WaitAny(waitHandles);
        // The time shown below should match the shortest task.
        Console.WriteLine("Task {0} finished first (time waited={1}).",
            index + 1, (DateTime.Now - dt).TotalMilliseconds);
    }

    static void DoTask(Object state) 
    {
        AutoResetEvent are = (AutoResetEvent) state;
        int time = 1000 * r.Next(2, 10);
        Console.WriteLine("Performing a task for {0} milliseconds.", time);
        Thread.Sleep(time);
        are.Set();
    }
}

 

 

2> C# 4.0  Barrier类

        public static void Speak()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.Write(i + "");
                d.SignalAndWait();//等待 当有3个线程进入了这个方法的时候就可以继续

            }
        }

        /// <summary>
        /// 当到达3个线程触发Action<Barrier>中的内容 第一个是要求达到的线程数
        /// </summary>
        static Barrier d = new Barrier(3, d1 => { Console.WriteLine(""); });

        static void Main(string[] args)
        {


            new Thread(Speak).Start();
            new Thread(Speak).Start();
            new Thread(Speak).Start();
            Console.ReadKey();



        }

 

数据同步 线程 c#

标签:user   task   barrier   should   div   使用   span   array   date   

原文地址:http://www.cnblogs.com/yangshasha/p/7799377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!