Re: [問題] 請教thread問題
※ 引述《missbus (smile)》之銘言:
前文述刪
看了閣下的程式後
關鍵在於
new Thread(new ThreadStart(new hello(1).add)).Start();
new Thread(new ThreadStart(new hello(2).add)).Start();
使得兩條 Thread 以一前一後的方式開始執行
接著在 Add 方法中
public void add()
{
for (int k = 0; k < 10; k++)
{
lock (this)
{
test.hello[1, 1] += 1;
Console.WriteLine("hello object " + i + " -- " +
Thread.CurrentContext.ToString() +
" -- " + test.hello[1, 1]);
}
}
}
雖然有 lock 但是兩條 Thread 並行的結果,使得在離開 lock 後另一條 Thread 的
函式有機可趁,故結果是無法預測的
--
my blog: http://kenshinnn.blogspot.com
operation system : Ubuntu GNU/linux
window manager : Gnome
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.232.5.46
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 2 之 5 篇):