Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace MultiThread
- {
- class Program
- {
- private static string outputText = "";
- private static Boolean stopThread = false;
- private static Mutex MutexKu = new Mutex(); //buat mutex tanpa nama
- public static void DisplayThread1()
- {
- while (stopThread != true)
- {
- MutexKu.WaitOne();//nunggu dulu untuk thread lain
- Console.WriteLine("\nDisplay Thread 1");
- outputText = "Saya Thread 1";
- Thread.Sleep(1000);
- Console.WriteLine("Thread 1 output --> {0}", outputText);
- MutexKu.ReleaseMutex(); //release mutex
- }
- }
- public static void DisplayThread2()
- {
- while (stopThread != true)
- {
- MutexKu.WaitOne();//nunggu dulu untuk thread lain
- Console.WriteLine("\nDisplay Thread 2");
- outputText = "Saya Thread 2";
- Thread.Sleep(1000);
- Console.WriteLine("Thread 2 output --> {0}", outputText);
- MutexKu.ReleaseMutex(); //release mutex
- }
- }
- static void Main(string[] args)
- {
- Thread thread1 = new Thread(DisplayThread1);
- Thread thread2 = new Thread(DisplayThread2);
- // start them
- thread1.Start();
- thread2.Start();
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement