更新时间:2022-03-01 22:53:45
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.