实现 java FixTreadPool 状态监听器

发布于 2024-09-01 08:41:46 字数 383 浏览 2 评论 0原文

它是关于一个应该处理(VAD、响度、削波)大量声音文件(例如 100k)的应用程序。此时,我创建尽可能多的工作线程(可调用线程)放入内存中,然后使用 threadPool.invokeAll() 运行所有线程,将结果写入文件系统,卸载已处理的文件并继续步骤 1。事实上,它是一个带有 GUI 的应用程序,我不希望用户在处理所有声音文件时感觉该应用程序“没有响应”。 (此时它会导致 invokeAll 阻塞)。我不确定解决这个问题的“好”方法是什么。用户在处理时不可能做其他事情,但我想显示一个进度条,例如“100000 个声音文件中的 10 个已完成”。那么我该如何到达那里呢?我是否必须创建一个“观察者线程”,以便每个工作人员都对其进行回调?我对多线程很陌生,不明白这种机制的想法。

如果您需要知道:我正在使用 SWT/JFace。

It's about an application which is supposed to process (VAD, Loudness, Clipping) a lot of soundfiles (e.g. 100k). At this time, I create as many worker threads (callables) as I can put into memory, and then run all with a threadPool.invokeAll(), write results to file system, unload processed files and continue at step 1. Due to the fact it's an app with a GUI, i don't want to user to feel like the app "is not responding" while processing all soundfiles. (which it does at this time cause invokeAll is blocking). I'm not sure what is a "good" way to fix this. It shall not be possible for the user to do other things while processing, but I'd like to show a progress bar like "10 of 100000 soundfiles are done". So how do I get there? Do I have to create a "watcher thread", so that every worker hold a callback on it? I'm quite new to multi threading, and don't get the idea of such a mechanism.

If you need to know: I'm using SWT/JFace.

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

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

发布评论

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

评论(2

抱猫软卧 2024-09-08 08:41:46

您可以使用 ExecutorCompletionService 为此目的;如果您在循环中提交每个 Callable 任务,则可以调用完成服务的 take 方法 - 在任务完成时一次接收一个任务。每次您接受任务时,您都可以更新您的 GUI。

作为另一种选择,您可以实现自己的 ExecutorService,它也是一个 Observable,允许在任务执行时向订阅的 Observer 发布更新。完全的。

You could use an ExecutorCompletionService for this purpose; if you submit each of the Callable tasks in a loop, you can then call the take method of the completion service - receiving tasks one at a time as they finish. Every time you take a task, you can update your GUI.

As another option, you could implement your own ExecutorService that is also an Observable, allowing the publication of updates to subscribing Observers whenever a task is completed.

想你的星星会说话 2024-09-08 08:41:46

您应该看看 SwingWorker。这是一个很好的类,可以进行长时间的操作,同时向 GUI 报告进度并保持响应式 GUI。

使用 Swing Worker 线程 提供了一些很好的信息。

You should have a look at SwingWorker. It's a good class for doing lengthy operations whilst reporting back progress to the gui and maintaining a responsive gui.

Using a Swing Worker Thread provides some good information.

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