parallel-extensions

List<T> thread safety

烂漫一生 提交于 2019-12-17 07:33:23
问题 I am using the below code var processed = new List<Guid>(); Parallel.ForEach(items, item => { processed.Add(SomeProcessingFunc(item)); }); Is the above code thread safe? Is there a chance of processed list getting corrupted? Or should i use a lock before adding? var processed = new List<Guid>(); Parallel.ForEach(items, item => { lock(items.SyncRoot) processed.Add(SomeProcessingFunc(item)); }); thanks. 回答1: No! It is not safe at all, because processed.Add is not. You can do following: items

What does MaxDegreeOfParallelism do?

非 Y 不嫁゛ 提交于 2019-12-17 05:09:49
问题 I am using Parallel.ForEach and I am doing some database updates, now without setting MaxDegreeOfParallelism , a dual core processor machine results in sql client timeouts, where else quad core processor machine somehow does not timeout. Now I have no control over what kind of processor cores are available where my code runs, but is there some settings I can change with MaxDegreeOfParallelism that will probably run less operations simultaneously and not result in timeouts? I can increase

Execute single workflow instance in parallel

北慕城南 提交于 2019-12-12 17:24:27
问题 Let's say I have a workflow, and I want to execute it many times in parallel: System.Threading.Tasks.Parallel.For( 0, 100, i => WorkflowInvoker.Invoke( new Workflow1(), new Dictionary<string, object> { { "Num", i } })); I wonder, is it legal to execute it this way: var w = new Workflow1(); System.Threading.Tasks.Parallel.For( 0, 100, i => WorkflowInvoker.Invoke( w, new Dictionary<string, object> { { "Num", i } })); 回答1: Yes it is "legal" to execute a workflow definition that way. The workflow

Is multi-level ConcurrentDictionary still thread-safe?

痞子三分冷 提交于 2019-12-12 11:54:49
问题 I have four level data structure defined like this: Dictionary<Type1, Dictionary<Type2, Dictionary<Type3, List<Type4>>>> The whole thing is encapsulated in a class which also maintains thread-safety. Currently it just locks whole collection while it reads/manipulates the data (reading is by orders of magnitude more common than writing). I was thinking of replacing the Dictionary with ConcurrentDictionary and List with ConcurrentBag (its items don't have to be ordered). If I do so, can I just

System.Threading.ThreadAbortException fired in new thread

别等时光非礼了梦想. 提交于 2019-12-11 03:14:34
问题 I am currently working in .net c# 4.0 and have come across an issue with some code that I written that is causing me some headaches. I am using the System.Threading.Tasks.TaskFactory class in conjunction with System.Threading.Tasks.TaskScheduler to start a new thread in my console application where the function of the thread is to check if an item has been added to a queue. When an item is added to the queue, it processes it. So the queue contains emails to be sent and once an email is added

Can I configure the number of threads used by Parallel Extensions?

淺唱寂寞╮ 提交于 2019-12-11 02:48:14
问题 I'm currently using the Parallel Extensions that are part of Reactive Extensions for .Net(Rx). I believe they are also available through .Net 4 beta releases. 1) Is there any way to determine how many threads are actually being utilized. I assume this is related to Environment.ProcessorCount (# of logical cores) but I want to check this. 2) Is there any way of configuring how many thread you wish to use? I noticed there is a ParallelOptions.MaxDegreeOfParallelism property that looks promising

How to make PLINQ to spawn more concurrent threads in .NET 4.0 beta 2?

纵然是瞬间 提交于 2019-12-10 16:02:55
问题 In former versions of Parallel Extensions you could set the number of threads: enumerable.AsParallel(numberOfThreads) But now that overload is not available anymore. How to do it now? 回答1: In the new version you can specify it with the extension method ".WithDegreeOfParallelism(int degreeOfParallelism)". IE: enumerable.AsParallel().WithDegreeOfParallelism(numberOfThreads) 回答2: I really have no idea why it changed, so I can't answer the question, but it seems like if the developer specifies

What is the purpose of BlockingCollection(Of T)

爷,独闯天下 提交于 2019-12-08 14:36:09
问题 I´m trying to understand the purpose of BlockingCollection in the context of the new Parallel Stacks on .NET 4. The MSDN documentation says: BlockingCollection is used as a wrapper for an IProducerConsumerCollection instance, allowing removal attempts from the collection to block until data is available to be removed. Similarly, a BlockingCollection can be created to enforce an upper-bound on the number of data elements allowed in the IProducerConsumerCollection; addition attempts to the

In parallel call, limit executions per second

两盒软妹~` 提交于 2019-12-07 17:23:14
问题 Using TPL / Parallel.ForEach is there an out-of-the-box way to limit the number of times a method is called per unit of time (i.e. no more than 50 calls per second). This is different than limiting the number of threads. Perhaps there's some simple hack to make this work? 回答1: One solution is to make a thread-safe version of the following https://stackoverflow.com/a/7728872/356790 /// <summary> /// This class limits the number of requests (method calls, events fired, etc.) that can occur in a

Parallel Extensions Equivalent in Java

若如初见. 提交于 2019-12-07 14:11:50
问题 I have some experience using parallel extensions in .Net development, but I was looking at doing some work in Java that would benefit from an easy to use parallelism library. Does the JVM offer any comparable tools to parallel-extensions? 回答1: There are some limited support in java.util.concurrent package since java 5, but full supports are java 7 feature. 回答2: You should become familiar with the java.util.concurrent package. It has simple and robust primitives for parallel programming upon