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();
        }
    }
}

  

相关文章:

  • 2021-11-14
  • 2022-12-23
  • 2022-01-22
  • 2022-12-23
  • 2021-05-21
  • 2021-12-26
  • 2022-12-23
  • 2021-07-01
猜你喜欢
  • 2021-12-05
  • 2022-02-21
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2022-01-14
相关资源
相似解决方案