且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

线程安全队列 - 入队/出队

更新时间:2022-03-01 22:54:21

private void DeQueueAlarm()
{
    Alarm alarm;
    while (alarmQueue.TryDequeue(out alarm))
        SendAlarm(alarm);
}

或者,您可以使用:

private void DeQueueAlarm()
{
    foreach (Alarm alarm in alarmQueue)
        SendAlarm(alarm);
}

根据ConcurrentQueue<T>.GetEnumerator上的 MSDN 文章>:

Per the MSDN article on ConcurrentQueue<T>.GetEnumerator:

枚举表示队列内容的即时快照.在调用 GetEnumerator 后,它不会反映对集合的任何更新.枚举器可以安全地与队列的读取和写入同时使用.

The enumeration represents a moment-in-time snapshot of the contents of the queue. It does not reflect any updates to the collection after GetEnumerator was called. The enumerator is safe to use concurrently with reads from and writes to the queue.

因此,当您的 DeQueueAlarm 方法被多个线程同时调用时,两种方法之间的差异就会出现.使用 TryQueue 方法,可以保证队列中的每个 Alarm 只会被处理一次;然而,哪个线程选择哪个警报是不确定的.foreach 方法确保每个竞赛线程将处理队列中的所有警报(从它开始迭代它们的时间点开始),从而导致多次处理相同的警报.

Thus, the difference between the two approaches arises when your DeQueueAlarm method is called concurrently by multiple threads. Using the TryQueue approach, you are guaranteed that each Alarm in the queue would only get processed once; however, which thread picks which alarm is determined non-deterministically. The foreach approach ensures that each racing thread will process all alarms in the queue (as of the point in time when it started iterating over them), resulting in the same alarm being processed multiple times.

如果您想只处理每个警报一次,然后将其从队列中删除,您应该使用第一种方法.

If you want to process each alarm exactly once, and subsequently remove it from the queue, you should use the first approach.