java-SwingWorker中的thread.sleep() [英] java - thread.sleep() within SwingWorker
本文介绍了java-SwingWorker中的thread.sleep()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道以下代码是否正确.我有一个SwingWorker,它可以执行某些操作,进行睡眠,执行其他操作并更新GUI.在SwingWorker中使用Thread.sleep可以吗?
I am just wondering if the following code is correct. I have a SwingWorker that does something, sleeps, does something else and updates GUI. Is it okay to use Thread.sleep inside SwingWorker?
class MySwingy extends SwingWorker<Void, Void> {
@Override
public Void doInBackground() {
//Do Something
try {
Thread.sleep(200);
} catch (Exception ex) {
}
//Do Something
}
@Override
public void done() {
//Update GUI
}
}
推荐答案
如果确实需要,则没有技术原因不能执行此操作.将阻塞的线程是一个后台线程,您的UI不会阻塞.
If you really need to, there is no technical reason why you can't do that. The thread that will block is a background thread and your UI will not block.
但是我想问一下为什么你需要在后台线程中睡觉吗?也许可以改进您的设计以消除这种需求?
But may I ask why you need to sleep in a background thread? Maybe your design can be improved to remove that need?
这篇关于java-SwingWorker中的thread.sleep()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文