private void PerformValuations(DateTime testDate, RegressionEffectivenessTest.RegressionDateWithValues date)
{
var valueDate = new LegacyWCFRateTimeStamp { Type = RateTimeStampType.EndOfDay, TimeStamp = date.ValueDate };
var curveDate = new LegacyWCFRateTimeStamp { Type = RateTimeStampType.EndOfDay, TimeStamp = date.CurveDate };
var shiftDate = new LegacyWCFRateTimeStamp { Type = RateTimeStampType.EndOfDay, TimeStamp = date.ShiftDate };
if (date.NeedHedgeValues)
{
date.HedgeCleanPrice = 0M;
date.HedgeCleanIntrinsicValue = 0M;
foreach (var tran in _hedgeTranQuoteIds)
{
var tranquoteId = tran.TranQuoteId;
CheckAndLoadTrade(testDate, valueDate, shiftDate, curveDate, tran, tranquoteId);
var result = ValueTrade(tranquoteId);
var rtnVal = !result.Succeeded
? HandleFailure(tranquoteId, shiftDate, curveDate, result, valueDate)
: CreateAccountingValuation(valueDate, result);
date.HedgeCleanIntrinsicValue += rtnVal.IntrinsicValue - rtnVal.AccruedInterest.GetValueOrDefault(0);
date.HedgeCleanPrice += rtnVal.CleanPrice;
}
}
所以我试图Parallel.ForEach
在这个方法上运行。有几件事让我担心。第一个在CheckAndLoadTrade
方法中,它访问private Dictionary
类的 a 以可能向其中添加一个项目(如果它不存在),然后该ValueTrade
方法从该字典中获取一个项目。
如果我将此并行化,我是否会在访问字典时遇到任何线程安全问题?或者可能还有什么我没有注意到的?所有其他方法调用都使用在它们自己的范围内定义的变量Dictionary
,我真正担心的只是这个。我应该lock
在实际字典访问发生之前和之后抛出一个吗?