使用线程池或线程
我有 1 个集合,我将其内容保存到持久存储中。然后我最终得到了 3 个集合,我喜欢将它们保存到持久存储中。
起初我使用 ThreadPool 来保存一个集合,但现在我有 3 个集合。由于每个集合都进入不同的存储,我不想将它们组合起来或保存到同一个地方。
我的问题是我应该使用手动线程并为每个 Save() 方法创建一个线程,还是应该为每个方法创建 3 个线程池,或者我应该在一个 ThreadPool.QueueUserWorkItem 调用中调用所有 3 个方法。
1.第一种方法
ThreadPool.QueueUserWorkItem(o =>
{ Save<Foo>(ConcurrentCollectionStorage.Bus1);
Save<Bar>(ConcurrentCollectionStorage.Bus2);
Save<Car>(ConcurrentCollectionStorage.Bus3);
});
2.第二种方法
ThreadPool.QueueUserWorkItem(o =>
{ Save<Foo>ConcurrentCollectionStorage.Bus); });
ThreadPool.QueueUserWorkItem(o =>
{ Save<Bar>(ConcurrentCollectionStorage.Bus2); });
ThreadPool.QueueUserWorkItem(o =>
{ Save<Car>(ConcurrentCollectionStorage.Bus3); });
3. 第三种方法。手动创建线程并加入它们。
在执行这些操作时,我不希望我的应用程序挂起。我希望它能够处理和保存数据,并完成,但不影响前台进程,整个应用程序。
最好的方法是什么?
我应该使用哪一个?有更好的方法吗?
I had 1 collection which I was saving the content to a persistent storage. Then I ended up having 3 collections which i like to save to a persistent storage.
At first I was using a ThreadPool to save one collection, but now I have 3 collections.Since each collection goes to a different storage, I dont want to combine them or save to same place.
My question is should i use manual Threads and create one thread for each Save() method or should I create 3 thread pool for each method, or should i call all the 3 methods in one ThreadPool.QueueUserWorkItem call.
1.First Approach
ThreadPool.QueueUserWorkItem(o =>
{ Save<Foo>(ConcurrentCollectionStorage.Bus1);
Save<Bar>(ConcurrentCollectionStorage.Bus2);
Save<Car>(ConcurrentCollectionStorage.Bus3);
});
2. Second Approach
ThreadPool.QueueUserWorkItem(o =>
{ Save<Foo>ConcurrentCollectionStorage.Bus); });
ThreadPool.QueueUserWorkItem(o =>
{ Save<Bar>(ConcurrentCollectionStorage.Bus2); });
ThreadPool.QueueUserWorkItem(o =>
{ Save<Car>(ConcurrentCollectionStorage.Bus3); });
3. Third Approach. Creating Thread Manually and join them.
While doing these operations I dont want my application to hang. I want it to process and save the data, and complete, but not to affect the foreground processes, the whole application.
What s the best way to do this?
Which one should I use? Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将三个线程放入线程池中。
Queue three threads in the thread pool.
使用.NET 4.0,您可以使用任务并行库:
但是,从您的问题中不清楚关键瓶颈是什么。如果您的瓶颈是磁盘 IO 或网络延迟,那么 TPL 无法帮助您。
如果您希望主线程等待它们全部完成,您可以这样做(有多种方法可以做到这一点):
这样,运行任务的线程将等待它们完成。 t1、t2 和 t3 将异步运行。
With .NET 4.0 you can use the Task Parallel Library:
However, it isn't clear from your question what the critical bottleneck is. The TPL can't help you if you're bottleneck is Disk IO or network latency.
If you want your main thread to wait for them all to complete, you can do this (there are several ways to do it):
This way the thread running the tasks will wait until they finish. t1,t2, and t3 will be running asynchronously.
如果您使用 .net 4,您应该使用 TASK 而不是调用 ThreadPool API。
如果您的保存操作很短,请按原样使用。如果它很长,这就是您考虑手动线程的原因,那么您应该使用任务,但将其标记为“长时间运行”
HTH
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 that is why you are considering manual thread then you should use task but mark it as "long running"
HTH