在 Java 中创建一个新线程的成本有多高?我们什么时候应该考虑使用线程池?

发布于 2024-09-08 15:51:52 字数 90 浏览 6 评论 0原文

我想知道应该使用线程池的界限在哪里。在不使用线程池的情况下,每秒可以创建多少个新线程,仍然可以避免明显的性能损失?

是否有任何可观察的开源线程池实现?

I wonder where is the verge after which a thread pool should be used. How many new threads per second can I create without using a thread pool still avoiding noticeable performance penalty?

Are there any observable open-source thread-pool implementations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-09-15 15:51:52

考虑到成本,唯一有效的答复是亲自测试它(不是那么优雅的方式告诉你我从未做过这样的测试,也永远不会这样做,因为现代执行机制提供了更先进的创建/销毁机制) 。

考虑到现有的实现,Java 现代版本(从 Java 5 开始)提供了 ThreadPoolExecutor 结合了线程池的优点和 java.util.concurrent 的最现代概念:执行器。

此外,我永远不会建议你忘记线程并用 Runnable、callable 和其他更高级的计算对象来代替它们。这样,您就可以轻松切换 Executor 的实现。

Considering the cost, the only valid reply is to test it for yourself (not-so-elegant way to tell you I have never done such a test, and will never do it, as modern Execution mechanism provides far advanced creation/destruction mechanisms).

Consdering existing implementations, Java modern versions (starting with Java 5) offers various subclasses of ThreadPoolExecutor that combines the benefits of a thread pool with the most modern concepts of java.util.concurrent : Executors.

Besides, I would never recommand enough to you to forget about Threads and to repalce them with Runnable, callable and other more advanced computation objects. This way, you can easily switch implementation of Executors.

追我者格杀勿论 2024-09-15 15:51:52

您应该始终使用线程池。不仅仅是为了性能,还为了 java.util.concurrent 包为您提供的易用性。在 Java 5 及更高版本中,内置了线程池。

不要考虑“线程”,而是使用 Executor 接口来执行您需要执行的任务。创建新的线程池非常简单:

Executor executor = Executors.newFixedThreadPool(5);

有关 java.util.concurrent 包的完整文档位于此处:
http://java.sun.com /javase/6/docs/api/java/util/concurrent/package-frame.html

You should always use a thread pool. Not just for performance, but for the ease of use the java.util.concurrent package gives you. With Java 5 and later, thread pooling is built in.

Instead of thinking in terms of 'threads', use the Executor interface to execute tasks you need to be performed. Creating a new thread pool is as simple as:

Executor executor = Executors.newFixedThreadPool(5);

Full documentation on the java.util.concurrent package is here:
http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-frame.html

笑梦风尘 2024-09-15 15:51:52

无论平台如何,创建线程总是很昂贵的。创建线程的实际时间取决于操作系统。

如果您经常创建短期线程,我建议使用线程池。例如,如果应用程序的主要任务是为此类短期任务提供服务,那么线程池会非常好。

如果您创建太多线程,线程池也很好。线程之间的上下文切换也会降低性能。长话短说,在现代四核 PC 架构上,您的目标应该是同时运行的线程不超过 200 个。

Creating threads is always expensive, regardless of the platform. Actual time for creating a thread depends on the OS.

I would recommend using a thread pool if you create short-lived threads very often. For example, if the primary task of your application is to service such short-lived tasks, a thread pool would be very nice.

A thread pool is also nice if you create too many threads. Context switching between threads can kill performance, too. Long story short, on contemporary quad-core PC architecture you should aim to have no more than 200 threads running a the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文