目录
多线程常用函数:
一、Thread -- .Net 1.0
二、ThreadPool -- .Net 2.0
三、Task -- .Net 3.0 -- 推荐
一、Task创建
二、Task.Run 创建
三、Parallel使用
四、Task 其他函数
四、TaskFactory -- .Net 4.0
一、TaskFactory创建
二、Task.Factory.StartNew 创建
三、TaskFactory其他函数
五、lock 、Async、Await
一、lock
二、Async、Await
涉及知识点: Thread、ThreadPool 、Task、TaskFactory
Thread.CurrentThread.ManagedThreadId --- 当前线程id
Thread.CurrentThread.IsThreadPoolThread --- 当前线程是否使用线程池
优点:API特别丰富
缺陷:
1、线程资源是直接从操作系统中获取,在线程挂起时,存在延时情况,造成任务执行顺序混乱
2、线程个数没有限制,对于新手容易造成死机状态
常用函数:
Thread thread = new Thread(threadStart);
thread.Start(); //线程开启
thread.Suspend(); //线程挂起
thread.Resume(); //挂起线程恢复
thread.Join(); //线程阻塞,直到线程完成后,执行下一步
thread.IsBackground = true; //设置为后台线程
thread.Abort();//线程摧毁
优点:线程池会根据限定的数量去申请和释放
若空闲线程太少时,会自动向操作系统申请线程
若空闲线程太多时,会自动释放线程
缺陷:
API 特别少 -- 造成很难控制线程执行顺序
例程:
WaitCallback waitCallback = (x) =>
{
Thread.Sleep(3000);
Console.WriteLine("Thread End");
};
ThreadPool.QueueUserWorkItem(waitCallback);
Console.WriteLine("完成");
优点:1、线程池会根据限定的数量去申请和释放
若空闲线程太少时,会自动向操作系统申请线程
若空闲线程太多时,会自动释放线程
2、 API丰富,便于控制
前期准备:
4个函数:1个无参、1个参数、2个参数、2参数+返回值
public static void DoSomeThing()
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End");
}
public static void DoSomeThing_1(string name)
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End " + name);
}
public static void DoSomeThing_2(string name1, string name2)
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End " + name1 + name2);
}
public static string DoSomeThing_2_1(string name1, string name2)
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End " + name1 + name2);
return name1;
}
1、无参
Task task = new Task(DoSomeThing);
task.Start();
2、一个参数
Task task = new Task((x)=>DoSomeThing_1((string)x),"XXXXXX");
task.Start();
3、一个参数
Task task = new Task(() => DoSomeThing_1("XXXXX"));
task.Start();
4、两个参数
Task task = new Task(() => DoSomeThing_2("XXXXX_1", "XXXXX_2"));
task.Start();
5、两个参数+返回值
Task task = new Task(() => DoSomeThing_2_1("XXXXX_1", "XXXXX_2"));
task.Start();
Console.WriteLine(task.Result);
1、无参
Task.Run(DoSomeThing);
2、一个参数
Task.Run(()=>DoSomeThing_1("XXXXX"));
3、两个参数+返回值
Task result = Task.Run(() => DoSomeThing_2_1("mask1", "mask2"));
Console.WriteLine(result.Result);
Parallel 可以启动多线程,主线程也参与计算,节约一个线程
可以通过ParallelOptions轻松控制最大并发数量
缺点:主线程卡住
例程:
Parallel.Invoke(
() =>
{
Console.WriteLine("Thread Start1");
Thread.Sleep(3000);
Console.WriteLine("Thread End1");
},
() =>
{
Console.WriteLine("Thread Start2");
Thread.Sleep(3000);
Console.WriteLine("Thread End2");
}
);
Console.WriteLine("完成");
Task.WaitAny(tasks.ToArray()); //任意一个完成,继续
Task.WaitAll(tasks.ToArray()); // 全部完成,继续
例程:
List tasks = new List();
tasks.Add(Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("task1"); }));
tasks.Add(Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("task2"); }));
tasks.Add(Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("task3"); }));
tasks.Add(Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("task4"); }));
Task.WaitAny(tasks.ToArray()); //任一一个完成,继续 --
Console.WriteLine("XXX第一个完成");
Task.WaitAll(tasks.ToArray()); // 全部完成,继续
Console.WriteLine("完成");
优点:1、线程池会根据限定的数量去申请和释放
若空闲线程太少时,会自动向操作系统申请线程
若空闲线程太多时,会自动释放线程
2、 API丰富,便于控制
前期准备:
4个函数:1个无参、1个参数、2个参数、2参数+返回值
public static void DoSomeThing()
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End");
}
public static void DoSomeThing_1(string name)
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End " + name);
}
public static void DoSomeThing_2(string name1, string name2)
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End " + name1 + name2);
}
public static string DoSomeThing_2_1(string name1, string name2)
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End " + name1 + name2);
return name1;
}
1、无参
TaskFactory taskFactory = new TaskFactory();
taskFactory.StartNew(DoSomeThing);
2、一个参数
TaskFactory taskFactory = new TaskFactory();
taskFactory.StartNew((x)=>DoSomeThing_1((string)x), "XXXXX");
3、两个参数
TaskFactory taskFactory = new TaskFactory();
taskFactory.StartNew(() => DoSomeThing_2("XXXXX_1", "XXXXX_2"));
4、两个参数+返回值
TaskFactory taskFactory = new TaskFactory();
Task result = taskFactory.StartNew(() => DoSomeThing_2_1("XXXXX_1", "XXXXX_2"));
Console.WriteLine(result.Result);
1、无参
Task.Factory.StartNew(DoSomeThing);
2、一个参数
Task.Factory.StartNew((x) => DoSomeThing_1((string)x), "XXXXX");
3、两个参数
Task.Factory.StartNew(() => DoSomeThing_2("XXXXX_1", "XXXXX_2"));
4、两个参数+返回值
Task result = Task.Factory.StartNew(() => DoSomeThing_2_1("XXXX_1", "XXXX_2"));
Console.WriteLine(result.Result);
taskFactory.ContinueWhenAny //任意一个完成,继续
taskFactory.ContinueWhenAll // 全部完成,继续
List tasks = new List();
tasks.Add(Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("task1"); }));
tasks.Add(Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("task2"); }));
tasks.Add(Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("task3"); }));
tasks.Add(Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("task4"); }));
TaskFactory taskFactory = new TaskFactory();
taskFactory.ContinueWhenAny(tasks.ToArray(),
(x) =>
{ Console.WriteLine("初步完成"); });
taskFactory.ContinueWhenAll(tasks.ToArray(),
(x) =>
{ Console.WriteLine("全部完成"); });
锁定的内容,多个线程同时访问时,需要排队进行
lock的标志,最好不用混用,一个标志配一把锁
class Program
{
public static void Main()
{
List tasks = new List();
tasks.Add(Task.Run(DoSomeThing));
tasks.Add(Task.Run(DoSomeThing));
Task.Run(tasks.ToArray);
Console.WriteLine("----");
}
//锁标志位创建
private static readonly Object LOCK = new Object();
public static void DoSomeThing()
{
//锁内容设置
lock (LOCK)
{
Console.WriteLine("Start");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method--{i}");
}
Console.WriteLine("End");
}
}
}
Async 只能修饰 void、Task、Task
Await 只能修饰 Task,用于等待任务完成
private void button2_Click(object sender, EventArgs e)
{
Task.Run(DoSomeThing);
Task.Run(DoSomeThing_1);
Console.WriteLine("------");
}
public async void DoSomeThing()
{
Console.WriteLine("---Start---");
await Task.Delay(1000);
for(int i = 0;i< 10;i++)
{
Console.WriteLine($"--{i}--");
}
Console.WriteLine("---End---");
}
public async void DoSomeThing_1()
{
Console.WriteLine("---Start---");
await Task.Delay(1000);
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"--Method_2--{i}--");
}
Console.WriteLine("---End---");
}
如有错误,烦请批评指正