开发者

Negative Delays in Java Swing Timers

I just came across this in some code, and I'm rather confused.

 timer = new Timer(-10, new
         ActionListener()
         {
            //Do some stuff
  开发者_运维技巧       });

What is the significance of a negative (or zero) delay in a swing Timer object? According to the API, the first event is fired immediately, but what is the difference between, say, -10 and negative -100? In my code, they seem to function differently, but I can't quite pin down what it changes about the Timer object.


The javadoc says: "If delay is less than or equal to zero, the timer fires as soon as it is started.".

So the answer is that there should be no difference. The apparent difference you are seeing is probably just a consequence of non-determinacy in your application and / or the runtime system.

FOLLOWUP

So then is each subsequent event fired immediately after the one before it as well, with any apparent 'delay' just the product of the system?

The delay is probably just because of thread switch non-determinacy, and the fact that the Swing Timer uses one thread for executing the event listeners for all timers.

You should probably read the javadocs for java.swing.Timer and java.util.Timer as well as the additional documentation that they link to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜