编写一个多线程程序,同时启动2个线程,a线程打印1到100,b线程打印101到200

2024-12-02 04:05:37
推荐回答(2个)
回答1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Thread thread1 = new Thread(Print1_100);
Thread thread2 = new Thread(Print101_200);

thread1.Start();
thread2.Start();

Console.Read();
}

private static void Print1_100()
{
Print(1, 100);
}

private static void Print101_200()
{
Print(101, 200);
}

private static void Print(int min, int max)
{
for (int i = min; i <= max; i++)
{
Console.WriteLine(i);
}
}
}
}

回答2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace BaiduZhidao
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnThread_Click(object sender, EventArgs e)
{
Thread th1 = new Thread(new ThreadStart(print1));
Thread th2 = new Thread(new ThreadStart(print2));
th1.Start();
th2.Start();

}
private void print1()
{
print(1, 100);
}
private void print2()
{
print(101, 200);
}

private void print(int begin, int end)
{
for (int i = begin; i <= end; i++)
{
Console.WriteLine(i);
}

}
}
}