Thread.sleep()方法被冻结 [英] Thread.Sleep() is freezing

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

问题描述

下面是一个片段

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

我使用一个HTMLEditorKit() HTMLDocument的()作为一个文本框。
文本框应该显示您好,然后等待1秒然而,当我尝试这一点,它会等待一秒钟把单词你好,这是不是我想要的。

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类(由纪尧姆的建议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天全站免登陆