Thread.Sleep() is freezing

后端 未结 3 694
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 08:30

Here is a snippet

kit.insertHTML(doc, doc.getLength(), \"Hello\", 0, 0, null);
try{
Thread.sleep(1000);
}catch(Exception e){}

I am using

3条回答
  •  萌比男神i
    2020-11-27 09:11

    Thread.sleep is a long running task. When you a running such a task in the EDT it blocks all repaint requests from being executed. All repaint requests which are pending and which were sent during the sleep phase are queued for future processing.

    As a result when the EDT comes out of the sleep phase it coalesce all such repaint request (if coalescing is enabled which is the default property) into a single repaint which gets executed. If coalescing is not enabled then all queued request are executed serially without any time gap in between. As a result it seems that the UI did not update.

    To correct the situation use a SwingTimer which triggers periodically after specific intervals of time, or a SwingWorker.

提交回复
热议问题