入门级问题,麻烦大家了~~~!
using System;
using System.Threading;
namespace ConsoleApplication2
{
class Class1
{
static int Interval;
static void Main(string[] args)
{
Console.Write("Interval to display results at?");
Interval = int.Parse(Console.ReadLine());
//主线程
Thread ThisThread = Thread.CurrentThread;
ThisThread.Name = "主线程";
//子线程
ThreadStart WorkerStart = new ThreadStart(StartMethod);
Thread WorkerThread = new Thread(WorkerStart);
WorkerThread.Name = "子线程";
// WorkerThread.Priority = ThreadPriority.AboveNormal;
//子线程准备执行
WorkerThread.Start();
//主线程执行
DisplayNumbers();
Console.WriteLine("主线程结束");
Console.WriteLine();
}
static void StartMethod()
{
DisplayNumbers();
Console.WriteLine("子线程结束");
}
static void DisplayNumbers()
{
Thread ThisThread = Thread.CurrentThread;
string Name = ThisThread.Name;
Console.WriteLine(Name+"线程开始");
for (int i=1;i<=8*Interval;i++)
{
if (i%Interval==0)
{
Console.WriteLine(Name+":数字达到"+i);
}
}
}
}
}
问题1:当Interval数值比较小的时候,为什么两个线程执行顺序不确定?比如Interval等于1000
问题2:当interval等于10000000时,为什么会接近于并发执行?
问题3:(请看红色部分)如果此行位于兰色之前,即使设置子线程的优先级高,却仍然是
主线程先运行完(未结束),然后才是子线程运行,而最后却是子线程先结束,这又是为什么
? Interval等于10000000