我最近在一个类似于下面的测试中遇到了一个面试问题,我没有很多使用线程开发的经验,有人可以帮助建议我如何处理这个问题吗?:
public class StringQueue
{
private object _lockObject = new object();
private List<string> _items = new List<string>();
public bool IsEmpty()
{
lock (_lockObject)
return _items.Count == 0;
}
public void Enqueue(string item)
{
lock (_lockObject)
_items.Add(item);
}
public string Dequeue()
{
lock (_lockObject)
{
string result = _items[0];
_items.RemoveAt(0);
return result;
}
}
}
Run Code Online (Sandbox Code Playgroud)
以上方法的线程是否安全?为什么?
public string DequeueOrNull()
{
if (IsEmpty())
return null;
return Dequeue();
}
Run Code Online (Sandbox Code Playgroud)