/// <summary> /// ShareState,不锁定 /// Job中方法DoTheJobByJobMethodLock,锁定 /// </summary> public string DoTheJobByJobMethodLock() { StringBuilder sb = new StringBuilder(); int numTasks = 20; var state = new ShareState(); var tasks = new Task[numTasks]; for (int i = 0; i < numTasks; i++) { tasks[i] = Task.Run(() => new Job(state).DoTheJobByJobMethodLock()); } Task.WaitAll(tasks); sb.Append($"总共循环次数{state.State}\r\n"); return(sb.ToString()); }
public Job(ShareState shareState) { _shareState = shareState; }