Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. //Create a Project which proves that Synchronization is the mechanism of ensuring
  2. //that two threads don’t execute a specific portion of program at the same time
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8.  
  9. //Create a Project which proves that Synchronization is the mechanism of ensuring
  10. //that two threads don’t execute a specific portion of program at the same time
  11. namespace _1_C_Sharp_Exam_Preparations
  12. {
  13. public class Program
  14. {
  15. //Create a ThreadMethod
  16. public static void ThreadMethod()
  17. {
  18. //inside the thread method we have for loop
  19. for (int i = 0; i < 10; i++)
  20. {
  21. Console.WriteLine("ThreadProc: {0}", i);
  22. //It is used to signal to Windows that this thread is finished
  23. Thread.Sleep(0);
  24. }
  25. }
  26. public static void Main(string[] args)
  27. {
  28. //System.Thread;
  29. //Create instance of Thread class and start it
  30.  
  31. Thread th = new Thread(new ThreadStart(ThreadMethod));
  32. th.Start();
  33. //Here we have to give a loop with condition
  34. for (int i = 0; i < 4; i++)
  35. {
  36. Console.WriteLine("Main thread: Do some extra work.");
  37. //It is used to signal to Windows that this thread is finished
  38. Thread.Sleep(0);
  39. }
  40. //The Thread.Join method is called on the main thread to let it wait until the other thread finishes.
  41. th.Join();
  42.  
  43. }
  44.  
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement