/// <summary>
        /// ShareStateMethodLock中的方法,锁定
        /// Job方法DoTheJobByShareStateMethodLock,不锁定
        /// </summary>
        public string DoTheJobByShareStateMethodLock()
        {
            StringBuilder sb                   = new StringBuilder();
            int           numTasks             = 20;
            var           shareStateMethodLock = new ShareStateMethodLock();
            var           tasks                = new Task[numTasks];

            for (int i = 0; i < numTasks; i++)
            {
                tasks[i] = Task.Run(() => new Job(shareStateMethodLock).DoTheJobByShareStateMethodLock());
            }
            Task.WaitAll(tasks);

            sb.Append($"总共循环次数{shareStateMethodLock.State}\r\n");
            return(sb.ToString());
        }
Ejemplo n.º 2
0
 public Job(ShareStateMethodLock shareStateMethodLock)
 {
     _ShareStateMethodLock = shareStateMethodLock;
 }