这里主要介绍了c#使用多线程的几种方式,通过示例学习c#的多线程使用方式,大家参考使用吧!
1 进程、线程、同步、异步的概念
2 回顾委托,开始异步
3 异步多线程的三大特点
异步多线程都觉得很厉害,也是面试必备,高级开发必备技能
多线程很熟悉/经常在用的 ?
多线程在用,但是很懵 ?
一直没怎么敢用的 ?
多线程在.Net不同的版本里面,都在不断的升级
单进程多线程的模型
进程:计算机概念,把程序运行时占用的全部计算资源,叫做进程
线程:计算机概念,程序执行的最小单位,包含自己使用的各种计算资源
多线程:计算机概念,多个线程同时执行(上传画面/声音上传/获取聊天消息)
一个进程可以包含多个线程,线程是隶属于进程的
C#是面向对象语言,万物皆对象,类来映射现实生活中的物体
Thread就是线程类
多个Thread并发执行,就是多线程
委托的异步调用
1 同步方法卡界面,Winform的UI线程在忙于计算,不能响应别的操作
异步多线程方法不卡界面,UI线程闲置,计算任务交给其他线程执行
Winform--点击个按钮不希望界面卡死
Web--写文本日志,发邮件,这正耗时操作其实可以多线程的
2 同步方法慢(14155),只有一个线程计算
异步多线程方法快(5144),多个线程并发计算
一般认为计算机是精准的,5个线程比1个线程 那应该是5倍?
多线程是用资源换性能,
a资源有限(一般来说资源够的) b线程调度管理损耗 性能不是线性增长,
速度快的应用太多了,
有个大表查询很慢,能不能多线程优化下,提升下性能? T/F 不行,任务不能拆分
一个数据库查询,一个接口调用,一个硬盘读写,一个相加计算,可以,任务是独立运行的
但是线程不是越多越好
3 多线程的无序性:
启动无序:线程是计算机资源,线程申请是操作系统调度管理的,随机了!
同一个线程执行同一个任务时间不确定:CPU分片,看运气(线程优先级)
结束无序:以上叠加
这个是提请注意,很多时候多线程操作,但是又有顺序要求,
延迟一下启动? 不靠谱, 线程优先级也不靠谱
也许运行1w次都是对的,但是随着业务的变化 数据量的增加 服务器的升级,执行时间都是会变的
以前运行的好好的,偶尔就错一下,或者数据量大了,总是错
线程顺序的控制,且听下回分解
(0)线程池
- //线程池
- class Thread2
- {
- public void ThreadMain()
- {
- ThreadDemoClass demoClass = new ThreadDemoClass();
-
- //设置当没有请求时线程池维护的空闲线程数
- //第一个参数为辅助线程数
- //第二个参数为异步 I/O 线程数
- ThreadPool.SetMinThreads(5, 5);
-
- //设置同时处于活动状态的线程池的线程数,所有大于次数目的请求将保持排队状态,直到线程池变为可用
- //第一个参数为辅助线程数
- //第二个参数为异步 I/O 线程数
- ThreadPool.SetMaxThreads(100, 100);
-
- //使用委托绑定线程池要执行的方法(无参数)
- WaitCallback waitCallback1 = new WaitCallback(demoClass.Run1);
- //将方法排入队列,在线程池变为可用时执行
- ThreadPool.QueueUserWorkItem(waitCallback1);
-
-
- //使用委托绑定线程池要执行的方法(有参数)
- WaitCallback waitCallback2 = new WaitCallback(demoClass.Run1);
- //将方法排入队列,在线程池变为可用时执行
- ThreadPool.QueueUserWorkItem(waitCallback2, "张三");
-
-
- UserInfo userInfo = new UserInfo();
- userInfo.Name = "张三";
- userInfo.Age = 33;
-
- //使用委托绑定线程池要执行的方法(有参数,自定义类型的参数)
- WaitCallback waitCallback3 = new WaitCallback(demoClass.Run2);
- //将方法排入队列,在线程池变为可用时执行
- ThreadPool.QueueUserWorkItem(waitCallback3, userInfo);
-
- Console.WriteLine();
- Console.WriteLine("主线程正在工作…");
- Console.WriteLine("主线程ID为:" + Thread.CurrentThread.ManagedThreadId.ToString());
- Console.ReadKey();
- }
-
- //子线程类
- public class ThreadDemoClass
- {
- //子线程1
- public void Run1(object obj)
- {
- string name = obj as string;
-
- Console.WriteLine();
- Console.WriteLine("子线程正在工作…");
- Console.WriteLine("我的名字是 " + name);
- Console.WriteLine("子线程ID为:" + Thread.CurrentThread.ManagedThreadId.ToString());
- }
-
- //子线程2
- public void Run2(object obj)
- {
- UserInfo userInfo = (UserInfo)obj;
-
- Console.WriteLine();
- Console.WriteLine("子线程正在工作…");
- Console.WriteLine("我的名字是 " + userInfo.Name);
- Console.WriteLine("我今年" + userInfo.Age + "岁");
- Console.WriteLine("子线程ID为:" + Thread.CurrentThread.ManagedThreadId.ToString());
- }
- }
-
- public class UserInfo
- {
- public string Name { get; set; }
-
- public int Age { get; set; }
- }
- }
(1)不需要传递参数,也不需要返回参数
ThreadStart是一个委托,这个委托的定义为void ThreadStart(),没有参数与返回值。
- class Program
-
-
- {
-
- static void Main(string[] args)
-
- {
-
- for (int i = 0; i < 30; i++)
-
- {
-
- ThreadStart threadStart = new ThreadStart(Calculate);
-
- Thread thread = new Thread(threadStart);
-
- thread.Start();
-
- }
-
- Thread.Sleep(2000);
-
- Console.Read();
-
- }
-
- public static void Calculate()
-
- {
-
- DateTime time = DateTime.Now;//得到当前时间
-
- Random ra = new Random();//随机数对象
-
- Thread.Sleep(ra.Next(10,100));//随机休眠一段时间
-
- Console.WriteLine(time.Minute + ":" + time.Millisecond);
-
- }
-
- }
(2)需要传递单个参数
ParameterThreadStart委托定义为void ParameterizedThreadStart(object state),有一个参数但是没有返回值。
- class Program
- {
-
- static void Main(string[] args)
-
- {
-
- for (int i = 0; i < 30; i++)
-
- {
-
- ParameterizedThreadStart tStart = new ParameterizedThreadStart(Calculate);
-
- Thread thread = new Thread(tStart);
-
- thread.Start(i*10+10);//传递参数
-
- }
-
- Thread.Sleep(2000);
-
- Console.Read();
-
- }
-
- public static void Calculate(object arg)
-
- {
-
- Random ra = new Random();//随机数对象
-
- Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间
-
- Console.WriteLine(arg);
-
- }
-
- }
(3)使用专门的线程类(常用)
使用线程类可以有多个参数与多个返回值,十分灵活!
- class Program
- {
-
- static void Main(string[] args)
-
- {
-
- MyThread mt = new MyThread(100);
-
- ThreadStart threadStart = new ThreadStart(mt.Calculate);
-
- Thread thread = new Thread(threadStart);
-
- thread.Start();
-
- //等待线程结束
-
- while (thread.ThreadState != ThreadState.Stopped)
-
- {
-
- Thread.Sleep(10);
-
- }
-
- Console.WriteLine(mt.Result);//打印返回值
-
- Console.Read();
-
- }
-
- }
-
- public class MyThread//线程类
-
- {
-
- public int Parame { set; get; }//参数
-
- public int Result { set; get; }//返回值
-
- //构造函数
-
- public MyThread(int parame)
-
- {
-
- this.Parame = parame;
-
- }
-
- //线程执行方法
-
- public void Calculate()
-
- {
-
- Random ra = new Random();//随机数对象
-
- Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间
-
- Console.WriteLine(this.Parame);
-
- this.Result = this.Parame * ra.Next(10, 100);
-
- }
-
- }
(4)使用匿名方法(常用)
使用匿名方法启动线程可以有多个参数和返回值,而且使用非常方便!
- class Program
- {
-
- static void Main(string[] args)
-
- {
-
- int Parame = 100;//当做参数
-
- int Result = 0;//当做返回值
-
- //匿名方法
-
- ThreadStart threadStart = new ThreadStart(delegate()
-
- {
-
- Random ra = new Random();//随机数对象
-
- Thread.Sleep(ra.Next(10, 100));//随机休眠一段时间
-
- Console.WriteLine(Parame);//输出参数
-
- Result = Parame * ra.Next(10, 100);//计算返回值
-
- });
-
- Thread thread = new Thread(threadStart);
-
- thread.Start();//多线程启动匿名方法
-
- //等待线程结束
-
- while (thread.ThreadState != ThreadState.Stopped)
-
- {
-
- Thread.Sleep(10);
-
- }
-
- Console.WriteLine(Result);//打印返回值
-
- Console.Read();
-
- }
- }
(5)使用委托开启多线程(多线程深入)
1、用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程
BeginInvoke方法可以使用线程异步地执行委托所指向的方法。然后通过EndInvoke方法获得方法的返回值(EndInvoke方法的返回值就是被调用方法的返回值),或是确定方法已经被成功调用。
- class Program
- {
-
- private delegate int NewTaskDelegate(int ms);
-
- private static int newTask(int ms)
-
- {
-
- Console.WriteLine("任务开始");
-
- Thread.Sleep(ms);
-
- Random random = new Random();
-
- int n = random.Next(10000);
-
- Console.WriteLine("任务完成");
-
- return n;
-
- }
-
- static void Main(string[] args)
-
- {
-
- NewTaskDelegate task = newTask;
-
- IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);
-
- //EndInvoke方法将被阻塞2秒
-
- int result = task.EndInvoke(asyncResult);
-
- Console.WriteLine(result);
-
- Console.Read();
-
- }
-
- }
2、使用IAsyncResult.IsCompleted属性来判断异步调用是否完成
- class Program
- {
-
- private delegate int NewTaskDelegate(int ms);
-
- private static int newTask(int ms)
-
- {
-
- Console.WriteLine("任务开始");
-
- Thread.Sleep(ms);
-
- Random random = new Random();
-
- int n = random.Next(10000);
-
- Console.WriteLine("任务完成");
-
- return n;
-
- }
-
- static void Main(string[] args)
-
- {
-
- NewTaskDelegate task = newTask;
-
- IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);
-
- //等待异步执行完成
-
- while (!asyncResult.IsCompleted)
-
- {
-
- Console.Write("*");
-
- Thread.Sleep(100);
-
- }
-
- // 由于异步调用已经完成,因此, EndInvoke会立刻返回结果
-
- int result = task.EndInvoke(asyncResult);
-
- Console.WriteLine(result);
-
- Console.Read();
-
- }
-
- }
3、使用WaitOne方法等待异步方法执行完成
WaitOne的第一个参数表示要等待的毫秒数,在指定时间之内,WaitOne方法将一直等待,直到异步调用完成,并发出通知,WaitOne方法才返回true。当等待指定时间之后,异步调用仍未完成,WaitOne方法返回false,如果指定时间为0,表示不等待,如果为-1,表示永远等待,直到异步调用完成。
- class Program
- {
-
- private delegate int NewTaskDelegate(int ms);
-
- private static int newTask(int ms)
-
- {
-
- Console.WriteLine("任务开始");
-
- Thread.Sleep(ms);
-
- Random random = new Random();
-
- int n = random.Next(10000);
-
- Console.WriteLine("任务完成");
-
- return n;
-
- }
-
- static void Main(string[] args)
-
- {
-
- NewTaskDelegate task = newTask;
-
- IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);
-
- //等待异步执行完成
-
- while (!asyncResult.AsyncWaitHandle.WaitOne(100, false))
-
- {
-
- Console.Write("*");
-
- }
-
- int result = task.EndInvoke(asyncResult);
-
- Console.WriteLine(result);
-
- Console.Read();
-
- }
-
- }
4、使用回调方式返回结果
要注意的是“my.BeginInvoke(3,300, MethodCompleted, my)”,BeginInvoke方法的参数传递方式:
前面一部分(3,300)是其委托本身的参数。
倒数第二个参数(MethodCompleted)是回调方法委托类型,他是回调方法的委托,此委托没有返回值,有一个IAsyncResult类型的参数,当method方法执行完后,系统会自动调用MethodCompleted方法。
最后一个参数(my)需要向MethodCompleted方法中传递一些值,一般可以传递被调用方法的委托,这个值可以使用IAsyncResult.AsyncState属性获得。
- class Program
- {
-
- private delegate int MyMethod(int second, int millisecond);
-
- //线程执行方法
-
- private static int method(int second, int millisecond)
-
- {
-
- Console.WriteLine("线程休眠" + (second * 1000 + millisecond) + "毫秒");
-
- Thread.Sleep(second * 1000 + millisecond);
-
- Random random = new Random();
-
- return random.Next(10000);
-
- }
-
- //回调方法
-
- private static void MethodCompleted(IAsyncResult asyncResult)
-
- {
-
- if (asyncResult == null || asyncResult.AsyncState == null)
-
- {
-
- Console.WriteLine("回调失败!!!");
-
- return;
-
- }
-
- int result = (asyncResult.AsyncState as MyMethod).EndInvoke(asyncResult);
-
- Console.WriteLine("任务完成,结果:" + result);
-
- }
-
- static void Main(string[] args)
-
- {
-
- MyMethod my = method;
-
- IAsyncResult asyncResult = my.BeginInvoke(3,300, MethodCompleted, my);
-
- Console.WriteLine("任务开始");
-
- Console.Read();
-
- }
-
- }
- 5、其他组件的BeginInvoke和EndInvoke方法
-
- 在其他的.net组件中也有类似BeginInvoke和EndInvoke的方法,如System.Net.HttpWebRequest类的BeginGetResponse和EndGetResponse方法。其使用方法类似于委托类型的BeginInvoke和EndInvoke方法,例如:
-
- class Program
- {
-
- //回调函数
-
- private static void requestCompleted(IAsyncResult asyncResult)
-
- {
-
- if (asyncResult == null || asyncResult.AsyncState==null)
-
- {
-
- Console.WriteLine("回调失败");
-
- return;
-
- }
-
- HttpWebRequest hwr = asyncResult.AsyncState as HttpWebRequest;
-
- HttpWebResponse response = (HttpWebResponse)hwr.EndGetResponse(asyncResult);
-
- StreamReader sr = new StreamReader(response.GetResponseStream());
-
- string str = sr.ReadToEnd();
-
- Console.WriteLine("返回流长度:"+str.Length);
-
- }
-
- static void Main(string[] args)
-
- {
-
- HttpWebRequest request =
-
- (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
-
- //异步请求
-
- IAsyncResult asyncResult = request.BeginGetResponse(requestCompleted, request);
-
- Console.WriteLine("任务开始");
-
- Console.Read();
-
- }
-
- }