楼主: ReneeBK
948 9

【Eugene Agafonov】Multithreading with C# Cookbook [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4900份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49655 个
通用积分
55.9937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2017-1-29 12:00:30 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

Multithreading with C# Cookbook, 2nd Edition.rar (2.07 MB) 本附件包括:
  • Multithreading with C# Cookbook, 2nd Edition.pdf


  1. Author:Eugene Agafonov
  2. Isbn:1785881256
  3. Year:2016
  4. Pages:264
  5. Language:English
  6. File size:12.3 MB
  7. File format:PDF
  8. Category:C#
复制代码



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Cookbook reading thread Eugene Eugen

沙发
ReneeBK 发表于 2017-1-29 12:03:45
Performing basic atomic operations
  1. In the Program.cs file, add the following using directives:
  2. using System;
  3. using System.Threading;
  4. using static System.Console;
  5. 3. Below the Main method, add the following code snippet:
  6. static void TestCounter(CounterBase c)
  7. {
  8. for (int i = 0; i < 100000; i++)
  9. {
  10. c.Increment();
  11. c.Decrement();
  12. }
  13. }
  14. class Counter : CounterBase
  15. {
  16. private int _count;
  17. public int Count => _count;
  18. public override void Increment()
  19. {
  20. _count++;
  21. }
  22. public override void Decrement()
  23. {
  24. _count--;
  25. }
  26. }
  27. class CounterNoLock : CounterBase
  28. {
  29. private int _count;
  30. public int Count => _count;
  31. public override void Increment()
  32. {
  33. Interlocked.Increment(ref _count);
  34. }
  35. public override void Decrement()
  36. {
  37. Thread Synchronization
  38. 30
  39. Interlocked.Decrement(ref _count);
  40. }
  41. }
  42. abstract class CounterBase
  43. {
  44. public abstract void Increment();
  45. public abstract void Decrement();
  46. }
  47. 4. Inside the Main method, add the following code snippet:
  48. WriteLine("Incorrect counter");
  49. var c = new Counter();
  50. var t1 = new Thread(() => TestCounter(c));
  51. var t2 = new Thread(() => TestCounter(c));
  52. var t3 = new Thread(() => TestCounter(c));
  53. t1.Start();
  54. t2.Start();
  55. t3.Start();
  56. t1.Join();
  57. t2.Join();
  58. t3.Join();
  59. WriteLine($"Total count: {c.Count}");
  60. WriteLine("--------------------------");
  61. WriteLine("Correct counter");
  62. var c1 = new CounterNoLock();
  63. t1 = new Thread(() => TestCounter(c1));
  64. t2 = new Thread(() => TestCounter(c1));
  65. t3 = new Thread(() => TestCounter(c1));
  66. t1.Start();
  67. t2.Start();
  68. t3.Start();
  69. t1.Join();
  70. t2.Join();
  71. t3.Join();
  72. WriteLine($"Total count: {c1.Count}");
  73. 5. Run the program.
复制代码

藤椅
MouJack007 发表于 2017-1-29 12:04:53
谢谢楼主分享!

板凳
MouJack007 发表于 2017-1-29 12:05:19

报纸
ReneeBK 发表于 2017-1-29 12:05:48
Using the Mutex construct
  1. In the Program.cs file, add the following using directives:
  2. using System;
  3. using System.Threading;
  4. using static System.Console;
  5. 3. Inside the Main method, add the following code snippet:
  6. const string MutexName = "CSharpThreadingCookbook";
  7. using (var m = new Mutex(false, MutexName))
  8. {
  9. if (!m.WaitOne(TimeSpan.FromSeconds(5), false))
  10. {
  11. WriteLine("Second instance is running!");
  12. }
  13. else
  14. {
  15. WriteLine("Running!");
  16. ReadLine();
  17. m.ReleaseMutex();
  18. }
  19. }
  20. 4.Run the program.
复制代码

地板
ReneeBK 发表于 2017-1-29 12:07:44
Using the SemaphoreSlim construct
  1. 1. Start Visual Studio 2015. Create a new C# console application project.
  2. 2. In the Program.cs file, add the following using directives:
  3. using System;
  4. using System.Threading;
  5. using static System.Console;
  6. using static System.Threading.Thread;
  7. 3. Below the Main method, add the following code snippet:
  8. static SemaphoreSlim _semaphore = new SemaphoreSlim(4);
  9. static void AccessDatabase(string name, int seconds)
  10. {
  11. WriteLine($"{name} waits to access a database");
  12. _semaphore.Wait();
  13. WriteLine($"{name} was granted an access to a database");
  14. Sleep(TimeSpan.FromSeconds(seconds));
  15. WriteLine($"{name} is completed");
  16. _semaphore.Release();
  17. }
  18. 4. Inside the Main method, add the following code snippet:
  19. for (int i = 1; i <= 6; i++)
  20. {
  21. string threadName = "Thread " + i;
  22. int secondsToWait = 2 + 2 * i;
  23. var t = new Thread(() => AccessDatabase(threadName,
  24. secondsToWait));
  25. t.Start();
  26. }
  27. 5. Run the program
复制代码

7
ReneeBK 发表于 2017-1-29 12:08:00

Using the AutoResetEvent construct

  1. How to do it...
  2. To understand how to send notifications from one thread to another with the help of the
  3. AutoResetEvent construct, perform the following steps:
  4. 1. Start Visual Studio 2015. Create a new C# console application project.
  5. 2. In the Program.cs file, add the following using directives:
  6. using System;
  7. using System.Threading;
  8. using static System.Console;
  9. using static System.Threading.Thread;
  10. 3. Below the Main method, add the following code snippet:
  11. private static AutoResetEvent _workerEvent = new
  12. AutoResetEvent(false);
  13. private static AutoResetEvent _mainEvent = new
  14. AutoResetEvent(false);
  15. static void Process(int seconds)
  16. {
  17. WriteLine("Starting a long running work...");
  18. Sleep(TimeSpan.FromSeconds(seconds));
  19. WriteLine("Work is done!");
  20. _workerEvent.Set();
  21. WriteLine("Waiting for a main thread to complete its work");
  22. _mainEvent.WaitOne();
  23. WriteLine("Starting second operation...");
  24. Sleep(TimeSpan.FromSeconds(seconds));
  25. WriteLine("Work is done!");
  26. _workerEvent.Set();
  27. }
  28. 4. Inside the Main method, add the following code snippet:
  29. var t = new Thread(() => Process(10));
  30. t.Start();
  31. WriteLine("Waiting for another thread to complete work");
  32. _workerEvent.WaitOne();
  33. WriteLine("First operation is completed!");
  34. WriteLine("Performing an operation on a main thread");
  35. Sleep(TimeSpan.FromSeconds(5));
  36. _mainEvent.Set();
  37. WriteLine("Now running the second operation on a second thread");
  38. _workerEvent.WaitOne();
  39. WriteLine("Second operation is completed!");
  40. 5. Run the program.
复制代码

8
ReneeBK 发表于 2017-1-29 12:10:41
Using the ManualResetEventSlim construct
  1. How to do it...
  2. To understand the use of the ManualResetEventSlim construct, perform the following steps:
  3. 1. Start Visual Studio 2015. Create a new C# console application project.
  4. 2. In the Program.cs file, add the following using directives:
  5. using System;
  6. using System.Threading;
  7. using static System.Console;
  8. using static System.Threading.Thread;
  9. 3. Below the Main method, add the following code:
  10. static void TravelThroughGates(string threadName, int seconds)
  11. {
  12. WriteLine($"{threadName} falls to sleep");
  13. Sleep(TimeSpan.FromSeconds(seconds));
  14. WriteLine($"{threadName} waits for the gates to open!");
  15. _mainEvent.Wait();
  16. WriteLine($"{threadName} enters the gates!");
  17. }
  18. static ManualResetEventSlim _mainEvent = new
  19. ManualResetEventSlim(false);
  20. 4. Inside the Main method, add the following code:
  21. var t1 = new Thread(() => TravelThroughGates("Thread 1", 5));
  22. var t2 = new Thread(() => TravelThroughGates("Thread 2", 6));
  23. var t3 = new Thread(() => TravelThroughGates("Thread 3", 12));
  24. t1.Start();
  25. t2.Start();
  26. t3.Start();
  27. Sleep(TimeSpan.FromSeconds(6));
  28. WriteLine("The gates are now open!");
  29. _mainEvent.Set();
  30. Sleep(TimeSpan.FromSeconds(2));
  31. _mainEvent.Reset();
  32. WriteLine("The gates have been closed!");
  33. Sleep(TimeSpan.FromSeconds(10));
  34. WriteLine("The gates are now open for the second time!");
  35. _mainEvent.Set();
  36. Sleep(TimeSpan.FromSeconds(2));
  37. WriteLine("The gates have been closed!");
  38. _mainEvent.Reset();
  39. 5. Run the program.
复制代码

9
gbkuce 发表于 2017-1-29 12:33:39
心情不好很郁闷!

10
iid_garch 发表于 2017-1-30 07:07:41

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-17 08:07