C#多线程时对同一资源加锁实现互斥访问
生活随笔
收集整理的這篇文章主要介紹了
C#多线程时对同一资源加锁实现互斥访问
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
//using System.Diagnostics;namespace 多線程
{#region 我的程序class ResultCount{public static int PassNumber { get; set; }public static int FailNumber { get; set; }}class Program{private static object _lock = new object();static void Main(string[] args){Thread[] threadArray = new Thread[10];for (int i = 0; i < 10; i++){threadArray[i] = new Thread(new ThreadStart(PassCountAdd));}foreach (var item in threadArray){item.Start();}foreach (var item in threadArray){//Join: 一個同步方法,該方法阻止調用線程 (即,調用方法的線程) ,直到 Join 調用方法的線程完成。//使用此方法可以確保線程已終止。 如果線程未終止,調用方將無限期阻止。 在下面的示例中,//Thread1 線程調用的 Join() 方法 Thread2 ,這會導致 Thread1 一直阻止到 Thread2 完成為止。item.Join();}Console.WriteLine(ResultCount.PassNumber);Console.WriteLine(ResultCount.FailNumber);}static void PassCountAdd(){for (int i = 0; i < 100000; i++){lock (_lock){ResultCount.PassNumber++;ResultCount.FailNumber++;}}}}#endregion#region Microsoft官網例程//public class Example//{// static Thread thread1, thread2;// public static void Main()// {// thread1 = new Thread(new ThreadStart(ThreadProc));// thread1.Name = "Thread1";// thread1.Start();// thread2 = new Thread(new ThreadStart(ThreadProc));// thread2.Name = "Thread2";// thread2.Start();// }// private static void ThreadProc()// {// Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);// if (Thread.CurrentThread.Name == "Thread1"// && (thread2.ThreadState != ThreadState.Unstarted))// {// //Join: 一個同步方法,該方法阻止調用線程 (即,調用方法的線程) ,直到 Join 調用方法的線程完成。// //使用此方法可以確保線程已終止。 如果線程未終止,調用方將無限期阻止。 在下面的示例中,// //Thread1 線程調用的 Join() 方法 Thread2 ,這會導致 Thread1 一直阻止到 Thread2 完成為止。// thread2.Join();// }// Thread.Sleep(4000);// Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);// Console.WriteLine("Thread1: {0}", thread1.ThreadState);// Console.WriteLine("Thread2: {0}", thread2.ThreadState);// }//}#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;namespace ConsoleApplication1
{public static class MyLock //使用同一把鎖對同一資源進行互斥訪問保護{public static object _lock = new object();}class Program{static void Main(string[] args){Student stu1 = new Student(1, "Tom", ConsoleColor.Green);Student stu2 = new Student(2, "Jerry", ConsoleColor.Yellow);Student stu3 = new Student(3, "Jason", ConsoleColor.Red);#region 使用Action委托//Action action1 = new Action(stu1.DoHomeWork);//Action action2 = new Action(stu2.DoHomeWork);//Action action3 = new Action(stu3.DoHomeWork);//action1.BeginInvoke(null, null);//action2.BeginInvoke(null, null);//action3.BeginInvoke(null, null);#endregion#region Thread多線程//Thread thread1 = new Thread(new ThreadStart(stu1.DoHomeWork));//Thread thread2 = new Thread(new ThreadStart(stu2.DoHomeWork));//Thread thread3 = new Thread(new ThreadStart(stu3.DoHomeWork));//thread1.Start();//thread2.Start();//thread3.Start();#endregion#region Task多線程Task task1 = new Task(new Action(stu1.DoHomeWork));Task task2 = new Task(new Action(stu2.DoHomeWork));Task task3 = new Task(new Action(stu3.DoHomeWork));task1.Start();task2.Start();task3.Start();#endregionfor (int i = 0; i < 10; i++){lock(MyLock._lock){Console.ForegroundColor = ConsoleColor.White;Console.WriteLine("Main thread{0}!", i + 1);}Thread.Sleep(100);}}}class Student{public int Id { get; set; }public string Name { get; set; }public ConsoleColor Color { get; set; }public Student(int id, string name, ConsoleColor color){Id = id;Name = name;Color = color;}public void DoHomeWork(){for (int i = 0; i < 5; i++){lock(MyLock._lock){Console.ForegroundColor = Color;Console.WriteLine("Id:{0},Name:{1},Color:{2}, doing homework {3}hours.",Id, Name, Console.ForegroundColor, i + 1);}Thread.Sleep(100);}}}
}
?
?
總結
以上是生活随笔為你收集整理的C#多线程时对同一资源加锁实现互斥访问的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: robocode 安装 使用
- 下一篇: C#代码整洁之道:代码重构与性能提升