日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#多线程时对同一资源加锁实现互斥访问

發布時間:2023/12/10 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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#多线程时对同一资源加锁实现互斥访问的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。