本文介绍了线程 - 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨! 我已经使用线程完成了一些代码,只是为了检查编写代码是否不正确 在使用某种安全数据时可能会导致严重问题类型。 所以在我的理解中,堆栈(集合类型)不会给任何项目更多 而不是一次。但确实如此!!! 我再次不正确地编写了这段代码,看看会发生什么但是我不知道为什么会发生这种情况所以如果你理解的话问题只是回复;) private void btnTestThreading_Click(object sender,EventArgs e) { Stack< intnumbersStack = new Stack< int>(); for(int i = 0; i< 100; i ++) numbersStack.Push(i); for(int i = 0; i< 10; i ++) { 线程th =新线程(delegate() { //我把它作为参考,所以它应该是 很好地分享 TestThreading(ref numbersStack,i); } ); th.Start(); } } private void TestThreading(ref Stack< intnumbers,int threadNumber) { for(int i = 0; i< 10; i ++) { Thread.Sleep(numbers.Peek()* 10); Console.WriteLin e(带号码的线程) + threadNumber + " \t\t processing item" + numbers.Pop()); } } 根据我的理解,最糟糕的情况是2线程 同时访问堆栈(我没有使用任何锁定)而且 相同的项目将被处理两次。但是发生的事情是1个线程两次获得相同项目的 ... 如何可能?我会理解,如果它是其他线程,但它是 同一个。如果你删除Thread.Sleep它会停止(至少在我的;) 谢谢 Jedrzej Hi! I''ve done some code using threading just to check if writing code improperlycan cause serious issues when using somehow safe data types. So in my understanding a stack ( collection type ) won''t give any item morethan once. But it does !!! Again I wrote this code improperly to see what happens but I don''tunderstand why it happens so if you understand the issue only than reply ;) private void btnTestThreading_Click(object sender, EventArgs e){Stack<intnumbersStack = new Stack<int>();for (int i = 0; i < 100; i++)numbersStack.Push(i); for (int i = 0; i < 10; i++){Thread th = new Thread(delegate(){// i''m putting this as a reference so it should beshared nicelyTestThreading(ref numbersStack, i);}); th.Start();}} private void TestThreading(ref Stack<intnumbers, int threadNumber){for (int i = 0; i < 10; i++){Thread.Sleep(numbers.Peek() * 10);Console.WriteLine("Thread with number " + threadNumber +"\t\t processed item " + numbers.Pop());}}In my understanding the worst case scenario here would be 2 threadsaccessing stack in the same time ( i''m not using any locks ) and than thesame item would be ''processed'' twice. But what happens is 1 thread is gettingthe same item twice...HOW it''s possible ? I would understand if it was other thread but it''s thesame one. If you delete Thread.Sleep it stops ( at least at mine ;) ThanksJedrzej 推荐答案 你是对的;) 你能解释一下这两个不同的线程是如何发生的吗? 在这种情况下,堆栈的结果会得到相同的结果。 当1个对象调用Pop()时,它应该从堆栈中删除一个项目,它们是访问内存中相同位置的。 ..可能有一个愚蠢的解释 :) 我需要解释它如何工作以更好地理解:) 谢谢 Jedrzej And you was right ;)Could you explain how this happens that 2 diffrent threads accessing samereference in this case stack are getting same results.When 1 object invokes Pop() it should remove an item from the stack they areaccessing same place in the memory... There is probably a silly explanation:)I need an explanation of how it works to get better understanding :) ThanksJedrzej 你是对的;) 你能解释一下这种情况如何发生这种情况下,在这种情况下,两个不同的线程访问相同的 引用得到相同的结果。 当1个对象时调用Pop()它应该从堆栈中删除一个项目 访问内存中的相同位置......可能有一个愚蠢的解释 :) 我需要解释它是如何工作以更好地理解:) And you was right ;)Could you explain how this happens that 2 diffrent threads accessing samereference in this case stack are getting same results.When 1 object invokes Pop() it should remove an item from the stack they areaccessing same place in the memory... There is probably a silly explanation:)I need an explanation of how it works to get better understanding :) 那么,Stack声称是线程安全的吗?大多数.NET集合都没有b / b 声称 - 如果你从多个没有锁定的 线程访问相同的集合,我会期望看到一些奇怪的东西。当你在看 系列时获得锁定时,你会得到同样的问题吗? 另外,我想你对于什么引用有一些误解确实 - 请参阅 http:// pobox。 com / ~siget / csharp / parameters.html Jon Well, does Stack claim to be thread-safe? Most .NET collections don''tmake that claim - if you access the same collection from multiplethreads with no locking, I''d expect to see some oddness. Do you getthe same issues when you obtain a lock while looking at thecollection? Also, I think you have some misunderstandings about what "ref" does -see http://pobox.com/~skeet/csharp/parameters.html Jon 这篇关于线程 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 20:53