我应该用 run(){} 包围哪些代码?
我完成了一个 2000 行 Swing + SQL 程序,我想在初始化所有内容之前向其中添加一个登录窗口。登录窗口是一个 JFrame
类,从主程序实例化。
所以我的主应用程序需要实现 Runnable。我想要用它创建的线程等待 - 使用 wait()
- 直到登录线程完成 - 并使用 notify()
- 。
我的程序由数十个可视组件、方法、main()
、构造函数、初始化程序等组成。我应该使用 run(){}
包围的最少代码量是多少?
这是我想做的一个例子。这绝不是正确的(我想),但你会明白的:
private void initialize() { // Called from main()
this.setBounds(100, 200, 1024, 576);
this.setTitle("Main app");
this.setVisible(false);
Runnable runnable = new Visual_Login();
Thread login_thread = new Thread(runnable);
login_thread.run();
main_thread.wait();
this.setVisible(true);
this.setContentPane(getJContentPane());
}
(...我希望我很好地理解并发性)
提前谢谢你。
I finished a 2000-line Swing + SQL program and I'd like to add to it a login window before everything is initialized. The login window is a JFrame
class, instanced from the main program.
So my main app needs to implement Runnable
. All I want with the thread I'm creating out of it is waiting -with wait()
- until the login thread finishes -and uses notify()
-.
My program consist of dozens of visual components, methods, main()
, constructor, initializer, etc. What's the minimum amount of code I should surround with run(){}
?
This is an example of what I want to do. It's not by any means correct (I suppose) but you'll get it:
private void initialize() { // Called from main()
this.setBounds(100, 200, 1024, 576);
this.setTitle("Main app");
this.setVisible(false);
Runnable runnable = new Visual_Login();
Thread login_thread = new Thread(runnable);
login_thread.run();
main_thread.wait();
this.setVisible(true);
this.setContentPane(getJContentPane());
}
(...I hope I'm understanding concurrency well)
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里不需要另一个线程。
在登录操作结束时,如果登录成功,则创建主 jframe 类的实例并处理登录 jframe
You don't need another thread here.
At the end of login action, if the login is successful create an instance of your main jframe class and dispose the login jframe
不确定我是否得到了您想要实现的目标,但是...如果您想在后台运行
initialize
那么您需要:要运行它,您可以选择执行以下操作:
或者
...不能真正走得更远比这更重要的是因为剩下的真的取决于你的逻辑。
最后,不值得在这里使用线程,除非您的
initialize
方法执行了一些需要花费大量时间冻结应用程序其余部分的操作。initialize
似乎除了设置一些 SWING 属性之外没有做任何事情。Not sure if I get what you want to achieve but ... if you want run
initialize
in background then you need:To run it you can optionally do:
or
... can't really go further than this because the rest really depends on your logic.
And just to finish, it is not worth to use a thread here unless your
initialize
method does something that takes considerable amount of time freezing the rest of the app. It doesn't seem thatinitialize
is doing anything but to set some SWING properties.您使用 启动一个线程 code>start() 方法而不是
run()
方法。 Thread 类在正确配置自身后将调用 run 方法。在你的情况下,它应该是:
除此之外,如果你打算阻止应用程序,那么为什么你需要启动另一个线程?
You start a thread with the
start()
method and not therun()
method. The Thread class will call the run method when it has properly configured itself.In your case it should be:
Other than that if you're planning to block the application anyway then why do you need to start another thread?