Use Interlocked operations : Interlocked « Thread « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Thread » Interlocked 
20.14.2.Use Interlocked operations
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
20.14.Interlocked
20.14.1.Use the Interlocked object
20.14.2.Use Interlocked operations
20.14.3.Interlocked.Increment and Interlocked.Decrement
20.14.4.Interlocked
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.