static void Main() { //lock语句会解析成使用Monitor类 object obj = new object(); bool lockTaken = false; Monitor.TryEnter(obj, 500, ref lockTaken);//500 milliseconds。设置超时 if (lockTaken) //如果被锁定 { try { // acquired the lock // synchronized region for obj } finally { Monitor.Exit(obj); } } else { // didn't get the lock, do something else } int numTasks = 20; var state = new SharedState(); var tasks = new Task[numTasks]; for (int i = 0; i < numTasks; i++) { tasks[i] = new Task(new Job(state).DoTheJob); tasks[i].Start(); } for (int i = 0; i < numTasks; i++) { tasks[i].Wait();//waiting for thread finished. } Console.WriteLine("summarized {0}", state.State); Console.ReadLine(); }
static void Main() { object obj = new object(); bool lockTaken = false; Monitor.TryEnter(obj, 500, ref lockTaken); if (lockTaken) { try { // acquired the lock // synchronized region for obj } finally { Monitor.Exit(obj); } } else { // didn't get the lock, do something else } int numTasks = 20; var state = new SharedState(); var tasks = new Task[numTasks]; for (int i = 0; i < numTasks; i++) { tasks[i] = new Task(new Job(state).DoTheJob); tasks[i].Start(); } for (int i = 0; i < numTasks; i++) { tasks[i].Wait(); } Console.WriteLine("summarized {0}", state.State); }
static void SyncSample() { object obj = new object(); bool lockTaken = false; Monitor.TryEnter(obj, 500, ref lockTaken); if (lockTaken) { try { // acquired the lock // synchronized region for obj } finally { Monitor.Exit(obj); } } else { // didn't get the lock, do something else } int numTasks = 20; var state = new SharedState(); var tasks = new Task[numTasks]; for (int i = 0; i < numTasks; i++) { tasks[i] = Task.Run(() => new Job(state).DoTheJob()); } for (int i = 0; i < numTasks; i++) { tasks[i].Wait(); } Console.WriteLine("summarized {0}", state.State); }
public Job(SharedState sharedState) { this.sharedState = sharedState; }