class Program
{
public static object obj = new object();
public static int n = 10;
static void Main(string[] args)
{
Thread t1 = new Thread ( Sell){ Name = "一号窗口" };
Thread t2 = new Thread(Sell){ Name = "二号窗口" };
Thread t3 = new Thread(Sell){ Name = "三号窗口" };
t1.Start();
t2.Start();
t3.Start();
}
public static void Sell()
{
while (n > 0)
{
Thread.Sleep(1000);
lock (obj)
{
if (n > 0)
{
Console.WriteLine("{0}出售车票{1}", Thread.CurrentThread.Name, n--);
}
else
{
Console.WriteLine("{0}已关闭", Thread.CurrentThread.Name);
break;
}
}
}
}
}
转载于:https://www.cnblogs.com/gsj2ronger918/p/4537027.html