Is there a .Net equivalent to java.util.concurrent

2020-07-03 06:21发布

问题:

Have a long running set of discrete tasks: parsing 10s of thousands of lines from a text file, hydrating into objects, manipulating, and persisting.

If I were implementing this in Java, I suppose I might add a new task to an Executor for each line in the file or task per X lines (i.e. chunks).

For .Net, which is what I am using, I'm not so sure. I have a suspicion maybe CCR might be appropriate here, but I'm not familiar enough with it, which is why I pose this question.

Can CCR function in an equivalent fashion to Java Executors, or is there something else available?

Thanks

回答1:

You may want to look at the Task Parallel Library.

As of C# 5 this is built into the language using the async and await keywords.



回答2:

If you're going to ask a bunch of .NET people what's closest to being equivalent to Java Excecutors, it might not hurt to describe the distinguishing features of Java Executors. The person who knows your answer may not be any more familiar with Java than you are with .NET.

That said, if the already-mentioned Task Parallel Library is overkill for your needs, or you don't want to wait for .NET 4.0, perhaps ThreadPool.QueueUserWorkItem() would be what you're looking for.



回答3:

Maybe this is related: Design: Task Parallel Library explored. See 10-4 Episode 6: Parallel Extensions as a quick intro.

For older thread-based approach, there's ThreadPool for pooling.



回答4:

The BackgroundWorker class is probably what you're looking for. As the name implies, it allows you to run background tasks, with automatically managed pooling, and status update events.



回答5:

For anyone looking for a more contemporary solution (as I was), check out the EventLoopScheduler class.