代码
using System.Diagnostics; namespace ConsoleAppLockDemo { internal class Program { static object lockobj = new object(); static ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim(); static async Task Main(string[] args) { //外循环数量 int outerLoopCount = 1 ; //线程数量 int ThreadNum = 100000; Stopwatch stopwatch = new Stopwatch(); ///1原子锁 int islock = 0; int countInterlocked = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { while (Interlocked.CompareExchange(ref islock, 1, 0) == 1) { } countInterlocked = countInterlocked + 2; Interlocked.Exchange(ref islock, 0); }); } } stopwatch.Stop(); Console.WriteLine($">>Interlocked.Exchange\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds*1.0/ outerLoopCount} ms/单循环"); //2临界锁 int countlockobj = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { lock (lockobj) { countlockobj = countlockobj + 2; } }); } } stopwatch.Stop(); Console.WriteLine($">>lock\t\t\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); int InterlockedCount = 0; //3原子性操作 stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { Interlocked.Add(ref InterlockedCount, 2); }); } } stopwatch.Stop(); int InterlockedCount2 = 0; Console.WriteLine($">>Interlocked Add\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { Interlocked.Increment(ref InterlockedCount2); }); } } stopwatch.Stop(); Console.WriteLine($">>Interlocked Increment\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); //4 读写锁 int ReaderWriterLockCount = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { readerWriterLock.EnterWriteLock(); ReaderWriterLockCount = ReaderWriterLockCount + 2; readerWriterLock.ExitWriteLock(); }); } } stopwatch.Stop(); Console.WriteLine($">>ReaderWriterLockSlim\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); int ReaderWriterLockCount3 = 0; /// ReaderWriterLock readerWriterLock3 = new ReaderWriterLock(); stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { readerWriterLock3.AcquireWriterLock(2000); ReaderWriterLockCount3 = ReaderWriterLockCount3 + 2; readerWriterLock3.ReleaseWriterLock(); }); } } stopwatch.Stop(); Console.WriteLine($">>ReaderWriterLock\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); //5信号量 Semaphore semaphore=new Semaphore(1,1); int SemaphoreCount = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { semaphore.WaitOne(); SemaphoreCount = SemaphoreCount + 2; semaphore.Release(); }); } } stopwatch.Stop(); Console.WriteLine($">>Semaphore\t\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1 ); int semaphoreSlimCount = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { semaphoreSlim.Wait(); //or //semaphoreSlim.Wait(2000); semaphoreSlimCount = semaphoreSlimCount + 2; semaphoreSlim.Release(); }); } } stopwatch.Stop(); Console.WriteLine($">>SemaphoreSlim\t\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); //6 事件 //初始化为true AutoResetEvent autoResetEvent = new AutoResetEvent( true ); int autoResetEventCount = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { autoResetEvent.WaitOne(); //or //autoResetEvent.WaitOne(2000 ); autoResetEventCount = autoResetEventCount + 2; autoResetEvent.Set(); }); } } stopwatch.Stop(); Console.WriteLine($">>AutoResetEvent\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); //阻塞线程 autoResetEvent.Reset(); ManualResetEvent manualResetEvent = new ManualResetEvent(true); int manualResetEventCount = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { manualResetEvent.WaitOne(); manualResetEventCount = manualResetEventCount + 2; manualResetEvent.Set(); }); } } stopwatch.Stop(); Console.WriteLine($">>ManualResetEvent\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); manualResetEvent.Reset(); ManualResetEventSlim manualResetEventSlim = new ManualResetEventSlim(true); int manualResetEventSlimCount = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { manualResetEventSlim.Wait(); manualResetEventSlimCount = manualResetEventSlimCount + 2; manualResetEventSlim.Set(); }); } } stopwatch.Stop(); Console.WriteLine($">>ManualResetEventSlim\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); manualResetEventSlim.Reset(); //7互斥 Mutex mutex = new Mutex(); int mutexCount = 0; stopwatch.Restart(); for (int j = 0; j < outerLoopCount; j++) { for (int i = 0; i < ThreadNum; i++) { await Task.Factory.StartNew(() => { mutex.WaitOne(); mutexCount = mutexCount + 2; mutex.ReleaseMutex(); }); } } stopwatch.Stop(); Console.WriteLine($">>Mutex\t\t\t外循环数量:{outerLoopCount},线程数量:{ThreadNum},执行时间:{stopwatch.ElapsedMilliseconds} ms,单循环{ThreadNum}线程: 执行速度:{stopwatch.ElapsedMilliseconds * 1.0 / outerLoopCount} ms/单循环"); Console.ReadLine(); } } }
10000线程测试结果
10000线程测试结果
1000线程测试结果
标签:C#,int,单循环,线程,outerLoopCount,stopwatch,ThreadNum From: https://www.cnblogs.com/JohnnyLei/p/17576807.html