【发布时间】:2014-12-03 00:58:06
【问题描述】:
我目前正在做一些带有一些动画的 GUI。我最终自己编写了标签的颜色变化。为此,我实施了以下任务:
Task tk= new Task() {
@Override protected Void call() throws Exception
{
colorAnimation=true;
boolean red = true;
long timer = System.currentTimeMillis();
long waitTime= 1000;
nome.setStyle("-fx-background-color: red;");
main: while(colorAnimation)
{
while(System.currentTimeMillis() < waitTime+ timer)
{
System.out.println(System.currentTimeMillis()-timer);
if(!colorAnimation)
break main;
}
timer=System.currentTimeMillis();
red=!red;
if(red)
{
nome.setStyle("-fx-background-color: red;");
waitTime=1000;
}
else
{
nome.setStyle("-fx-background-color: black;");
waitTime=500;
}
}
nome.setStyle("-fx-background-color: black;");
return null;
}
};
Thread t = new Thread(tk);
t.setDaemon(true);
t.start();
上面的函数应该初始化标签颜色为红色,等待1秒,改成黑色,等待半秒再改成红色,重复这个过程,直到colorAnimation关闭(声明为volatile)。问题是该函数被卡在第二个while内,并且在控制台打印等待时间(System.currentTimeMillis()-timer)被卡在982。如果我将初始等待时间更改为500,那么打印的等待时间卡在 469。我真的不知道发生了什么。
谢谢!
【问题讨论】: