C#控制台下测试多线程的方法
作者:feige 时间:2022-02-19 15:43:51
本文实例讲述了C#控制台下多线程实现方法。分享给大家供大家参考。具体如下:
class Program
{
static void Main(string[] args)
{
ThreadStart num = new ThreadStart(PrintNum);
Thread ConstrolNum = new Thread(num);
ThreadStart str = new ThreadStart(PrintStr);
Thread ConstrolStr = new Thread(str);
Stopwatch watch = new Stopwatch();
watch.Start();
ConstrolNum.Start();
ConstrolStr.Start();
while (true)
{
if (ConstrolNum.ThreadState == System.Threading.ThreadState.Stopped && ConstrolStr.ThreadState == System.Threading.ThreadState.Stopped)
{
watch.Stop();
Console.WriteLine(watch.Elapsed.TotalMilliseconds);
break;
}
}
Console.ReadKey();
}
private static void PrintNum()
{
for (int i = 1; i < 1000; i++)
{
Console.WriteLine(i);
}
}
private static void PrintStr()
{
for (int i = 1; i < 1000; i++)
{
Console.WriteLine("当前数为:{0}", i);
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java SoftReference类案例详解
2023-04-07 06:52:29
Spring Task定时任务每天零点执行一次的操作
2021-09-12 10:59:44
C#算法函数:获取一个字符串中的最大长度的数字
2022-12-25 10:20:04
使用JSON.toJSONString格式化成json字符串时保留null属性
2023-05-10 14:56:34
解决nacos项目启动报错:Connection refused: no further informa问题
2023-08-16 16:55:41
![](https://img.aspxhome.com/file/2023/1/65201_0s.jpg)
Spring Bean实例的创建及构造器的挑选
2021-08-02 09:35:57
Android 自定义密码输入框实现代码
2023-02-11 09:18:40
![](https://img.aspxhome.com/file/2023/2/88712_0s.png)
springboot 注解方式批量插入数据的实现
2022-02-20 19:03:50
![](https://img.aspxhome.com/file/2023/2/71692_0s.jpg)
Android对话框AlertDialog详解
2023-06-20 01:47:19
![](https://img.aspxhome.com/file/2023/3/82423_0s.jpg)
Spring的AOP极简入门
2023-07-10 22:24:32
![](https://img.aspxhome.com/file/2023/2/76482_0s.png)
浅谈java并发之计数器CountDownLatch
2023-03-21 23:42:59
![](https://img.aspxhome.com/file/2023/2/66702_0s.jpg)
JAVA实现LRU算法的参考示例
2022-01-26 21:56:49
c#求两个数中最大值的方法
2021-09-21 15:24:20
ShardingSphere jdbc集成多数据源的实现步骤
2023-11-25 07:54:56
![](https://img.aspxhome.com/file/2023/6/59766_0s.png)
Java 使用IO流实现大文件的分割与合并实例详解
2023-08-23 09:33:33
![](https://img.aspxhome.com/file/2023/8/58078_0s.png)
Mybatis-plus实现主键自增和自动注入时间的示例代码
2022-11-05 00:22:51
完美解决idea创建文件时,文件不分级展示的情况
2022-01-01 22:10:19
![](https://img.aspxhome.com/file/2023/9/62099_0s.jpg)
可视化Swing中JTable控件绑定SQL数据源的两种方法深入解析
2023-11-28 19:13:58
![](https://img.aspxhome.com/file/2023/3/60283_0s.jpg)
Java 调用天气Webservice详解及实例代码
2021-10-09 21:59:03
Java编程实现轨迹压缩之Douglas-Peucker算法详细代码
2023-11-29 15:25:47
![](https://img.aspxhome.com/file/2023/6/59486_0s.png)