OKIEWARDOYO

C# No.42 1

Jun 30th, 2014
2,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace MultiThreadingProblem
  8. {
  9.     class Program
  10.     {
  11.         private static string outputText = "";
  12.         private static Boolean stopThread = false;
  13.         static void DisplayThread1()
  14.         {
  15.             while (stopThread != true)
  16.             {
  17.                 Console.WriteLine("\nDisplay Thread 1");
  18.                 outputText = "Saya Thread 1";
  19.                 Thread.Sleep(1000);
  20.                 Console.WriteLine("Thread 1 output --> {0}", outputText);
  21.             }
  22.         }
  23.  
  24.         static void DisplayThread2()
  25.         {
  26.             while (stopThread != true)
  27.             {
  28.                 Console.WriteLine("\nDisplay Thread 2");
  29.                 outputText = "Saya Thread 2";
  30.                 Thread.Sleep(1000);
  31.                 Console.WriteLine("Thread 2 output --> {0}", outputText);
  32.             }
  33.         }
  34.         static void Main(string[] args)
  35.         {
  36.             Thread thread1 = new Thread(DisplayThread1);
  37.             Thread thread2 = new Thread(DisplayThread2);
  38.  
  39.             // start them
  40.             thread1.Start();
  41.             thread2.Start();
  42.  
  43.             Console.ReadLine();
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment