C#基础–线程之Task
Task 是.NetFramework3.0出现的,Task里面的线程是来自于线程池
一、Task
1. 开启一个线程
Task 构造函数
传入一个无参数的 Action 委托作为参数
Task task = new Task(() =>
{
this.DoSomethingLong("btntask_Click1");
});
task.Start();
Run() 静态方法
//Run 为静态方法
Task.Run(() =>
{
this.DoSomethingLong("btntask_Click2");
});
TaskFactory 工厂
TaskFactory taskFactory = new TaskFactory();
taskFactory.StartNew(() =>
{
this.DoSomethingLong("btntask_Click3");
});
TaskFactory taskFactory = Task.Factory;
taskFactory.StartNew(() =>
{
this.DoSomethingLong("btntask_Click3");
});
Task.Delay 延迟执行
Task.Delay 出现于4.5版本
Thread.Sleep() 是阻塞的,而 Delay 是非阻塞的;如果Sleep 和Delay 平级,则会因Sleep造成阻塞(卡界面)
ContinueWith 类似于回调
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//任务在2000ms=2s 以后执行
Task task = Task.Delay(2000).ContinueWith(t =>
{
Console.WriteLine($"之前花费时间:{stopwatch.ElapsedMilliseconds}");
Console.WriteLine("回调已完成");
Thread.Sleep(2000);
Console.WriteLine($"之后花费时间:{stopwatch.ElapsedMilliseconds}");
stopwatch.Stop();
});
2. 等待任务
2.1 WaitAny
阻塞式,当前线程会等待任意任务结束,然后才能执行后续语句
List<Task> tasksList = new List<Task>();
TaskFactory taskFactory = new TaskFactory();
tasksList.Add(taskFactory.StartNew(() => { this.Coding("杰克", "系统管理"); }));
tasksList.Add(taskFactory.StartNew(() => { this.Coding("流光易逝", "部门管理"); }));
tasksList.Add(taskFactory.StartNew(() => { this.Coding("偏执", "客户管理"); }));
tasksList.Add(taskFactory.StartNew(() => { this.Coding("清茶", "接口管理"); }));
tasksList.Add(taskFactory.StartNew(() => { this.Coding("秋陌", "写Api"); }));
//如果有一个同学完成了某一个模块,老师就需要准备环境!
//等待某一个线程执行完毕以后 继续往后执行
Task.WaitAny(tasksList.ToArray());
Console.WriteLine("Richard老师开始准备环境部署项目!");
2.2 WaitAll
阻塞式,当前线程会等待所有任务结束,然后才能执行后续语句
List<Task> tasksList = new List<Task>();
TaskFactory taskFactory = new TaskFactory();
tasksList.Add(taskFactory.StartNew(() => { this.Coding("杰克", "系统管理"); }));
tasksList.Add(taskFactory.StartNew(() => { this.Coding("流光易逝", "部门管理"); }));
tasksList.Add(taskFactory.StartNew(() => { this.Coding("偏执", "客户管理"); }));
tasksList.Add(taskFactory.StartNew(() => { this.Coding("清茶", "接口管理"); }));
tasksList.Add(taskFactory.StartNew(() => { this.Coding("秋陌", "写Api"); }));
//Richard老师要等待大家都完成了以后,开始给点评!
Task.WaitAll(tasksList.ToArray()); //阻塞主线程
Console.WriteLine("5个模块均已完成,Richard老师点评!");
2.3 WhenAny
非阻塞式,当前线程会返回一个新的任务,在该任务中等待任意任务结束,因此当前线程不会阻塞
static int TaskMethod(string name, int seconds)
{
Console.WriteLine("Task {0} 运行在线程 {1} 上。是否是线程池线程: {2}",
name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
Thread.Sleep(TimeSpan.FromSeconds(seconds));
return 42 * seconds;
}
var tasks = new List<Task<int>>();
for (int i = 1; i < 4; i++)
{
int counter = i;
var task = new Task<int>(() => TaskMethod(string.Format("Task {0}", counter), counter));
tasks.Add(task);
task.Start();
}
while (tasks.Count > 0)
{
var completedTask = Task.WhenAny(tasks).Result;//这里的Result是指代Task<int>!
tasks.Remove(completedTask);
Console.WriteLine("A task has been completed with result {0}.", completedTask.Result);
}
2.4 WhenAll
非阻塞式,当前线程会返回一个新的任务,在该任务中等待所有任务结束,因此当前线程不会阻塞文章来源:https://www.toymoban.com/news/detail-589834.html
static int TaskMethod(string name, int seconds)
{
Console.WriteLine("Task {0} 运行在线程 {1} 上。是否是线程池线程: {2}",
name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
Thread.Sleep(TimeSpan.FromSeconds(seconds));
return 42 * seconds;
}
var firstTask = new Task<int>(() => TaskMethod("First Task", 3));
var secondTask = new Task<int>(() => TaskMethod("Second Task", 2));
var whenAllTask = Task.WhenAll(firstTask, secondTask);
whenAllTask.ContinueWith(t =>Console.WriteLine("The first answer is {0}, the second is {1}", t.Result[0], t.Result[1])
, TaskContinuationOptions.OnlyOnRanToCompletion
);
firstTask.Start();
secondTask.Start();
文章来源地址https://www.toymoban.com/news/detail-589834.html
到了这里,关于C#基础--线程之Task的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!