【发布时间】:2011-01-03 07:24:05
【问题描述】:
我有这样的事情:
for(int i=0; i<5; i++){
mytextarea.setText("hello " + i);
try{
Thread.currentThread().sleep(1000); //to give time for users to read
} catch(Exception e){}
}
我希望它会在文本区域显示“hello 0”,等待 1 秒,然后显示“hello 1”,然后等待 1 秒,等等。
但发生的情况不同,它等待 5 秒,然后显示“hello 4”。
有什么想法吗?
【问题讨论】:
-
sleep是一个静态方法。最好使用Thread.sleep(..)而不是Thread.currentThread().sleep(..)。两者在语法上都是正确的。
标签: java user-interface multithreading