マルチスレッド ビジネス ロジックで次のコードを実行しています。
using System;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var biz1 = new Biz { Some = 1, Value = "a" };
var biz2 = new Biz { Some = 2, Value = "b" };
var foo = new Foo();
//thread 1
new Task(() => foo.Run(biz1)).Start();
//thread 2
new Task(() => foo.Run(biz2)).Start();
//more threads here for other Biz objects....
Console.Read();
}
}
public class Biz
{
public int Some { get; set; }
public string Value { get; set; }
}
public class Foo
{
public void Run(Biz biz)
{
//base on the biz object do some task here
}
}
}
オブジェクトは、biz
スレッド化中にいつでも変更されていません
質問:
foo.Run
スレッドセーフですか?- 個々のオブジェクトをインスタンス化して各
Foo
オブジェクトを実行する方がよいでしょうかBiz
(Run
内の唯一の関数ですFoo
)。