大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > C#线程同步代码

C#线程同步代码

关键词:线程同步  阅读(658) 赞(14)

[摘要]本文主要讲解C#线程同步,通过实例的方法讲解。
 using System;
 using System.Threading;
 using System.Text;
 using System.Threading.Tasks;
 
 //多线程调试: 2013.10.08
 namespace ThreadExample
 {
     class SpinLockSample
     {
         public static void Test()
         {
             SpinLock sLock = new SpinLock();
             StringBuilder sb = new StringBuilder();
             Action action = () =>
                 {
                     bool gotLock = false;
                     for (int i = 0; i < 5; i++)
                     {
                         gotLock = false;
                         try
                         {
                             sLock.Enter(ref gotLock);
                             sb.Append(i.ToString());
                         }
                         catch (System.Exception ex)
                         {
 
                         }
                         finally
                         {
                             if (gotLock)
                             {
                                 sLock.Exit();
                             }
                         }
                     }
                 };
             //多线程调用action
             Parallel.Invoke(action,action,action);
             Console.WriteLine("Ouput {0}",sb.ToString());
         }
     }
 
     class App
     {
         private static object UsingPrinterLocker = new object();
         private static Mutex mutex = new Mutex();
 
         public static void Main()
         {
             SpinLockSample.Test();
             //TestPrint();
         }
 
         public static void TestPrint()
         {
             Thread thread;
             Random random = new Random();
 
             for (int i = 0; i < 10;i++ )
             {
                 thread = new Thread(MyThreadProc);
                 thread.Name = string.Format("Thread {0}", i);
                 Thread.Sleep(random.Next(3));
                 thread.Start();
             }
         }
 
         public static void MyThreadProc()
         {
             //UserPrinter();
             //UsePrinterWithMutex();
             UsePrinterWithMoniter();
         }
 
         public static void UsePrinterWithMutex()
         {
             mutex.WaitOne();
             try
             {
                 Console.WriteLine("{0} acquired thd lock", Thread.CurrentThread.Name);
                 Thread.Sleep(2000);
                 Console.WriteLine("{0} exiting lock.", Thread.CurrentThread.Name);
             }
             catch (System.Exception ex)
             {
                 
             }
             finally
             {
                 mutex.ReleaseMutex();
             }
         }
 
         public static void UsePrinterWithMoniter()
         {
             System.Threading.Monitor.Enter(UsingPrinterLocker);
             try
             {
                 Console.WriteLine("{0} acquired the lock", Thread.CurrentThread.Name);
                 Thread.Sleep(500);
                 Console.WriteLine("{0} exit lock", Thread.CurrentThread.Name);
             }
             catch (System.Exception ex)
             {
                 
             }
             finally
             {
                 System.Threading.Monitor.Exit(UsingPrinterLocker);
             }
         }
 
         public static void UserPrinter()
         {
             lock (UsingPrinterLocker)
             {
                 Console.WriteLine("{0} acquired the lock", Thread.CurrentThread.Name);
                 Thread.Sleep(500);
                 Console.WriteLine("{0} exiting lock.", Thread.CurrentThread.Name);
             }
         }
     }
 }


相关评论