线程启动顺序的问题
我我初学 写了个线程测试的程序如下:
using System;
using System.Threading;
namespace ConsoleApplication1
{
class ThreadTest
{
private char theChar;
private int theInterval;
public ThreadTest(char c,int i)
{
theChar = c;
theInterval = i;
}
public void Show()
{
Console.WriteLine(theChar+ "start! ");
for(int i = 0;i <5;i++)
{
Console.WriteLine(theChar+ ": "+i+ "---------------- ");
if(theInterval > 0)
Thread.Sleep(theInterval);
}
Console.WriteLine(theChar + "end! ");
}
}
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
ThreadTest tc1 = new ThreadTest( 'A ',20);
ThreadTest tc2 = new ThreadTest( 'B ',10);
ThreadTest tc3 = new ThreadTest( 'C ',100);
Thread t1 = new Thread(new ThreadStart(tc1.Show));
Thread t2 = new Thread(new ThreadStart(tc2.Show));
Thread t3 = new Thread(new ThreadStart(tc3.Show));
t1.Start();
t2.Start();
t3.Start();
}
}
}
我想问下 按理说我先启动A线程结果因该是:
Astart!
A:0----------------
Bstart!
B:0----------------
Cstart!
C:0----------------
B:1----------------
A:1----------------
......
但是多执行几次后出现:
Cstart!
C:0----------------
Bstart!
B:0----------------
Astart!
A:0----------------
B:1----------------
A:1----------------
.......
C先启动了
这个线程启动的顺序是怎么回事啊?
------解决方案--------------------线程调度的顺序是不可预料的,因此出现什么样的顺序都是可能的