您好我是多线程并尝试将其合并到我的程序中,以便使特定计算更快地收敛。但是当我创建下面的两个线程时
Thread firstThread = new Thread(() => { p0 = f(changePoint, result); });
Thread secondThread = new Thread(() => { p1 = f(changePoint + 1, result); });
firstThread.Start();
secondThread.Start();
firstThread.Join();
secondThread.Join();
p0和p1中的值相同(p0与p1的值相同)。现在,如果我拿走多线程并只是这样称呼它们:
p0 = f(changePoint, result);
p1 = f(changePoint + 1, result);
返回不同的值,一切正常。
我缺少什么?
代码f(x,y)
public double f(double x,double result)
{
double temp = PCAcont.Future2Yrs(x).Last().FirstOrDefault().StatNetWorthToAssets.GetValueOrDefault();
return temp - result;
}
主要方法
public double SecantMethod(double prec, int stepsCutoff, double changePoint, double result)
{
double p2, p1 = 0, p0 = 0;
int i;
Thread firstThread = new Thread(() => { p0 = f(changePoint, result); });
Thread secondThread = new Thread(() => { p1 = f(changePoint + 1, result); });
firstThread.Start();
secondThread.Start();
firstThread.Join();
secondThread.Join();
//p0 = f(changePoint, result);
//p1 = f(changePoint + 1, result);
p2 = p1 - f(p1, result) * (p1 - p0) / (f(p1, result) - f(p0, result));
for (i = 0; System.Math.Abs(p2 - p1) > prec && i < stepsCutoff; i++)
{
p0 = p1;
p1 = p2;
p2 = p1 - f(p1, result) * (p1 - p0) / (f(p1, result) - f(p0,result));
}
if (i < stepsCutoff)
return p2;
else
{
System.Diagnostics.Debug.WriteLine("{0}.The method did not converge", p2);
return double.NaN;
}
}
答案 0 :(得分:0)
基于你的评论只采取第二个。
看看这个
PCAcont.Future2Yrs(X)。去年()FirstOrDefault()StatNetWorthToAssets.GetValueOrDefault()。;
PCAcont未锁定,您将获得第二个X的结果
你需要锁定。
并锁定了返回,因为linq可能会被推迟。