您的位置:首页 > 编程语言 > C#

06.C#线程Thread

2016-01-07 19:47 411 查看
Program.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
using System.Threading;

namespace Test1
{
class Program
{
static void Main(string[] args)
{
/*
1.使用using System.Threading;
2.自己摸索,可以查看api文档。
*/

//1.相当于不带参数的委托delegate
Thread t1 = new Thread(new ThreadStart(Test1));
t1.Name = "线程1";
t1.Start();

//2.相当于带参数的委托delegate
Thread t2 = new Thread(new ParameterizedThreadStart(Test2));
t2.Name = "线程2";
t2.Priority = ThreadPriority.Highest;//设置线程的优先级(前提是在线程池中)
t2.IsBackground = true;//是否在后台执行
t2.Start("lai哈喽");

}

static void Test1() {
Thread.Sleep(2000);//线程睡眠2秒
Console.WriteLine("当前线程的Name:"+Thread.CurrentThread.Name);
}

static void Test2(object s1)
{
Thread.CurrentThread.Join(1000);//线程阻塞1秒
Console.WriteLine(s1+"当前线程的Name:" + Thread.CurrentThread.Name);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线程