小编41K*_*41K的帖子

Monitor.Wait,Condition变量

给出以下代码片段(在学习线程时在某处找到).

 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循环时发现的类似代码时,任何人都可以帮助我理解一个在另一个之上的使用.谢谢.

c# multithreading synchronization

11
推荐指数
1
解决办法
5975
查看次数

正则表达式之间替换

我一直在努力理解正则表达式,有没有办法可以替换两个字符串之间的字符/例如我有

sometextREPLACEsomeothertext

我想替换,REPLACE(可以是实际工作中的任何东西)在sometext和someothertext之间与其他字符串.任何人都可以帮我这个.

编辑 假设,我的输入字符串是

sometext_REPLACE_someotherText_something_REPLACE_nothing

我想在sometext和someotherText之间替换REPLACE文本,从而产生以下输出

sometext_THISISREPLACED_someotherText_something_REPLACE_nothing

谢谢

c# regex

8
推荐指数
1
解决办法
5404
查看次数

标签 统计

c# ×2

multithreading ×1

regex ×1

synchronization ×1