我得到一个难以复制的错误在下面的程序中,更新平行,主线程并发字典中的线程数显示后固定的时间间隔字典排序顺序的状态,直到所有的更新线程完成.
public void Function(IEnumerable<ICharacterReader> characterReaders, IOutputter outputter)
{
ConcurrentDictionary<string, int> wordFrequencies = new ConcurrentDictionary<string, int>();
Thread t = new Thread(() => UpdateWordFrequencies(characterReaders, wordFrequencies));
bool completed = false;
var q = from pair in wordFrequencies orderby pair.Value descending, pair.Key select new Tuple<string, int>(pair.Key, pair.Value);
t.Start();
Thread.Sleep(0);
while (!completed)
{
completed = t.Join(1);
outputter.WriteBatch(q);
}
}
Run Code Online (Sandbox Code Playgroud)
该函数给出了字符流和输出器的列表.该函数维护从每个字符流中读取的字的字频的并发字典(并行).的文字,被一个新的线程读入,并且,直到所有的输入流已被读出的主线程输出词典的当前状态(以排序的顺序),每1毫秒(在实践中,输出将是什么每10秒等,但错误似乎只出现在非常小的值上.WriteBatch函数只是写入控制台:
public void WriteBatch(IEnumerable<Tuple<string, int>> batch)
{
foreach (var tuple in batch)
{
Console.WriteLine("{0} - {1}", tuple.Item1, tuple.Item2);
}
Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)
大多数执行都很好,但有时我在WriteBatch函数的foreach语句中得到以下错误:
"未处理的异常:System.ArgumentException:索引等于或大于数组的长度,或者字典中的元素数量比索引到目标数组末尾的可用空间大."
如果主线程在启动更新线程之后和启动显示循环之前暂停一段时间,则错误确实会消失.如果删除了orderby子句并且未在linq查询中对字典进行排序,它似乎也会消失.有什么解释吗?
foreach (var …
c# concurrency multithreading race-condition concurrentdictionary
我正在使用 ConcurrentDictionary 来存储日志行,当我需要向用户显示它们时,我会调用ToList()以生成一个列表。但奇怪的是,有些用户在列表中最先收到最近的行,而从逻辑上讲,他们应该在最后。
这是因为 ConcurrentDictionary 不能保证 IEnumerate 接口上的持久顺序,或者是什么原因?
.net concurrency list concurrentdictionary blockingcollection
我正在使用并发字典的GetOrAdd方法来检索值列表,然后引用我正在编辑它们的值列表.这样做是否可以线程安全?
我正在添加一个值的第一种方法和我正在清除列表的第二种方法.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using Test.Web.Services;
namespace Test.Web.Messaging
{
public class Dispatch
{
private static readonly ConcurrentDictionary<string, IList<Message>> Messages = new ConcurrentDictionary<string, IList<Message>>();
public static void AddMessage(string id, Message value)
{
var msgs = Messages.GetOrAdd(id, new List<Message>());
msgs.Add(value);
}
public static void Send(string id)
{
var msgs = Messages.GetOrAdd(id, new List<Message>());
foreach (var msg in msgs)
{
Connection.Send(id, msg);
}
msgs.Clear();
}
}
}
Run Code Online (Sandbox Code Playgroud) 如果我有一个实例,我使用该属性还是 LINQConcurrentDictionary是否重要?我宁愿写,而不是我认为更具描述性的。CountAny()dict.Any()dict.Count > 0Any()
我只关心正确性,而不关心性能。用例是
void process()
{
if (concurrentDictionary.Count <= 0) // or !Any() ?
return; // dictionary is empty, nothing to do
// ...
}
Run Code Online (Sandbox Code Playgroud) 如果我有以下代码:
var dictionary = new ConcurrentDictionary<int, HashSet<string>>();
foreach (var user in users)
{
if (!dictionary.ContainsKey(user.GroupId))
{
dictionary.TryAdd(user.GroupId, new HashSet<string>());
}
dictionary[user.GroupId].Add(user.Id.ToString());
}
Run Code Online (Sandbox Code Playgroud)
将项添加到HashSet本身就是线程安全的行为是因为HashSet是并发字典的值属性吗?
我有一个ConcurrentDictionary. 我使用它的AddOrUpdate方法来操作它的项目。
我的问题是:是否可以使用AddOrUpdate's update 参数来包含 if 语句?例如,我的ConcurrentDictionary包含具有字符串 Id 和 DateTime 日期属性的对象。
我想 - 向它添加一个新对象,如果具有给定 Id 的对象不存在 - 更新它,如果新对象的日期等于或大于现有对象,如果它小于,则不做任何事情。
在我的例子中:
Dictionary.AddOrUpdate(testObject.Id,testObject,(k, v) => v);
Run Code Online (Sandbox Code Playgroud)
我应该改变
(k, v) => v
Run Code Online (Sandbox Code Playgroud)
到
if(v.Date >= existingItem.Date) (k, v) => v
else do nothing
Run Code Online (Sandbox Code Playgroud) 我有一个文件列表,其中每个文件都包含一个Foo数据列表。现在,同一段 Foo 数据(例如Id = 1)可能存在于多个文件中,但最新的数据将覆盖现有的数据。
我只是将每条数据读入内存集合中。
if !cache.HasKey(foo.Id) then Add
else cache[foo.Id].UpdatedOn < foo.UpdatedOn then Update
else do nothing
Run Code Online (Sandbox Code Playgroud)
当我阅读文件时(因为其中有一些),我也在使用Parallel.ForEach(files, file => { .. });
我不知道我该怎么做。
我正在考虑使用 aConcurrentDictionary但我不确定如何使用AddOrUpdatewhere子句。
有什么建议么?
.net c# parallel-processing concurrentdictionary parallel.foreach
我有一个并发字典,其中Ids为键,令牌为值。在某些情况下,我将具有要删除其令牌的ID,在某些情况下,我将具有要删除的特定令牌。我可以在字典上调用什么来查找具有指定值的对?
令牌是ID所独有的。
我正在学习构建聊天客户端和服务器的教程,现在我遇到了以下错误:
Inconsistent accessibility: field type 'System.Collections.Concurrent.ConcurrentDictionary<string,ChattingServer.ConnectedClient>' is less accessible than field 'ChattingServer.ChattingService._connectedClients' c:\Users\KOEMXE\Documents\Visual Studio 2012\Projects\ChatApplication\ChattingServer\ChattingService.cs 17 62 ChattingServer
Run Code Online (Sandbox Code Playgroud)
这是相关问题所在的类文件 (ChattingService.cs):
using ChattingInterfaces;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ChattingServer
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public class ChattingService : IChattingService
{
//private ConnectedClient _connectedClients;
public ConcurrentDictionary<string, ConnectedClient> _connectedClients = new ConcurrentDictionary<string, ConnectedClient>();
public int Login(string userName)
{
//is anyone else logged in with my name?
foreach (var client in _connectedClients) …Run Code Online (Sandbox Code Playgroud) 我在 Azure 应用服务上使用 MVC 5、ASP.NET 4.7
我使用 ConcurrentDictionary 对象来保存数据以保存对数据源的多次调用。
关于其行为的几个问题:
1) 一旦填充,它将持续供同一 Web 应用程序的多个用户访问。我对么?因此,只有网站重新启动后的第一个用户才会受到性能影响。
2) 持续多久?是直到另一次池刷新或站点重新启动,还是存在某种形式的非活动超时?
提前致谢。
编辑:
public class myCache
{
private static readonly ConcurrentDictionary<int, string> myCacheDictionary = new ConcurrentDictionary<int, string>();
public static async Task populateCache()
Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×4
concurrency ×2
.net-4.0 ×1
.net-4.5 ×1
asp.net ×1
asp.net-mvc ×1
foreach ×1
linq ×1
list ×1