multithreading 线程池与线程生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2049948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread Pool vs Thread Spawning
提问by reonze
Can someone list some comparison points between Thread Spawning vs Thread Pooling, which one is better? Please consider the .NET framework as a reference implementation that supports both.
有人可以列出线程生成与线程池之间的一些比较点,哪个更好?请考虑将 .NET 框架作为支持两者的参考实现。
回答by Hans Passant
Thread pool threads are much cheaper than a regular Thread, they pool the system resources required for threads. But they have a number of limitations that may make them unfit:
线程池线程比普通线程便宜得多,它们将线程所需的系统资源池化。但是它们有许多限制可能使它们不适合:
- You cannot abort a threadpool thread
- There is no easy way to detect that a threadpool completed, no Thread.Join()
- There is no easy way to marshal exceptions from a threadpool thread
- You cannot display any kind of UI on a threadpool thread beyond a message box
- A threadpool thread should not run longer than a few seconds
- A threadpool thread should not block for a long time
- 您不能中止线程池线程
- 没有简单的方法来检测线程池是否完成,没有 Thread.Join()
- 没有简单的方法可以从线程池线程中封送异常
- 您不能在消息框之外的线程池线程上显示任何类型的 UI
- 线程池线程的运行时间不应超过几秒钟
- 线程池线程不应长时间阻塞
The latter two constraints are a side-effect of the threadpool scheduler, it tries to limit the number of active threads to the number of cores your CPU has available. This can cause long delays if you schedule many long running threads that block often.
后两个约束是线程池调度程序的副作用,它试图将活动线程的数量限制为您的 CPU 可用的内核数量。如果您调度许多经常阻塞的长时间运行的线程,这可能会导致长时间的延迟。
Many other threadpool implementations have similar constraints, give or take.
许多其他线程池实现也有类似的约束,给予或接受。
回答by jldupont
A "pool" contains a list of available "threads" ready to be used whereas "spawning" refers to actually creating a new thread.
“池”包含准备使用的可用“线程”列表,而“生成”是指实际创建一个新线程。
The usefulness of "Thread Pooling" lies in "lower time-to-use": creation time overhead is avoided.
“线程池”的用处在于“更低的使用时间”:避免了创建时间开销。
In terms of "which one is better": it depends. If the creation-time overhead is a problem use Thread-pooling. This is a common problem in environments where lots of "short-lived tasks" need to be performed.
在“哪个更好”方面:这取决于。如果创建时间开销是一个问题,请使用线程池。在需要执行大量“短期任务”的环境中,这是一个常见问题。
As pointed out by other folks, there is a "management overhead" for Thread-Pooling: this is minimal if properly implemented. E.g. limiting the number of threads in the pool is trivial.
正如其他人所指出的,线程池有一个“管理开销”:如果正确实施,这是最小的。例如,限制池中的线程数是微不足道的。
回答by danben
For some definition of "better", you generally want to go with a thread pool. Without knowing what your use case is, consider that with a thread pool, you have a fixed number of threads which can all be created at startup or can be created on demand (but the number of threads cannot exceed the size of the pool). If a task is submitted and no thread is available, it is put into a queue until there is a thread free to handle it.
对于“更好”的某些定义,您通常希望使用线程池。在不知道您的用例是什么的情况下,请考虑使用线程池,您有固定数量的线程,这些线程都可以在启动时创建或按需创建(但线程数不能超过池的大小)。如果一个任务被提交并且没有可用的线程,它就会被放入一个队列,直到有一个空闲的线程来处理它。
If you are spawning threads in response to requests or some other kind of trigger, you run the risk of depleting all your resources as there is nothing to cap the amount of threads created.
如果您正在生成线程以响应请求或某种其他类型的触发器,您将面临耗尽所有资源的风险,因为没有任何东西可以限制创建的线程数量。
Another benefit to thread pooling is reuse - the same threads are used over and over to handle different tasks, rather than having to create a new thread each time.
线程池的另一个好处是重用——重复使用相同的线程来处理不同的任务,而不必每次都创建一个新线程。
As pointed out by others, if you have a small number of tasks that will run for a long time, this would negate the benefits gained by avoiding frequent thread creation (since you would not need to create a ton of threads anyway).
正如其他人指出的那样,如果您有少量任务将运行很长时间,这将抵消避免频繁创建线程所带来的好处(因为无论如何您都不需要创建大量线程)。
回答by Wim Hollebrandse
All depends on your scenario. Creating new threads is resource intensive and an expensive operation. Most very short asynchronous operations (less than a few seconds max) could make use of the thread pool.
一切都取决于您的场景。创建新线程是一种资源密集型和昂贵的操作。大多数非常短的异步操作(最多少于几秒)都可以使用线程池。
For longer running operations that you want to run in the background, you'd typically create (spawn) your own thread. (Ab)using a platform/runtime built-in threadpool for long running operations could lead to nasty forms of deadlocks etc.
对于您想要在后台运行的更长时间运行的操作,您通常会创建(产生)自己的线程。(Ab) 使用平台/运行时内置线程池进行长时间运行的操作可能会导致令人讨厌的死锁等形式。
回答by dicroce
My feeling is that you should start just by creating a thread as needed... If the performance of this is OK, then you're done. If at some point, you detect that you need lower latency around thread creation you can generally drop in a thread pool without breaking anything...
我的感觉是你应该从根据需要创建一个线程开始......如果这个性能没问题,那么你就完成了。如果在某个时候,您发现在创建线程时需要更低的延迟,您通常可以将其放入线程池中而不会破坏任何东西......
回答by Michael Bray
The main difference is that a ThreadPool maintains a set of threads that are already spun-up and available for use, because starting a new thread can be expensive processor-wise.
主要区别在于 ThreadPool 维护一组已经启动并可供使用的线程,因为启动新线程在处理器方面可能会很昂贵。
Note however that even a ThreadPool needs to "spawn" threads... it usually depends on workload - if there is a lot of work to be done, a good threadpool will spin up new threads to handle the load based on configuration and system resources.
但是请注意,即使是 ThreadPool 也需要“产生”线程......它通常取决于工作量 - 如果有很多工作要做,一个好的线程池会根据配置和系统资源启动新线程来处理负载.
回答by winwaed
Thread pooling is usually considered better, because the threads are created up front, and used as required. Therefore, if you are using a lot of threads for relatively short tasks, it can be a lot faster. This is because they are saved for future use and are not destroyed and later re-created.
线程池通常被认为更好,因为线程是预先创建的,并根据需要使用。因此,如果您将大量线程用于相对较短的任务,则速度会快很多。这是因为它们被保存以备将来使用,并且不会被销毁并在以后重新创建。
In contrast, if you only need 2-3 threads and they will only be created once, then this will be better. This is because you do not gain from caching existing threads for future use, and you are not creating extra threads which might not be used.
相比之下,如果你只需要2-3个线程并且它们只会被创建一次,那么这会更好。这是因为您不会从缓存现有线程以供将来使用中获益,并且您不会创建可能不会使用的额外线程。
回答by Jeff Cyr
It depends on what you want to execute on the other thread.
这取决于你想在另一个线程上执行什么。
For short task it is better to use a thread pool, for long task it may be better to spawn a new thread as it could starve the thread pool for other tasks.
对于短任务,最好使用线程池,对于长任务,生成一个新线程可能会更好,因为它可能会使其他任务的线程池饿死。
回答by Kelly S. French
This answeris a good summary but just in case, here is the link to Wikipedia:
这个答案是一个很好的总结,但为了以防万一,这里是维基百科的链接:
回答by Kees van Dieren
For Multi threaded execution combined with getting return values from the execution, or an easy way to detect that a threadpool has completed, java Callables could be used.
对于多线程执行结合从执行中获取返回值,或者检测线程池已完成的简单方法,可以使用 java Callables。
See https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6for more info.
有关更多信息,请参阅https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6。