CSharp examples for Thread Asynchronous:Synchronize
Synchronize Access to a Shared Data Value
using System;/*from w w w . java2s .c o m*/ using System.Threading; class MainClass { public static void Main() { int firstInt = 2500; int secondInt = 8000; Console.WriteLine("firstInt initial value = {0}", firstInt); Console.WriteLine("secondInt initial value = {0}", secondInt); Interlocked.Decrement(ref firstInt); Console.WriteLine("firstInt after decrement = {0}", firstInt); Interlocked.Increment(ref secondInt); Console.WriteLine("secondInt after increment = {0}", secondInt); Interlocked.Add(ref firstInt, secondInt); Console.WriteLine("firstInt after Add = {0}", firstInt); Console.WriteLine("secondInt after Add = {0}", secondInt); Interlocked.Exchange(ref secondInt, firstInt); Console.WriteLine("firstInt after Exchange = {0}", firstInt); Console.WriteLine("secondInt after Exchange = {0}", secondInt); Interlocked.CompareExchange(ref firstInt, 5000, secondInt); Console.WriteLine("firstInt after CompareExchange = {0}", firstInt); Console.WriteLine("secondInt after CompareExchange = {0}", secondInt); } }