如何在Java中使用信号量
我有一个需要一段时间并想要显示等待屏幕的函数:
Loading.showSplash("Working...");
for (FileListRow row : model.getList()) {
performAction(row);
}
Loading.hideSplash();
问题是 PerformAction(row);似乎是在加载屏幕之前执行的,因此它达不到目的。
解决此问题的任何帮助都可以强制等待屏幕在执行其余部分之前显示,并强制该功能在等待屏幕消失之前完成。
I have a function that takes a while and want to display a waiting screen :
Loading.showSplash("Working...");
for (FileListRow row : model.getList()) {
performAction(row);
}
Loading.hideSplash();
The problem is that the performAction(row); seems to be executed before the loading screen and hence it defeat the purpose.
Any help on solving this to force the waiting screen to show before the rest is executed and to force the function to finish before the waiting screen goes away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以假设这是一个 Swing 程序吗?
如果是,那么看起来您正在事件调度线程上执行长时间运行的操作。这是一个坏主意;您应该在工作线程上执行所有长时间运行的操作。有关详细信息,请参阅 Java 教程。
如果您确实在后台线程上执行长时间运行的操作,那么您肯定不想使用信号量来挂起 GUI 线程,直到这些操作完成,甚至直到它们开始。应允许 GUI 线程自由运行并分派事件,否则您的 UI 将出现“滞后”,这会让用户感到烦恼。正如其他发帖者所指出的,您的工作线程可以使用
SwingUtilities.invokeLater()
来更新 UI。Can I assume this is a Swing program?
If yes, then it looks like you're performing your long-running operations on the event dispatch thread. This is a bad idea; you should perform all long-running operations on a workert hread. See the Java tutorial for more information.
And if you do perform long-running operations on a background thread, your definitely do not want to use a semaphore to suspend the GUI thread until those operations complete -- or even until they start. The GUI thread should be allowed to run freely and dispatch events, or your UI will have "lags," which are annoying to users. As other posters have indicated, your worker thread can use
SwingUtilities.invokeLater()
to update the UI.尝试一下。您应该从事件调度线程 (EDT) 调用执行 GUI 更新的所有代码。 Swing 不是线程安全的。
Try giving this a shot. You should call all code that does GUI updates from the Event Dispatch Thread (EDT). Swing is not thread safe.
我假设您正在开发一个 Swing 应用程序。
阅读 SwingWorker 类的 javadoc : http://download .oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html。
这个想法是
1.显示你的启动画面
2. 启动一个线程,在后台执行所有操作。
3. 当后台操作完成后,隐藏启动屏幕
SwingWorker 会处理这个问题。但请注意,后台操作可能不使用任何与 Swing 相关的组件或类,因为它们不是线程安全的并且必须在事件分派线程 (EDT) 中执行。
另一种更简单的方法是显示启动屏幕,然后使用 SwingUtilities.invokeLater 执行操作并隐藏启动屏幕。这样,一切都将在 EDT 上完成,但启动屏幕将在执行其他操作之前显示。但请注意,使用此技术时,应用程序 GUI 将无响应,直到隐藏启动屏幕为止。
I'll assume you are developing a Swing application.
Read the javadoc of the SwingWorker class : http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html.
The idea is to
1. display your splash screen
2. start a thread which performs all your actions in the background.
3. when the background operations are finished, hide the splash screen
The SwingWorker takes care of this. Note however that the background operations may not use any Swing-related component or class, since they're not thread-safe and must execute in the event dispatch thread (EDT).
Another, simpler approach would be to display your splash screen, and then use SwingUtilities.invokeLater to perform your operations and hide the splash screen. This way, everything will be done on the EDT, but the splash screen will be displayed before the other operations are performed. Note however that with this technique, the application GUI will be unresponsive until the splash screen is hidden.