Thread.Sleep() 冻结 [英] Thread.Sleep() is freezing

查看:51
本文介绍了Thread.Sleep() 冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个片段

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

我使用 HTMLEditorKit()HTMLDocument() 作为文本框.文本框应该显示你好"然后等待一秒钟,但是当我尝试这个时,它等待一秒钟然后输入你好"这个词,这不是我想要的.

I am using HTMLEditorKit() and HTMLDocument() as a textbox. The textbox should show "Hello" then wait one second however, when I try this, It waits one second then puts the word "Hello" which is not what I want.

我不确定为什么会发生这种情况,因为我按逻辑顺序排列了它.如果有人能帮我解决这个问题,那就太好了.

I am not sure why this happens because I put this in the logical order. If anyone can help me with this, that would be great.

有谁知道替代方案,以便我可以使用延迟"效果?

Does anyone know an alternative, so I can use the "delay" sort of effect?

推荐答案

永远不要从 Swing 事件线程中调用 Thread.sleep(...),因为这会使事件线程本身进入睡眠状态.由于该线程负责所有 Swing 绘制和用户交互,这将使您的应用程序进入睡眠状态.

Don't ever call Thread.sleep(...) from within the Swing event thread as this will put the event thread itself to sleep. Since this thread is responsible for all Swing painting and user interaction, this will put your application to sleep.

如果您想要的只是显示延迟,请考虑使用 摇摆计时器.

If all you want is a delay in the display, then consider use of a Swing Timer.

另一方面,如果您的事件线程受到长时间运行的任务的影响,则在后台执行该任务,使用 SwingWorker(如 Guillaume 1+ 向他建议的那样).

If on the other hand your event thread is being compromised by a long-running task, then do the task in the background, using a SwingWorker (as suggested by Guillaume 1+ to him).

这篇关于Thread.Sleep() 冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆