用c#小程序理解线程
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void DisplayNumbers()
{
string _threadName;
string _cultrue;
System.Threading.Thread myThread = System.Threading.Thread.CurrentThread;
_threadName = " "+ myThread.Name;
_cultrue = " " + myThread.CurrentCulture.ToString();
for (int i = 1; i <= 8 * interval; i++)
{
if (i % interval == 0)
{
Console.WriteLine(_threadName +_cultrue+ "Name have reached:" + i);
}
}
}
static void startMethod()
{
DisplayNumbers();
Console.WriteLine("It's Finished;");
}
static int interval;
static void Main(string[] args)
{
Console.WriteLine("Please input the number:");
interval = int.Parse(Console.ReadLine());
Thread myThread = Thread.CurrentThread;
myThread.Name = "mainThread";
ThreadStart startWork = new ThreadStart(startMethod);
Thread workThead = new Thread(startWork);
workThead.Name = "WorkThread";
workThead.Start();
DisplayNumbers();
Console.WriteLine("It's Finished;");
Console.ReadLine();
}
}
}
输出结果: