Java wait for JFrame to finish(Java 等待 JFrame 完成)
问题描述
我有一个登录框架,我必须等待另一个线程.登录成功后,框架会自行处理.我想弹出应用程序的主框架.现在我正在观察一个布尔值来确定何时启动主框架.这样做的正确方法是什么?观察布尔值并不优雅.
I have a login frame that i have to wait for from another thread. Upon successful login frame disposes itself. And i want to pop up the main frame for the application. Right now i am watching a boolean value to determine when to fire up the main frame. What is the correct way of doing this? Watching a boolean value just does not feel elegant.
推荐答案
如果你有 Java 5 或更高版本,你可以使用 CountDownLatch.例如,假设主机最初处于控制状态,让主机创建倒计时为 1 的 CountDownLatch
并将此锁存器传递给登录帧.然后让主框架等待latch变为0:
If you have Java 5 or later available, you could use a CountDownLatch. For example, assuming the main frame is in control initially, have the main frame create the CountDownLatch
with a count down of 1 and pass this latch to the login frame. Then have the main frame wait for the latch to become 0:
CountDownLatch loginSignal = new CountDownLatch(1);
... // Initialize login frame, giving it loginSignal
... // execute login frame in another Thread
// This await() will block until we are logged in:
loginSignal.await();
拥有登录框架,完成后,减少锁存器:
Have the login frame, when done, decrement the Latch:
loginSignal.countDown();
确保您的登录框架无法在忘记减少闩锁的情况下退出!一旦 CountDownLatch
达到 0,主框架就变为可运行的.
Ensure that there is no way for your login frame to exit where it forgets to decrement the latch! As soon as the CountDownLatch
reaches 0, the main frame becomes runnable.
您也可以使用 信号量
或 Condition
(或 java.util.concurrent
中的任何其他选择),但为此目的,CountDownLatch
似乎更容易使用.
You could also use a Semaphore
or Condition
(or any of a few other choices from java.util.concurrent
), but for this purpose, a CountDownLatch
seems easier to use.
这篇关于Java 等待 JFrame 完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 等待 JFrame 完成
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01