有没有一种简单的方法来判断 Java 执行器在给定时刻正在运行哪些任务?

发布于 2024-10-10 09:32:13 字数 181 浏览 7 评论 0原文

我确信我可以将一些东西组合在一起,这将使我能够解决这个问题,但我希望有一个我所缺少的开箱即用的解决方案。我阅读了文档,但没有看到任何内容。

我的特定应用程序使用由 DelayQueue 支持的 ThreadPoolExecutor,尽管我不确定这是否重要。

谢谢!

I'm sure I could hack something together which would enable me to figure this out, but I'm hoping there's an out-of-the-box solution I'm just missing. I read the docs but I didn't see anything.

My specific app is using a ThreadPoolExecutor backed by a DelayQueue, although I'm not sure that matters.

Thanks!

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

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

发布评论

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

评论(3

非要怀念 2024-10-17 09:32:13

一种简单的方法:用代码包装您的任务,该代码将调用回调信号通知新任务的开始,并传递您想要的所有数据:

interface StartTaskWatcher {
    void taskStarted(Object data);
}

class StartTaskSignalingWrapper implements Runnable {
    private final Runnable task;
    private final String taskDescription;
    private final StartTaskWatcher startTaskWatcher;

    StartTaskSignalingWrapper(Runnable task, String taskDescription, StartTaskWatcher startTaskWatcher) {
        this.task = task;
        this.taskDescription = taskDescription;
        this.startTaskWatcher = startTaskWatcher;
    }

    public void run() {
        startTaskWatcher.taskStarted(taskDescription);
        task.run();
    }
}

One simple approach: wrap your tasks with code which will call callback signalling start of new task, passing along all data you will want it to:

interface StartTaskWatcher {
    void taskStarted(Object data);
}

class StartTaskSignalingWrapper implements Runnable {
    private final Runnable task;
    private final String taskDescription;
    private final StartTaskWatcher startTaskWatcher;

    StartTaskSignalingWrapper(Runnable task, String taskDescription, StartTaskWatcher startTaskWatcher) {
        this.task = task;
        this.taskDescription = taskDescription;
        this.startTaskWatcher = startTaskWatcher;
    }

    public void run() {
        startTaskWatcher.taskStarted(taskDescription);
        task.run();
    }
}
故人如初 2024-10-17 09:32:13

我认为没有可靠的方法可以做到这一点。如果您想要计数,可以使用可用的方法(getTaskCount()/getCompletedTaskCount())来帮助。如果您需要实际任务,那么据我所知,您需要手动跟踪任务,通过覆盖 beforeExecute() 和 afterExecute() 是一种方法。

I don't think there is a reliable way to do this. If you want counts there are methods available (getTaskCount()/getCompletedTaskCount()) which help. If you need the actual tasks then AFAIK you need manually to keep track of the tasks, by overriding beforeExecute() and afterExecute() is one way.

我爱人 2024-10-17 09:32:13

您实际上需要在运行时知道,还是只是为了调试?

如果您只是进行调试,请考虑查看 JVisualVM 应用程序(安装在 JDK 的“bin”目录中)。它允许您动态检查所有正在运行的线程,因此它应该能够为您提供一些见解。

(JVisualVM 还可以执行 CPU/内存分析、执行和分配热点,并实时显示垃圾收集活动。)

Do you actually need to know at runtime, or just for debugging?

If you're just debugging, consider taking a look at the JVisualVM application (installed in the 'bin' directory of the JDK). It lets you dynamically inspect all running threads, so it should be able to provide you with some insight.

(The JVisualVM can also perform CPU/Memory profiling, with execution & allocation hotspots, and shows garbage collection activity in realtime.)

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