
正文
C# 线程参数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
、
class ThreadSample
{
private readonly int _iterations;
public ThreadSample(int iterations)
{
_iterations = iterations;
}
public void CountNumbers()
{
for (int i = ; i <= _iterations; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(0.5));
Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
}
}
}
class Program
{
static void Main(string[] args)
{
var sample = new ThreadSample(); var threadOne = new Thread(sample.CountNumbers);
threadOne.Name = "ThreadOne";
threadOne.Start();
threadOne.Join(); Console.WriteLine("--------------------------"); var threadTwo = new Thread(Count);
threadTwo.Name = "ThreadTwo";
threadTwo.Start();
threadTwo.Join(); Console.WriteLine("--------------------------"); var threadThree = new Thread(() => CountNumbers());
threadThree.Name = "ThreadThree";
threadThree.Start();
threadThree.Join();
Console.WriteLine("--------------------------"); int i = ;
var threadFour = new Thread(() => PrintNumber(i));
i = ;
var threadFive = new Thread(() => PrintNumber(i));
threadFour.Start();
threadFive.Start();
Console.Read();
} static void Count(object iterations)
{
CountNumbers((int)iterations);
} static void CountNumbers(int iterations)
{
for (int i = ; i <= iterations; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(0.5));
Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
}
} static void PrintNumber(int number)
{
Console.WriteLine(number);
}
、







