使用线程和 ProcessBuilder
我真的不熟悉使用线程,所以我希望有人可以帮助我找出最好的方法来做到这一点。
我的 java 应用程序中有一个 JButton...当您单击该按钮时,我有一个进程生成器,它创建一个执行一些外部 python 代码的进程。 python 代码生成一些文件,这可能需要一些时间。 当 python 代码执行完毕后,我需要将这些文件加载到我的 Java 应用程序中的小程序中。
在当前的形式中,我在调用外部 python 文件的代码中有一个 p.waitFor()...因此,当您单击按钮时,按钮会挂起(实际上整个应用程序挂起),直到该过程完成。 显然,我希望用户能够在此过程进行时与应用程序的其余部分进行交互,但是一旦完成,我希望我的应用程序知道它,以便它可以将文件加载到小程序中。
做这个的最好方式是什么?
感谢您的帮助。
I am really unfamiliar with working with threads, so I was hoping someone could help me figure out the best way to do this.
I have a JButton in my java application...when you click on the button, I have a Process Builder that creates a process which executes some external python code. The python code generates some files, and this can take some time. When the python code is done executing, I need to load those files into an applet within my Java application.
In its current form, I have a p.waitFor() within the code that calls the external python file...so when you click on the button, the button hangs (the entire application hangs actually) until the process is done. Obviously, I want the user to be able to interact with the rest of the application while this process is going on, but as soon as it's done, I want my application to know about it, so that it can load the files into the applet.
What is the best way to do this?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 SwingWorker 来调用 Python后台线程上的进程。 这样,您的 UI 将在长时间运行的任务运行时保持响应。
You should use SwingWorker to invoke the Python process on a background thread. This way your UI will remain responsive whilst the long-running task runs.
您确实想创建一个新线程来监视新进程。 正如您所发现的,仅对 UI 使用一个线程并监视子进程将使 UI 在子进程运行时看起来挂起。
这是一些示例代码,假设存在 log4j 记录器,我认为这将说明一种可能的方法......
You really want to create a new thread for monitoring your new process. As you've discovered, using just one thread for both the UI and monitoring the child process will make the UI seem to hang while the child process runs.
Here's some example code that assumes the existence of a log4j logger which I think will illustrate one possible approach...