多线程05-传参
class Program
{
static void Main()
{
var sample = new ThreadSample(10);
var threadOne = new Thread(sample.CountNumbers);
threadOne.Name = "ThreadOne";
threadOne.Start();
threadOne.Join();
Console.WriteLine("end threadOne");
var threadSecond = new Thread(Count);
threadSecond.Name = "threadSecond";
threadSecond.Start(10);
threadSecond.Join();
Console.WriteLine("end threadSecond");
var threadThree = new Thread(() => CountNumbers(10));
threadThree.Name = "threadThree";
threadThree.Start();
threadThree.Join();
Console.WriteLine("end threadThree");
}
static void Count(object iterations)
{
CountNumbers((int)iterations);
}
static void CountNumbers(int iterations)
{
for(int i=1;i<iterations;i++)
{
Thread.Sleep(TimeSpan.FromSeconds(0.5));
Console.WriteLine("Thread Name is {0},Prints={1}", Thread.CurrentThread.Name, i);
}
}
static void PrintNumbers(int number)
{
Console.WriteLine(number);
}
class ThreadSample
{
private readonly int _interations;
public ThreadSample(int interations)
{
_interations = interations;
}
public void CountNumbers()
{
for (int i = 1; i < _interations; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(0.5));
Console.WriteLine("Thread Name is {0},Prints={1}", Thread.CurrentThread.Name, i);
}
}
}
}
{
static void Main()
{
var sample = new ThreadSample(10);
var threadOne = new Thread(sample.CountNumbers);
threadOne.Name = "ThreadOne";
threadOne.Start();
threadOne.Join();
Console.WriteLine("end threadOne");
var threadSecond = new Thread(Count);
threadSecond.Name = "threadSecond";
threadSecond.Start(10);
threadSecond.Join();
Console.WriteLine("end threadSecond");
var threadThree = new Thread(() => CountNumbers(10));
threadThree.Name = "threadThree";
threadThree.Start();
threadThree.Join();
Console.WriteLine("end threadThree");
}
static void Count(object iterations)
{
CountNumbers((int)iterations);
}
static void CountNumbers(int iterations)
{
for(int i=1;i<iterations;i++)
{
Thread.Sleep(TimeSpan.FromSeconds(0.5));
Console.WriteLine("Thread Name is {0},Prints={1}", Thread.CurrentThread.Name, i);
}
}
static void PrintNumbers(int number)
{
Console.WriteLine(number);
}
class ThreadSample
{
private readonly int _interations;
public ThreadSample(int interations)
{
_interations = interations;
}
public void CountNumbers()
{
for (int i = 1; i < _interations; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(0.5));
Console.WriteLine("Thread Name is {0},Prints={1}", Thread.CurrentThread.Name, i);
}
}
}
}