Python微信订餐小程序课程视频
https://edu.youkuaiyun.com/course/detail/36074
Python实战量化交易理财系统
https://edu.youkuaiyun.com/course/detail/35475
线程
线程:对于所有需要等待的操作,例如移动文件,数据库和网络访问都需要一定的时间,此时就可以启动一个新的线程,同时完成其他任务。一个进程的多个线程可以同时运行在不同的CPU上或多核CPU的不同内核上。
一个应用程序启动时,会启动一个进程(应用程序的载体),然后进程会启动多个线程。
一,使用Thread类启动线程和数据传输
使用Thread类可以创建和控制线程,Thread构造函数是一个无参无返回值的委托类型。
1️⃣对Thread传入一个无参无返回类型的方法-ThreadStart。
public delegate void ThreadStart();
实例:
static void test()
{
Console.WriteLine("test is start");
Console.WriteLine("test is running");
Thread.Sleep(3000);
Console.WriteLine("test is completed");
}
static void Main(string[] args)
{
Thread th = new Thread(test);
th.Start();
Console.WriteLine("main is completed");
}

2️⃣对Thread传入一个匿名方法(或lambda表达式)。用于传入的方法代码简单的情况
static void Main(string[] args)
{
//lambad表达式
Thread th = new Thread(()=> {
Console.WriteLine("子线程1-ID:" + Thread.CurrentThread.ManagedThreadId);
});
th.Start();
//匿名方法
Thread th2 = new Thread(delegate ()
{
Console.WriteLine("子线程2-I

本文介绍了C#中的线程和任务。首先讲解如何使用Thread类启动线程和数据传输,包括ThreadStart和ParameterizedThreadStart的方式。接着讨论线程池ThreadPool的使用,以及线程的优先级和线程池的限制。然后转向任务,阐述如何创建并启动任务,以及连续任务的实现。最后,文章探讨了资源冲突问题,提出了使用lock关键字解决并发访问同一资源时的冲突问题。
最低0.47元/天 解锁文章
656

被折叠的 条评论
为什么被折叠?



