(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Interlocked
using System;
using System.Threading;
class MainClass
{
public static void Main()
{
int firstInt = 1;
int secondInt = 2;
Console.WriteLine("firstInt initial value = {0}", firstInt);
Console.WriteLine("secondInt initial value = {0}", secondInt);
Interlocked.Decrement(ref firstInt);
Console.WriteLine(Environment.NewLine);
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(Environment.NewLine);
Console.WriteLine("firstInt after Add = {0}", firstInt);
Console.WriteLine("secondInt after Add = {0}", secondInt);
Interlocked.Exchange(ref secondInt, firstInt);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("firstInt after Exchange = {0}", firstInt);
Console.WriteLine("secondInt after Exchange = {0}", secondInt);
Interlocked.rupareExchange(ref firstInt, 5000, secondInt);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("firstInt after CompareExchange = {0}", firstInt);
Console.WriteLine("secondInt after CompareExchange = {0}", secondInt);
}
}
firstInt initial value = 1
secondInt initial value = 2
firstInt after decrement = 0
secondInt after increment = 3
firstInt after Add = 3
secondInt after Add = 3
firstInt after Exchange = 3
secondInt after Exchange = 3
firstInt after CompareExchange = 5000
secondInt after CompareExchange = 3
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
Use the Interlocked object
using System;
using System.Threading;
class MainClass
{
private int Runs = 0;
public void CountUp()
{
while (Runs < 10)
{
Interlocked.Increment(ref Runs);
Console.WriteLine(Thread.CurrentThread.Name + " " + Runs);
Thread.Sleep(1000);
}
}
public static void Main()
{
MainClass ex = new MainClass();
ex.RunThreads();
}
public void RunThreads()
{
Thread t2 = new Thread(new ThreadStart(CountUp));
t2.Name = "t2";
Thread t3 = new Thread(new ThreadStart(CountUp));
t3.Name = "t3";
t2.Start();
t3.Start();
}
}
t2 1
t3 2
t2 3
t3 4
t2 5
t3 6
t2 7
t3 8
t2 9
t3 10