Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Create a Project which proves that Synchronization is the mechanism of ensuring
- //that two threads don’t execute a specific portion of program at the same time
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- //Create a Project which proves that Synchronization is the mechanism of ensuring
- //that two threads don’t execute a specific portion of program at the same time
- namespace _1_C_Sharp_Exam_Preparations
- {
- public class Program
- {
- //Create a ThreadMethod
- public static void ThreadMethod()
- {
- //inside the thread method we have for loop
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("ThreadProc: {0}", i);
- //It is used to signal to Windows that this thread is finished
- Thread.Sleep(0);
- }
- }
- public static void Main(string[] args)
- {
- //System.Thread;
- //Create instance of Thread class and start it
- Thread th = new Thread(new ThreadStart(ThreadMethod));
- th.Start();
- //Here we have to give a loop with condition
- for (int i = 0; i < 4; i++)
- {
- Console.WriteLine("Main thread: Do some extra work.");
- //It is used to signal to Windows that this thread is finished
- Thread.Sleep(0);
- }
- //The Thread.Join method is called on the main thread to let it wait until the other thread finishes.
- th.Join();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement