Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. class ThreadTest
  2. {
  3. //Index i is declared as static so that both the threads have only one copy
  4. static int i;
  5.  
  6.  
  7. static void Main(string[] args)
  8. {
  9. Thread t = new Thread(WriteY);
  10. i = 0;
  11.  
  12. //Start thread Y
  13. t.Start();
  14. //Do something on the main thread.
  15. for (; i < 100; i++)
  16. {
  17. if (i % 10 == 0)
  18. {
  19. //Simulate Yield() function
  20. Thread.Sleep(0);
  21. Console.WriteLine("The X thread");
  22. }
  23. Console.Write(i + ":X ");
  24. }
  25. Console.ReadKey(true);
  26. }
  27.  
  28. static void WriteY()
  29. {
  30. for (; i < 100; i++)
  31. {
  32. if (i % 10 == 0)
  33. {
  34. //Simulate Yield() function
  35. Thread.Sleep(0);
  36. Console.WriteLine("The Y thread");
  37. }
  38. Console.Write(i + ":Y ");
  39. }
  40. }
  41. }
  42.  
  43. class Test
  44. {
  45. //Index i is declared as static so that both the threads have only one copy
  46. static int i;
  47.  
  48. static AutoResetEvent parentEvent = new AutoResetEvent(true);
  49. static AutoResetEvent childEvent = new AutoResetEvent(false);
  50.  
  51. static void Main(string[] args)
  52. {
  53. Thread t = new Thread(WriteY);
  54.  
  55. i = 0;
  56.  
  57. //Start thread Y
  58. t.Start();
  59. // Print X on the main thread
  60. parentEvent.WaitOne();
  61. while (i < 100)
  62. {
  63. if (i % 10 == 0)
  64. {
  65. childEvent.Set();
  66. parentEvent.WaitOne();
  67. }
  68. Console.Write(i + ":Y ");
  69. i++;
  70. }
  71. t.Join();
  72. }
  73.  
  74. static void WriteY()
  75. {
  76. childEvent.WaitOne();
  77. while (i < 100)
  78. {
  79. if (i % 10 == 0)
  80. {
  81. parentEvent.Set();
  82. childEvent.WaitOne();
  83. }
  84. Console.Write(i + ":X ");
  85. i++;
  86. }
  87. }
  88. }
  89.  
  90. using System.Threading;
  91. using System;
  92. class ThreadTest
  93. {
  94. //Index i is declared as static so that both the threads have only one copy
  95. static int i;
  96.  
  97. //The lock object
  98. static readonly object locker = new object();
  99.  
  100. static void Main(string[] args)
  101. {
  102. Thread t = new Thread(WriteY);
  103.  
  104. i = 0;
  105.  
  106. //Start thread Y
  107. t.Start();
  108. lock (locker)
  109. {
  110. // Print X on the main thread
  111. for (; i < 100; i++)
  112. {
  113. if (i % 10 == 0)
  114. {
  115. Monitor.PulseAll(locker); // move any "waiting" threads to the "ready" queue
  116. Monitor.Wait(locker); // relinquish the lock, and wait for a pulse
  117. Console.WriteLine("The X thread");
  118. }
  119. Console.Write(i + ":X ");
  120. }
  121. Monitor.PulseAll(locker);
  122. }
  123. Console.ReadKey(true);
  124. }
  125.  
  126. static void WriteY()
  127. {
  128. lock (locker)
  129. {
  130. for (; i < 100; i++)
  131. {
  132. if (i % 10 == 0)
  133. {
  134. Monitor.PulseAll(locker); // move any "waiting" threads to the "ready" queue
  135. Monitor.Wait(locker); // relinquish the lock, and wait for a pulse
  136. Console.WriteLine("The Y thread");
  137. }
  138. Console.Write(i + ":Y ");
  139. }
  140. Monitor.PulseAll(locker); // move any "waiting" threads to the "ready" queue
  141. }
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement