110,533
社区成员
发帖
与我相关
我的任务
分享
static object obj = new object();
static int num = 0;
static void Main(string[] args)
{
Program p = new Program();
string strTemp = "第一,第二,第三,第四,第五,第六";
string[] ArrayTemp = strTemp.Split(new string[] { "," }, StringSplitOptions.None);
num = ArrayTemp.Length;
Thread thread1 = new Thread(new ThreadStart(p.GetNum));
thread1.Name = "第一线程";
thread1.Priority = ThreadPriority.Highest;
Thread thread2 = new Thread(new ThreadStart(p.GetNum));
thread2.Name = "第二线程";
thread2.Priority = ThreadPriority.Lowest;
thread1.Start();
thread2.Start();
Console.ReadKey();
}
public void GetNum()
{
//lock (obj)
//{
string strTemp = "第一,第二,第三,第四,第五,第六";
string[] ArrayTemp = strTemp.Split(new string[] { "," }, StringSplitOptions.None);
while (true)
{
if (num >= 1)
{
lock (this)
{
Console.WriteLine(Thread.CurrentThread.Name + ArrayTemp[num - 1].ToString());
Thread.Sleep(2000);
num--;
if (num <= 1)
break;
}
}
}
//}
}