多线程(1-6 Thread)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadClass
{
class Program
{
static void Main(string[] args)
{
#region 控制线程
{
}
#endregion
#region Thread类创建线程
Thread th = new Thread(newThread);
th.Start();
Thread th2 = new Thread(Thread_param);
th2.Start(50);
MyThread<string> myThread = new MyThread<string>("Thread_child");
Thread th3 = new Thread(myThread.ThreadChild);
th3.Start();
Console.WriteLine("This is main thread");
Console.Read();
运行结果:
#endregion
#region 后台线程
#endregion
}
static void newThread()
{
Console.WriteLine("this is child thread");
Console.WriteLine("newThread State!");
Thread.Sleep(1000);
Console.WriteLine("newThread Complete!");
}
static void Thread_param(object msg)
{
Console.WriteLine("this is child thread");
int message = (int)msg;
Console.WriteLine("Result:{0}",message);
}
}
class MyThread<T>
{
private T data;
public MyThread(T data)
{
this.data = data;
}
public void ThreadChild()
{
Console.WriteLine("this is child thread");
Console.WriteLine("Child Thread Start! Result:{0}",data);
}
}
}
图一:
|