写一个Csharp的多线程例子
时间: 2023-09-04 21:05:08 浏览: 80
### 回答1:
C#的多线程例子可以写成这样: using System;
using System.Threading; namespace MultiThreadedExample
{
public class Program
{
public static void Main()
{
Thread t1 = new Thread(new ThreadStart(ThreadMethod1));
t1.Start();
Thread t2 = new Thread(new ThreadStart(ThreadMethod2));
t2.Start();
}
public static void ThreadMethod1()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread1: {0}", i);
Thread.Sleep(500);
}
}
public static void ThreadMethod2()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread2: {0}", i);
Thread.Sleep(500);
}
}
}
}
### 回答2:
以下是一个简单的C#多线程例子:
```csharp
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
// 创建两个线程对象
Thread thread1 = new Thread(CountNumbers);
Thread thread2 = new Thread(CountNumbers);
// 启动两个线程
thread1.Start();
thread2.Start();
// 等待两个线程完成
thread1.Join();
thread2.Join();
Console.WriteLine("所有线程执行完毕");
}
static void CountNumbers()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("当前线程ID: {0}, 数字: {1}", Thread.CurrentThread.ManagedThreadId, i);
Thread.Sleep(100); // 暂停100毫秒
}
}
}
```
上面的例子创建了两个线程对象,并使用`Start`方法启动它们。每个线程执行相同的函数`CountNumbers`,该函数会输出当前线程ID和数字1到10。为了模拟并发执行,我们在每次输出后让线程暂停100毫秒。最后,通过调用线程的`Join`方法来等待线程完成,然后打印出"所有线程执行完毕"。
### 回答3:
下面是一个使用C#编写的多线程示例:
```
using System;
using System.Threading;
class Program
{
static void Main()
{
// 创建5个线程
for (int i = 1; i <= 5; i++)
{
Thread thread = new Thread(CountNumbers);
thread.Start(i);
}
Console.ReadKey();
}
static void CountNumbers(object threadNumber)
{
// 打印线程号
Console.WriteLine("线程 {0} 开始", threadNumber);
// 打印数字1到10
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("线程 {0}: {1}", threadNumber, i);
Thread.Sleep(100);
}
// 打印线程结束信息
Console.WriteLine("线程 {0} 结束", threadNumber);
}
}
```
在此示例中,我们创建了5个线程来打印数字1到10。每个线程都负责打印一部分数字,并且它们可以同时运行。我们使用`Thread.Start()`方法启动每个线程,并通过`Thread.Sleep()`方法在每次打印之间添加了一些延迟来模拟多线程的并发执行。最后,我们通过`Console.ReadKey()`方法来暂停主线程的执行,以便在所有其他线程完成后程序不会立即退出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)