我必須維護信息日誌,這些日誌可以從多個線程同時寫入,但是當我需要它們時,我只使用一個線程來將它列出在取出集合之間需要約5秒鐘的休息時間。最有效的方法來創建在一定的時間戳ConcurrentQueue中的所有現有項目列表
以下是我寫給Dequeue的代碼。
if (timeNotReached)
{
InformationLogQueue.Enqueue(informationLog);
}
else
{
int currentLogCount = InformationLogQueue.Count;
var informationLogs = new List<InformationLog>();
for (int i = 0; i < currentLogCount; i++)
{
InformationLog informationLog1;
InformationLogQueue.TryDequeue(out informationLog1);
informationLogs.Add(informationLog1);
}
WriteToDatabase(informationLogs);
}
出隊後,我將它傳遞給LINQ的插入方法,它需要List of InformationLog插入數據庫。
這是正確的方法還是有任何其他有效的方法來做到這一點?
如果您每次出院時都將引用設置爲新實例,那麼使用'ConcurrentQueue'有什麼意義? – Jodrell
@Jodrell,更新了我的問題。 –
爲什麼不直接從該線程使用ConcurrentQueue?爲什麼你先把所有的物品都扔到清單上? –