rt, C# codes as below:
using System;
using System.Threading;
namespace MConsoleApp
{
class Program
{
static void Main(string[] args)
{
PrintHelper.MaxCycle = 30;
Thread thread1 = new Thread(new ThreadStart(PrintHelper.Print)) { Name = "A" };
Thread thread2 = new Thread(new ThreadStart(PrintHelper.Print)) { Name = "B" };
Thread thread3 = new Thread(new ThreadStart(PrintHelper.Print)) { Name = "C" };
thread1.Start();
thread2.Start();
thread3.Start();
Console.ReadKey();
}
}
class PrintHelper
{
/// <summary>
/// Help current thread keep in touch with other threads.
/// </summary>
private static int counter;
/// <summary>
/// Define the exit of method cycle.
/// </summary>
public static int MaxCycle { get; set; }
public static void Print()
{
while (counter < MaxCycle)
{
string name = Thread.CurrentThread.Name;
if ((counter % 3 == 0 && name == "A") || (counter % 3 == 1 && name == "B") || (counter % 3 == 2 && name == "C"))
{
lock ("lock this Code!")
{
if (counter < MaxCycle)
{
Console.Write(name);
counter++;
}
}
}
}
}
}
}