实现 java FixTreadPool 状态监听器
它是关于一个应该处理(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
ExecutorCompletionService
为此目的;如果您在循环中提交每个Callable
任务,则可以调用完成服务的take
方法 - 在任务完成时一次接收一个任务。每次您接受任务时,您都可以更新您的 GUI。作为另一种选择,您可以实现自己的
ExecutorService
,它也是一个Observable
,允许在任务执行时向订阅的Observer
发布更新。完全的。You could use an
ExecutorCompletionService
for this purpose; if you submit each of theCallable
tasks in a loop, you can then call thetake
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 anObservable
, allowing the publication of updates to subscribingObserver
s whenever a task is completed.您应该看看
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.