本文介绍了跨线程共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在开发一个多线程项目,我有一个关于 多个线程同时使用的共享内存的问题。 我有两个独立的线程,它们有不同的回调方法。 线程一是创建一个名为mHistoricalList的ArrayList。并添加 的东西。线程2在相同的 时间循环遍历同一个列表。虽然第一个线程正在向列表添加内容,但第二个线程是无法循环列表。程序崩溃了。 所以我在考虑使用锁定。但它不起作用 - 它给了我一个 运行时未知错误。我的代码如下(在回调方法中 线程一): void CreateHistoricalList(string ticker,double openPrice,double volume) { lock(mHistoricalList) { SymbolPriceVolume spv = new SymbolPriceVolume(ticker) ; PriceVolume pv = new PriceVolume(openPrice,volume); spv.PriceVolumeArray.Add(pv); mHistoricalList.Add(spv ); } } 我使用锁定的方式有什么问题吗?任何人都可以告诉我如何解决这个问题吗?谢谢!I''m working on a multi-threading project and I''ve got an issue aboutshared memory used by multiple threads at the same time.I have two separate threads that have different callback methods.Thread one is to create an ArrayList called "mHistoricalList" and addthings to it. Thread two is looping through the same list at the sametime. While thread one is adding things to the list, thread two isunable to loop the list. So the program crashed.So I was thinking of using "lock". But it doesn''t work - It gives me aruntime unknown error. My code is below (in the callback methond onthread one):void CreateHistoricalList (string ticker, double openPrice, doublevolume){lock (mHistoricalList){SymbolPriceVolume spv = new SymbolPriceVolume(ticker);PriceVolume pv = new PriceVolume(openPrice, volume);spv.PriceVolumeArray.Add(pv);mHistoricalList.Add(spv);}}Is there anything wrong with the way I use the "lock"? Could anyoneadvise me on how I can get this fixed? Thanks!推荐答案 这篇关于跨线程共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 17:18
查看更多