これに似たシングルトンクラスがあります
public class Singleton
{
private static Singleton m_instance;
private Timer m_timer;
private static List<CustomObject> m_cacheObjects;
private Singleton()
{
m_cacheObjects = new List<CustomObject>();
m_timer= new Timer(MyTimerCallBack,
null,
TimeSpan.FromSeconds(60),
TimeSpan.FromSeconds(60));
}
public static Singleton Instance
{
get
{
if (m_instance == null)
{
m_instance = new Singleton();
}
return m_instance;
}
}
private void MyTimerCallBack(object state)
{
//******** Update the list by interval here ******************
m_cacheObjects = UpdateTheList();
}
public void CallMe()
{
foreach (CustomObject obj in m_cacheObjects)
{
// do something here based on obj
// The question is, does the m_cacheObjects is thread safe??
// what happen if the m_cacheObjects is changed
// during the loop interation?
}
}
}
CallMe メソッドは Web サービスによって呼び出されます。
[WebMethod]
public void CallMeWebService()
{
Singleton.Instance.CallMe();
}
質問: 1) m_cacheObjects はスレッドセーフですか? m_cacheObjects が (タイマーのために) ループの反復中に ( CallMe() で) 変更された場合はどうなりますか?
2) Web サービスの CallMeWebService() が呼び出されると、新しいスレッドが作成されますか?