我有一个带有 2 个线程的 C# 代码。它调用 print 方法,但它总是有相同的时间.. 为什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MultiThreading
{
class Program
{
static void Main(string[] args)
{
Thread thread = new Thread(new ThreadStart(new Program().print));
Thread thread2 = new Thread(new ThreadStart(new Program().print));
thread.Name = "Thread1";
thread2.Name = "thread2";
thread.Start();
thread2.Start();
}
public void print()
{
Random r = new Random();
int time = r.Next(3000);
System.Console.WriteLine(Thread.CurrentThread.Name + ", " + (double)time/1000 + " secs!");
Thread.Sleep(time);
}
}
}
好的,所以我有 2 个线程,每个线程都有“打印”委托。
print 生成一个数字time
什么什么time
秒。
thread
并且thread2
总是生成相同的时间,如何解决?