Java中的线程池的负载平衡

发布于 2025-02-06 14:52:33 字数 387 浏览 0 评论 0原文

免责声明:这与这个

有些任务可以同时消耗网络和CPU。有时性能会粘在网络中,有时会粘在CPU中。因此,根据我需要在程序运行之前无法精确设置的不同线程号。理想情况下,每个任务都应记录其时间(以及其他一些参数),如果时间增加 - >当任务更快地完成时,线程池会减小线程 - >线程的数量应增加。

当然,应该有防止连续负载平衡的保护:例如,当系统添加1个线程并将其删除时,然后一次又一次地重复此操作,因为任务时间会发生变化。

Java提供与此类似的东西吗?

Disclaimer: this is not the same as this one.

There are some tasks that can consume both network and CPU. Sometimes performance sticks into a network, sometimes into the CPU. So depending on that I need different thread number that can not be precisely set before a program run. Ideally, each task should log its time (and some other parameters) and if time increases -> thread pool decreases threads otherwise when a task is completed faster -> number of threads should be increasing.

Of course, there should be protection from continuous load balancing: e.g. when the system adds 1 thread and remove it, then repeat this again and again because task time changes.

Does java offer something similar to this?

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

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

发布评论

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

评论(1

无声静候 2025-02-13 14:52:33

目前在Java,线程非常昂贵,直接映射到主机OS线程一对一。它们会影响内存使用量和CPU使用情况。因此,我们通常将线程数限制为少数几个,通常大约是核心的数量。因此,如今,增加/减小线程池的大小不太可能是有意义的。

project loom中的虚拟线程

听起来您的场景是 firtual threads ( fibers )来自 project loom project。许多虚拟线程映射到单个主机OS线程。

在今天的Java线程中,如果Java代码块,则主机OS线程块。该线程没有进一步的工作。使用虚拟线程,当Java代码阻止其分配的主机OS线程并“停放”时,虚拟线程被卸下。当该代码最终返回,因此需要进一步执行时,将其安装到另一个主机OS线程上。与主机OS线程的阻止/解密相比,虚拟线的停车和安装速度要快得多。虚拟线程对内存和CPU的影响要小得多。因此,我们可以在常规硬件上同时运行数千甚至数百万的线程。

在您的工作负载可能上升或下降的情况下,Project Loom中的虚拟线程设施将自动管理线程池和调度。您应该看到大大改善的吞吐量,而无需付出任何努力。

一些警告:

  • 便宜的线程仍然可以做昂贵的事情。因此,您可能需要管理或限制您的特定并发任务,以免吹出内存或增加其他资源。
  • 虚拟线程仅对于阻止的任务才有意义。这意味着最常见的爪哇工作。但是,对于完全限制的任务,例如几乎没有日志记录的视频编码/解码,请存储I/O,网络I/O等。您会坚持使用常规的Java线程。
  • 在某些情况下,您的任务中特定内容可能会阻止停车。您可以选择稍微更改代码,以使虚拟线程从“固定”到主机OS线程。尤其是织机的初始版本尤其如此。现在,这种情况在释放前织机中已经流畅,因此我们需要了解变化。

虚拟线程和其他项目织机功能可作为Java 19中的预览功能可用,现已提供实验性构建。

有关更多信息,请参阅项目织机团队(例如Ron Pressler和Alan Bateman)的文章,演讲和访谈。

Currently in Java, threads are quite expensive, being mapped directly one-to-one to host OS threads. They impact both memory usage and CPU usage. So we usually limit the number of threads to a few, often roughly the number of cores or so. So increasing/decreasing the size of the thread pool is not likely to make sense nowadays.

Virtual threads in Project Loom

Sounds like your scenario is ideal for the virtual threads (fibers) coming from the Project Loom project. Many virtual threads are mapped to a single host OS thread.

In today's Java threading, if the Java code blocks, the host OS thread blocks. No further work is performed on that thread. With virtual threads, when the Java code blocks the virtual thread is dismounted from its assigned host OS thread, and “parked”. When that code eventually returns, and therefore needs further execution, it is mounted onto another host OS thread. This parking and mounting of virtual threads is much faster than blocking/unblocking of host OS threads. Virtual threads have much less impact on memory and and CPU. So we can have thousands, or even millions, of threads running simultaneously on conventional hardware.

In your scenario where your work load may go up or down, the virtual thread facility in Project Loom will automatically manage the thread pool and scheduling. You should see vastly improved throughput with no effort on your part.

A few caveats:

  • Cheap threads can still do expensive things. So you may need to manage or limit your particular concurrent tasks to avoid blowing out memory or overburdening other resources.
  • Virtual threads only make sense for tasks that block. That means most common Java work. But for entirely CPU bound tasks such as video encoding/decoding with little to no logging, storage I/O, network I/O, etc. you would stick with conventional Java threads.
  • There may be some situations where the particular content in your task may prevent the parking while blocked. You may choose to alter your code a bit to enable the virtual thread from being “pinned” to the host OS thread. This may be especially the case with the initial releases of Loom. This situation is fluid right now in pre-release Loom, so we will need to stay informed as to changes.

Virtual threads and other Project Loom features are available as preview features in Java 19, with experimental builds available now.

For more information, see the articles, presentations, and interviews by members of the Project Loom team such as Ron Pressler and Alan Bateman.

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