Untitled
unknown
csharp
3 years ago
771 B
6
Indexable
using System;
using System.Threading;
class Program {
static int winner = 0;
static void Main(string[] args) {
Thread t1 = new Thread(RunRace);
Thread t2 = new Thread(RunRace);
t1.Start("Thread 1");
t2.Start("Thread 2");
t1.Join();
t2.Join();
Console.WriteLine("Winner: " + winner);
Console.ReadLine();
}
static void RunRace(object threadName) {
for (int i = 0; i < 10; i++) {
Console.WriteLine(threadName + " is at position " + i);
Thread.Sleep(100);
if (i == 9 && Interlocked.CompareExchange(ref winner, 1, 0) == 0) {
Console.WriteLine(threadName + " wins the race!");
}
}
}
}
Editor is loading...