gpt4 book ai didi

c# - 使用线程池或线程

转载 作者:太空狗 更新时间:2023-10-30 00:56:31 25 4
gpt4 key购买 nike

我有 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. 第三种方法。手动创建线程并加入它们。

在执行这些操作时,我不希望我的应用程序挂起。我希望它处理和保存数据,并完成,但不影响前台进程,整个应用程序。

最好的方法是什么?

我应该使用哪一个?有更好的方法吗?

最佳答案

Since each collection goes to a different storage, I dont want to combine them or save to same place.

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.

在线程池中排队三个线程。

关于c# - 使用线程池或线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7519392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com