Thread C# Tutorial

using System;  
using System.Threading;  
 
class SharedRes { 
  public static int count = 0; 

 
class IncThread {  
  public Thread thrd; 
  
  public IncThread(string name) {  
    thrd = new Thread(this.run);  
    thrd.Name = name; 
    thrd.Start();  
  }  
  
  void run() {  
 
    for(int i=0; i<100; i++) { 
      Thread.Sleep(200); 
      Interlocked.Increment(ref SharedRes.count); 
      Console.WriteLine(thrd.Name + " count is " + SharedRes.count); 
    } 
  }  
}  
 
class DecThread {  
  public Thread thrd; 
  
  public DecThread(string name) {  
    thrd = new Thread(this.run);  
    thrd.Name = name; 
    thrd.Start();  
  }  
  
  void run() {  
 
    for(int i=0; i<100; i++) { 
      Thread.Sleep(500); 
      Interlocked.Decrement(ref SharedRes.count); 
      Console.WriteLine(thrd.Name + " count is " + SharedRes.count); 
    } 
  }  
}  
  
class InterdlockedDemo {  
  public static void Main() {  
 
    IncThread mt1 = new IncThread("Increment Thread");  
    DecThread mt2 = new DecThread("Decrement Thread");  
  
    mt1.thrd.Join(); 
    mt2.thrd.Join(); 
  }  
}
Increment Thread count is 1
Increment Thread count is 2
Decrement Thread count is 1
Increment Thread count is 2
Increment Thread count is 3
Decrement Thread count is 2
Increment Thread count is 3
Increment Thread count is 4
Increment Thread count is 5
Decrement Thread count is 4
Increment Thread count is 5
Increment Thread count is 6
Decrement Thread count is 5
Increment Thread count is 6
Increment Thread count is 7
Increment Thread count is 8
Decrement Thread count is 7
Increment Thread count is 8
Increment Thread count is 9
Decrement Thread count is 8
Increment Thread count is 9
Increment Thread count is 10
Increment Thread count is 11
Decrement Thread count is 10
Increment Thread count is 11
Increment Thread count is 12
Decrement Thread count is 11
Increment Thread count is 12
Increment Thread count is 13
Increment Thread count is 14
Decrement Thread count is 13
Increment Thread count is 14
Increment Thread count is 15
Decrement Thread count is 14
Increment Thread count is 15
Increment Thread count is 16
Increment Thread count is 17
Decrement Thread count is 16
Increment Thread count is 17
Increment Thread count is 18
Decrement Thread count is 17
Increment Thread count is 18
Increment Thread count is 19
Increment Thread count is 20
Decrement Thread count is 19
Increment Thread count is 20
Increment Thread count is 21
Decrement Thread count is 20
Increment Thread count is 21
Increment Thread count is 22
Decrement Thread count is 21
Increment Thread count is 22
Increment Thread count is 23
Increment Thread count is 24
Decrement Thread count is 23
Increment Thread count is 24
Increment Thread count is 25
Decrement Thread count is 24
Increment Thread count is 25
Increment Thread count is 26
Increment Thread count is 27
Decrement Thread count is 26
Increment Thread count is 27
Increment Thread count is 28
Decrement Thread count is 27
Increment Thread count is 28
Increment Thread count is 29
Increment Thread count is 30
Decrement Thread count is 29
Increment Thread count is 30
Increment Thread count is 31
Decrement Thread count is 30
Increment Thread count is 31
Increment Thread count is 32
Increment Thread count is 33
Decrement Thread count is 32
Increment Thread count is 33
Increment Thread count is 34
Decrement Thread count is 33
Increment Thread count is 34
Increment Thread count is 35
Increment Thread count is 36
Decrement Thread count is 35
Increment Thread count is 36
Increment Thread count is 37
Decrement Thread count is 36
Increment Thread count is 37
Increment Thread count is 38
Increment Thread count is 39
Decrement Thread count is 38
Increment Thread count is 39
Increment Thread count is 40
Decrement Thread count is 39
Increment Thread count is 40
Increment Thread count is 41
Decrement Thread count is 40
Increment Thread count is 41
Increment Thread count is 42
Increment Thread count is 43
Decrement Thread count is 42
Increment Thread count is 43
Increment Thread count is 44
Decrement Thread count is 43
Increment Thread count is 44
Increment Thread count is 45
Increment Thread count is 46
Decrement Thread count is 45
Increment Thread count is 46
Increment Thread count is 47
Decrement Thread count is 46
Increment Thread count is 47
Increment Thread count is 48
Increment Thread count is 49
Decrement Thread count is 48
Increment Thread count is 49
Increment Thread count is 50
Decrement Thread count is 49
Increment Thread count is 50
Increment Thread count is 51
Increment Thread count is 52
Decrement Thread count is 51
Increment Thread count is 52
^CTerminate batch job (Y/N)? n