码迷,mamicode.com
首页 > 编程语言 > 详细

(线程)三个窗口随机销售火车票

时间:2015-05-28 23:06:27      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

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;
}
}
}
}
}

(线程)三个窗口随机销售火车票

标签:

原文地址:http://www.cnblogs.com/gsj2ronger918/p/4537027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!