program.cs

来自「C#高级编程第6版随书源代码 值得下载」· CS 代码 · 共 102 行

CS
102
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SynchronizationDemo
{

   public class SharedState
   {
      private int state = 0;

      public int State
      {
         get
         {
            return state;
         }
         //set
         //{

         //   state = value;
         //}
      }

      public int IncrementState()
      {
         lock (this)
         {
            return ++state;
         }
         // return Interlocked.Increment(ref state);
      }
	
   }

   public class Task
   {
      SharedState sharedState;
      public Task(SharedState sharedState)
      {
         this.sharedState = sharedState;
      }
      public void DoTheTask()
      {
         for (int i = 0; i < 50000; i++)
         {
            // lock (sharedState)
            {
               // sharedState.State += 1;
               sharedState.IncrementState();
            }
         }

      }
   }

   class Program
   {
      static void Main()
      {
         object obj = new object();
         if (Monitor.TryEnter(obj, 500))
         {
            try
            {
               // acquired the lock
               // synchronized region for obj
            }
            finally
            {
               Monitor.Exit(obj);
            }

         }
         else
         {
            // didn't get the lock, do something else
         }

         int numThreads = 20;
         SharedState state = new SharedState();
         Thread[] threads = new Thread[numThreads];

         for (int i = 0; i < numThreads; i++)
         {
            threads[i] = new Thread(new Task(state).DoTheTask);
            threads[i].Start();
         }

         //for (int i = 0; i < numThreads; i++)
         //{
         //   threads[i].Join();
         //}


         Console.WriteLine("summarized {0}", state.State);
      
      }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?