c#使用多线程并发之Thread
来源:互联网 发布:阿里云服务器 密钥 编辑:程序博客网 时间:2024/06/05 05:02
这篇文章主要介绍了c#使用多线程的几种方式,通过示例学习c#的多线程使用方式,大家参考使用吧
(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();}}
阅读全文
0 1
- c#使用多线程并发之Thread
- C#多线程之Thread
- C#多线程之Thread
- C# 多线程的使用(Thread)-初识多线程
- C#使用多线程并发之异步委托IAsyncResult
- C#多线程Thread.Abort的使用
- C#中的多线程使用 -- Thread 类
- C#中的多线程使用 -- Thread 类
- C#中的多线程使用 -- Thread 类
- C#多线程的使用(Thread)-多线程的挂起,恢复操作
- C#使用多线程进行并发处理
- 多线程之Thread.interrupt()方法使用
- C# 多线程 Thread.Abort()
- C#梳理【多线程Thread】
- java并发之Thread
- C#多线程编程介绍——使用thread、threadpool、timer
- C#多线程编程介绍——使用thread、threadpool、timer
- java并发编程之Thread类详细使用(四)
- 解决AndroidStudio用setText()方法乱码
- 浏览器中如何阻止默认事件的发生?
- hdu 6035 树形dp+补形分块
- 单调队列 和 单调栈
- 分布式锁2 Java非常用技术方案探讨之ZooKeeper
- c#使用多线程并发之Thread
- openlayers4
- eclipse Ctrl+Alt+↑ 快捷键冲突的解决办法
- install and config docker toolbox in win10
- Spring教程____Spring简单小列子
- 父类构造器调用被子类重写的方法
- 关于云的一点认识。
- shell 脚本编程之变量
- 深入浅出MySql(2)