Use ThreadPool or Thread

I had 1 collection in which I saved the contents to persistent storage. Then I got 3 collections that I like to keep them in permanent storage.

At first I used ThreadPool to save one collection, but now I have 3 collections. Since each collection goes to a different repository, I do not want to combine them or keep them in the same place.

My question is whether I should use manual threads and create one thread for each Save () method, or should I create 3 thread pools for each method, or should I call all 3 methods in one call to ThreadPool.QueueUserWorkItem.

1. The first approach

ThreadPool.QueueUserWorkItem(o => { Save<Foo>(ConcurrentCollectionStorage.Bus1); Save<Bar>(ConcurrentCollectionStorage.Bus2); Save<Car>(ConcurrentCollectionStorage.Bus3); }); 

2. The second approach

 ThreadPool.QueueUserWorkItem(o => { Save<Foo>ConcurrentCollectionStorage.Bus); }); ThreadPool.QueueUserWorkItem(o => { Save<Bar>(ConcurrentCollectionStorage.Bus2); }); ThreadPool.QueueUserWorkItem(o => { Save<Car>(ConcurrentCollectionStorage.Bus3); }); 

3. The third approach. Create a stream manually and join them.

Performing these operations, I do not want my application to freeze. I want it to process and save data and complete, but not affect the foreground processes, the entire application.

What is the best way to do this?

Which should i use? Is there a better way to do this?

+4
source share
3 answers

With .NET 4.0, you can use a parallel task library:

  Task.Factory.StartNew(() => { Save<Foo>(ConcurrentCollectionStorage.Bus1); }); Task.Factory.StartNew(() => { Save<Bar>(ConcurrentCollectionStorage.Bus2); }); Task.Factory.StartNew(() => { Save<Car>(ConcurrentCollectionStorage.Bus3); }); 

However, it is not clear from your question what the critical bottleneck is. TPL cannot help you if you are a bottleneck, is disk IO or network latency.

If you want your main thread to wait for them to complete, you can do this (there are several ways to do this):

  Task t1 = Task.Factory.StartNew(() => { Save<Foo>(ConcurrentCollectionStorage.Bus1); }); Task t2 = Task.Factory.StartNew(() => { Save<Bar>(ConcurrentCollectionStorage.Bus2); }); Task t3 = Task.Factory.StartNew(() => { Save<Car>(ConcurrentCollectionStorage.Bus3); }); Task.WaitAll(t1,t2,t3); 

Thus, the thread performing the tasks will wait until they end. t1, t2 and t3 will execute asynchronously.

+2
source

Since each collection goes to a different repository, I do not want to combine them or store them in the same place.

Performing these operations, I do not want my application to freeze. I want it to process and save data and complete, but not affect the foreground processes, the entire application.

Queue three threads in a thread pool.

+3
source

if you are using .net 4, you should use TASK instead of calling the ThreadPool API. If your save operation is short, use it as is. in case it is long, and therefore you are considering a manual thread, then you should use the task, but mark it as "long"

Hth

+1
source

All Articles