java - thread.sleep() within SwingWorker

元气小坏坏 提交于 2019-12-11 17:34:21

问题


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
    }
}

回答1:


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?



来源:https://stackoverflow.com/questions/9625381/java-thread-sleep-within-swingworker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!