给出以下代码片段(在学习线程时在某处找到).
public class BlockingQueue<T>
{
private readonly object sync = new object();
private readonly Queue<T> queue;
public BlockingQueue()
{
queue = new Queue<T>();
}
public void Enqueue(T item)
{
lock (sync)
{
queue.Enqueue(item);
Monitor.PulseAll(sync);
}
}
public T Dequeue()
{
lock (sync)
{
while (queue.Count == 0)
Monitor.Wait(sync);
return queue.Dequeue();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要了解的是,
为什么会有一个while循环?
while (queue.Count == 0)
Monitor.Wait(sync);
Run Code Online (Sandbox Code Playgroud)
这有什么问题,
if(queue.Count == 0)
Monitor.Wait(sync);
Run Code Online (Sandbox Code Playgroud)
事实上,当我看到使用while循环时发现的类似代码时,任何人都可以帮助我理解一个在另一个之上的使用.谢谢.
我一直在努力理解正则表达式,有没有办法可以替换两个字符串之间的字符/例如我有
sometextREPLACEsomeothertext
我想替换,REPLACE(可以是实际工作中的任何东西)只在sometext和someothertext之间与其他字符串.任何人都可以帮我这个.
编辑 假设,我的输入字符串是
sometext_REPLACE_someotherText_something_REPLACE_nothing
我想在sometext和someotherText之间替换REPLACE文本,从而产生以下输出
sometext_THISISREPLACED_someotherText_something_REPLACE_nothing
谢谢