首页 > 编程语言 > 详细

C#多线程方法同步

时间:2014-08-08 17:52:06      阅读:272      评论:0      收藏:0      [点我收藏+]
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#多线程方法同步

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!