如何检查进程的进度
我使用 java 开发了一个独立的应用程序,在其中使用运行时执行外部进程。但该过程的执行需要很多时间,我想显示它的进度监视器。
如何获取已使用 Java 运行时执行的进程的进度?我应该使用进度条吗?
I have developed a standalone application using java, in which I am executing an external process using Runtime. But the execution of that process takes much time, and I would like to display a progress monitor of it.
How can I get the progress of a process that has been executed using Java runtime? Should I use a Progress Bar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了能够“了解”流程的进度,您将需要实现流程随着进展而变化的某种里程碑。您必须实现该逻辑,以便外部进程将其进度通知您的应用程序。然而,这并不总是可能的(因为,首先您需要访问外部进程的代码)或容易做到。
您可以做的是使用选框进度条,这将使动画无限期地进行:
您可以查看这个 Java 教程(来自同一位置)我从哪里得到了上面的文字)以获取更多信息。
To be able to 'know' the progress of the process, you will need to implement some sort of milestones that the process changes as it goes along. You will have to implement that logic so that the external process notifies your application of its progress. However, this is not always possible (because, first of all you need access to the code of the external process) or simple to do.
What you can do, is to use a Marquee Progress bar, this will keep an animation going indefinitevely:
You can take a look at this Java tutorial (same place from where I got the above text) for more information.
您必须与外部进程通信才能获取其状态。如果外部进程不提供任何有关其状态的信息,您就无法完成您想要的任务。
You have to communicate with the external process to get its status. If the external process does not provide anything about its status, you could not accomplish what you want.
这只能通过读取进程的输出流(myProcess.getInputStream() 和 myProcess.getErrorStream())来实现,当然只有进程在输出上显示其进度时才可能。
有些进程仅在认为自己正在交互式 shell 中运行时才显示进度,因此您必须通过传递相应的环境参数来“模拟”shell(wget 是这些工具之一)。
当然,您必须手动解析进程的输出。
如果您的外部进程包括将数据从一个命令传输到另一个命令,则可以使用 pv(1) 命令,显示输出。
This is only possible by reading from the process's output streams (myProcess.getInputStream() and myProcess.getErrorStream()), and of course only if the process displays its progress on the output.
There are processes that only display progress when they believe they are running in an interactive shell, so you have to "emulate" a shell by passing the corresponding environment parameters (wget is one of those tools).
Of course you have to manually parse the process's output.
If your external process includes piping data from one command to another, you can use the pv(1) command, to display the output.
触及核心:如果您不知道且无法控制、测量或预测事务的持续时间,请使用
jProgressBar1.setIndependent(true);
To get to the core: if you do not know and can not control, measure or predict the duration of the transaction use
jProgressBar1.setIndeterminate(true);