C#中multithreading的Lambda表达式
我试图理解为什么这个程序不起作用
预期输出:随机顺序的数字0-19运行时得到的结果:重复一些数字,有时打印20。
请帮忙。 我在DoSomething()中尝试使用lock(obj),但它没有帮助。
程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication2 { public delegate void callbackDelegate(int x); class Program { void processCallback(int x) { Console.WriteLine("IN callback: The value I got is " + x); } static void Main(string[] args) { Program p = new Program(); p.processinThreads(); Console.ReadKey(); } public void DoSomething(int x, callbackDelegate callback) { Thread.Sleep(1000); //Console.WriteLine("I just woke up now " + x); callback(x); } public void processinThreads() { for (int i = 0; i DoSomething(i, processCallback))); t.Start(); } } } }
public void processinThreads() { for (int i = 0; i < 20; i++) { int local = i; Thread t = new Thread(new ThreadStart(()=>DoSomething(local, processCallback))); t.Start(); } }
你的问题与关闭lambda有关。
您应该使用TPL,它比手动线程管理更容易和推荐 :
Parallel.For(0, 20, x => { Thread.Sleep(1000); Console.WriteLine("IN callback: The value I got is " + x); });
这也将阻塞,直到循环结束,如果你不希望你可以使用TPL Task
,但我肯定会建议避免线程。
正如Jakub已经告诉过你的那样,你需要将i
复制到另一个局部变量local
。 在你的代码中,代理可以直接访问i
本身,而不是i
的副本,因此它们打印出i
当前值,这可能比启动线程时更大。 这叫做封闭。