class Program
{
static void Test()
{
Console.WriteLine("Start……");
Thread.Sleep(2000); // 1s等于1000ms,暂停2s
Console.WriteLine("end");
}
static void Download(Object ob)
{
string str = ob as string; // 遍历传递过来的ob字符串
Console.WriteLine(str); // http://www.c.com/xx/xx……
}
public struct Data // 定义结构体传递多个数据
{
public string name;
public int age;
}
static void UseData(Object o)
{
// Data da = o as Data; // Data不能为null
Data da = (Data)o;
Console.WriteLine(da.name + ":" + da.age);
}
static void Main(string[] args)
{
Thread t = new Thread(Test); // 将Test方法传递给线程
t.Start(); // 调用线程
//Start……
//end
Console.WriteLine("哈喽world"); // 线程的同步执行
//哈喽world
//Start……
//end
Thread d = new Thread(Download);
d.Start("http://www.c.com/xx/xx……"); // 将网址传递给d线程
Thread d2 = new Thread(UseData);
Data data = new Data(); // 结构体定义数据
data.name = "xiaoming";
data.age = 18;
d2.Start(data); // 将数据传递给线程d2
// xiaoming: 18
// Mian id:1
// 匿名方法的传递
Thread t2 = new Thread(() => Console.WriteLine("Child Thread:" + Thread.CurrentThread)); // 获取线程正在其中执行的当前上下文
// Child Thread:System.Threading.Thread
t2.Start();
Thread t3 = new Thread(() => Console.WriteLine("Child Thread:" + Thread.CurrentThread.ManagedThreadId)); // 获取当前托管线程的唯一标识符
// Child Thread:5
t3.Start();
Console.WriteLine("Mian id:" + Thread.CurrentThread.ManagedThreadId); // Mian id:1
}
}