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

C#多线程方法同步

时间:2014-08-08 17:52:06      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   ar   cti   div   line   

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

namespace TestMethodSynchronized
{
    class SyncHelper
    {
        static int cnt = 0;

        [MethodImpl(MethodImplOptions.Synchronized)]
        public static void Execute(object state)
        {
            int t = cnt;
            Console.WriteLine("ThreadId:{2,-5}, Sate:{1},Excute at {0},计时器:{3}", DateTime.Now, state.ToString(),
                Thread.CurrentThread.ManagedThreadId, t);
            Thread.Sleep(500);
            ++t;
            cnt = t;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                ThreadPool.QueueUserWorkItem(SyncHelper.Execute,i);
            }

            Console.WriteLine("done.");
            Console.ReadKey();
        }
    }
}

  

C#多线程方法同步,布布扣,bubuko.com

C#多线程方法同步

标签:blog   os   io   for   ar   cti   div   line   

原文地址:http://www.cnblogs.com/wucg/p/3899659.html

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