线程发起方式
委托方式发起
static void myEx1_StartThread()
{
//1、无返回值的线程
Action<int, string> a = Test;
//开启一个新的线程去执行a所引用的方法
a.BeginInvoke(100, "asdf", null, null);
//可以认为main线程和test线程是同时执行的,也叫做异步执行
//2、有返回值的线程,并得到返回值
Func<int, string, int> b = Test1;
b.BeginInvoke(100, "asdf", null, null);//IAsyncResult取得当前线程的状态
Console.WriteLine("main");
}
通过Thread类发起线程
定义需要线程执行的方法
static void DownloadFile()//不带参数
{
Console.WriteLine("开始下载" + Thread.CurrentThread.ManagedThreadId);//获取线程ID
Thread.Sleep(2000);
Console.WriteLine("下载完成");
}
static void DownloadFileWithName(object fileName)//可以传递任何类型
{
Console.WriteLine("开始下载" + fileName);
Thread.Sleep(2000);
Console.WriteLine(fileName+"下载完成");
}
直接使用系统Thread类
//线程执行不带参数的方法
static void MyEx1_StartWithClassThread()
{
Thread t = new Thread(DownloadFile);//创建对象,没有启动
t.Start();
}
//Lambda方法
static void MyEx2_Lambda()
{
Thread t = new Thread(() =>
{
Console.WriteLine("开始下载" + Thread.CurrentThread.ManagedThreadId);//获取线程ID
Thread.Sleep(2000);
Console.WriteLine("下载完成");
});
t.Start();
}
//传递带参数的方法
static void MyEx3_WithPara()
{
Thread t = new Thread(DownloadFileWithName);
t.Start("xxx.mva");
}
使用自定义类传递参数
//定义类
class MyThread
{
private string filename;
private string filepath;
public MyThread(string filename, string filepath)
{
this.filename = filename;
this.filepath = filepath;
}
public void DownLoadFile()
{
Console.WriteLine("开始下载" + filepath + filename);
Thread.Sleep(2000);
Console.WriteLine("下载完成");
}
}
//实现
static void MyEx4_WithParaUseClass()
{
MyThread my = new MyThread("xxx.bt", "http://www.xxx.bbs");
Thread t = new Thread(my.DownLoadFile);//构造Thread对象的时候,可以传递静态方法,也可以传递对象的普通方法
t.Start();
}
线程池启动线程
class Program
{
static void ThreadMethod()
{
Console.WriteLine("线程开始");
Thread.Sleep(2000);
Console.WriteLine("线程结束");
}
static void ThreadMethodWithPara(object para)
{
Console.WriteLine("线程开始" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(2000);
Console.WriteLine("线程结束");
}
/// <summary>
/// 使用线程池调用方法,必须带有参数
/// </summary>
static void MyEx1()
{
//ThreadPool.QueueUserWorkItem(ThreadMethod);
ThreadPool.QueueUserWorkItem(ThreadMethodWithPara);
ThreadPool.QueueUserWorkItem(ThreadMethodWithPara);
ThreadPool.QueueUserWorkItem(ThreadMethodWithPara);
ThreadPool.QueueUserWorkItem(ThreadMethodWithPara);
ThreadPool.QueueUserWorkItem(ThreadMethodWithPara);
ThreadPool.QueueUserWorkItem(ThreadMethodWithPara);
ThreadPool.QueueUserWorkItem(ThreadMethodWithPara);
}
static void Main(string[] args)
{
MyEx1();
Console.ReadKey();
}
}
通过任务开启线程
class Program
{
static void ThreadMethod()
{
Console.WriteLine("任务开始");
Thread.Sleep(2000);
Console.WriteLine("任务结束");
}
/// <summary>
/// 使用Task对象启动
/// </summary>
static void MyEx1()
{
Task t = new Task(ThreadMethod);//传递一个需要线程执行的方法
t.Start();
}
/// <summary>
/// 任务工程:TaskFactory
/// </summary>
static void MyEx2()
{
TaskFactory tf = new TaskFactory();
Task t = tf.StartNew(ThreadMethod);
}
static void Main(string[] args)
{
MyEx2();
Console.WriteLine("main");
Console.ReadKey();
}
}
线程状态的检测
IAsyncResult对象
static void myEx1_StartThread()
{
//1、无返回值的线程
Action<int, string> a = Test;
//开启一个新的线程去执行a所引用的方法
a.BeginInvoke(100, "asdf", null, null);
//可以认为main线程和test线程是同时执行的,也叫做异步执行
//2、有返回值的线程,并得到返回值
Func<int, string, int> b = Test1;
IAsyncResult ar = b.BeginInvoke(100, "asdf", null, null);//IAsyncResult取得当前线程的状态
while (ar.IsCompleted == false)//当前线程没有执行完毕
{
Console.Write(".");
//Thread.Sleep(10);//控制子线程的检测频率
}
int res = b.EndInvoke(ar);//取得线程的返回值
Console.WriteLine(res);
Console.WriteLine("main");
}
IAsyncResult.WaitOne()
后面参数表示等待时间,如果等待时间内结束,返回true,否则返回false
static void myEx2_TestThreadEnd()
{
Func<int, string, int> b = Test1;
IAsyncResult ar = b.BeginInvoke(100, "asdf", null, null);//IAsyncResult取得当前线程的状态
//1000毫秒表示超时时间,如果超过了1000未返回,则返回false,否则返回true
bool isEnd = ar.AsyncWaitHandle.WaitOne(1000);//等1000毫秒
if(isEnd)
{
int res = b.EndInvoke(ar);
Console.WriteLine(res);
}
}
异步回调
即当线程结束的时候,调用某个函数
static void doAfterThread(IAsyncResult ar)//回调函数,参数固定
{
Func<int, string, int> a = ar.AsyncState as Func<int, string, int>;
int res = a.EndInvoke(ar);
Console.WriteLine("res= " + res);
}
static void myEx3()
{
Func<int, string, int> b = Test1;
//第三个参数是个委托,传递一个方法,当线程结束的时候调用
//第四个参数用来给回调函数传递数据
IAsyncResult ar = b.BeginInvoke(100, "asdf", doAfterThread, b);
b.BeginInvoke(100, "asdf", br =>
{
int res = b.EndInvoke(br);
Console.WriteLine(res + "在lambda表达式中获得");
}, null);
}
异步回调,lambda写法
static void myEx4_Lambda()
{
Func<int, string, int> b = Test1;
b.BeginInvoke(100, "asdf", br =>
{
int res = b.EndInvoke(br);
Console.WriteLine(res + "在lambda表达式中获得");
}, null);
}
前台进程和后台进程
一、只有一个前台线程在运行,应用程序的进程就在运行
二、多个前台线程在运行,但Main方法结束了,应用程序的进程仍是运行的,直到所有的前台线程完成其任务为止。
三、在默认情况下,用Thread类创建的线程是前台线程。线程池中的线程总是后台线程。
class Program
{
static void DownloadFile()
{
Console.WriteLine("开始下载" + Thread.CurrentThread.ManagedThreadId);//获取线程ID
Thread.Sleep(2000);
Console.WriteLine("下载完成");
}
/// <summary>
/// 在main中,没有写ReadKey(),默认应该是一闪即过
/// 但是因为Thread是前台线程,现在要等到MyEx1线程结束了之后,才会真正结束
/// </summary>
static void MyEx1()
{
Thread t = new Thread(DownloadFile);
t.Start();
}
/// <summary>
/// 使用IsBackground设置为后台线程
/// 设置为后台线程,那么Main线程结束了之后,不用等待MyEx2结束,直接结束,一闪即过的效果
/// </summary>
static void MyEx2()
{
Thread t = new Thread(DownloadFile);
t.IsBackground = true;
t.Start();
}
static void Main(string[] args)
{
}
}
任务:连续任务
使用ContinueWith()
class Program
{
static void doFirst()
{
Console.WriteLine("do in task:" + Task.CurrentId);
Thread.Sleep(3000);
}
static void doSecont(Task t)
{
Console.WriteLine("tast" + t.Id + "finished");
Console.WriteLine("this task id is " + Task.CurrentId);
Thread.Sleep(3000);
}
static void MyEx1()
{
Task t1 = new Task(doFirst);
Task t2 = t1.ContinueWith(doSecont);
Task t3 = t1.ContinueWith(doSecont);
Task t4 = t2.ContinueWith(doSecont);
t1.Start();
}
static void Main(string[] args)
{
MyEx1();
Console.ReadKey();
}
}
线程争用和死锁问题
class Program
{
static void ChangeState(object o)
{
MyThreadObject my = o as MyThreadObject;//把传递过来的传输转换类型
while(true)
{
lock (my)//向系统申请加锁,锁定my对象,如果my对象锁定,那么语句会暂停,直到申请到my对象
{
my.ChangeState();//在同一时刻只有一个线程在执行这个方法
}//释放对my的锁定
}
}
static void MyEx1()//控制台不会有任何反映
{
MyThreadObject m = new MyThreadObject();
Thread t = new Thread(ChangeState);
t.Start(m);
}
static void MyEx2()//这个时候会进行输出,因为两个线程同时修改同一个内存区域
{
MyThreadObject m = new MyThreadObject();
Thread t = new Thread(ChangeState);
t.Start(m);
new Thread(ChangeState).Start(m);
}
static void Main(string[] args)
{
MyEx2();
Console.ReadKey();
}
}
死锁问题最好是在软件设计的时候就对需要锁的顺序进行设计好,防止出现死锁现象。