Java 线程创建跟踪

发布于 2024-10-21 13:33:38 字数 237 浏览 6 评论 0原文

我有一个 Java 应用程序,重点关注并发性和可突发容量。这意味着它使用线程池来排队和执行事件。

所有这些线程池都是固定大小的,但当应用程序部署在 Linux (CentOS 5.5) 服务器上时,我经常发现自己遇到打开文件描述符限制。

据我所知,应用程序在负载下时不应同时创建超过 20 个线程,但我的 ulimit 为 1024。

有什么方法可以跟踪这些线程回到创建它们的代码/池?

I have a java app with a heavy focus on concurrency and on burstable capacity. This means it uses thread pools to queue and execute events.

All these thread pools are fixed-size, but I constantly find myself bumping up against open file descriptor limits when the app is deployed on the Linux (CentOS 5.5) server.

To my count, the app, when under load, shouldn't create more than 20 threads at any one time, but I'm hitting a ulimit of 1024.

Is there any way I can track these threads back to the code/pool that created them?

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

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

发布评论

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

评论(1

小情绪 2024-10-28 13:33:38

健全的线程池应该(至少可选地)允许指定名称。

对于正常的 ThreadPoolExecutor 您需要实现 ThreadFactory 适当地命名线程。然后使用 setThreadFactory() 使其使用您的实现。

您还可以使用 Guava ThreadFactoryBuilder 并调用 setNameFormat() 来预先构建该功能:

ThreadPoolExecutor myExecutor = ...;
ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("myExecutorThread-%d").build();
myExecutor.setThreadFactory(tf);

A sane thread pool should (at least optionally) allow the name to be specified.

For the normal ThreadPoolExecutor you need to implement a ThreadFactory that names the threads appropriately. Then use setThreadFactory() to make it use your implementation.

You can also use the Guava ThreadFactoryBuilder and call setNameFormat() to get that functionality pre-built:

ThreadPoolExecutor myExecutor = ...;
ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("myExecutorThread-%d").build();
myExecutor.setThreadFactory(tf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文