如果我产生各种线程,并告诉他们都使用相同的方法:
internal class Program {
private static DoSomething() {
int result = 0;
Thread.Sleep(1000);
result++;
int ID = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Thread {0} return {1}", ID, result);
}
private static Main() {
Thread[] threads = new Thread[50];
for (int i = 0; i < 50; i++)
threads[i] = new Thread(DoSomething);
foreach (Thread t in threads)
t.Start();
}
}
所有线程会共享同一个堆栈吗?当我运行程序时,所有线程都返回 1,所以我猜答案是否定的,但这是否意味着 CLR 在内存中制作了不同的方法副本?