简体   繁体   中英

What is the meaning of “quick” in Swing's Timers?

From the Java tutorial :

Note that the Swing timer's task is performed in the event dispatch thread. This means that the task can safely manipulate components, but it also means that the task should execute quickly. If the task might take a while to execute, then consider using a SwingWorker instead of or in addition to the timer.

What does it mean by "quickly"? I mean that is not exact, less than a minute is quickly or what?

for example if I want to make an animation(1 minute) with some panels, via moving them, change their transparency and...and the user is just going to see the panels and is not going to work with them (no I/O), now is Timer a good idea for such situation ?

This means just that - it's supposed to be fast, as in - little to no visible latency .

However, later in the page they say that if you have a complicated and concurrent application, look into the SwingWorker class and know about concurrency in regards to swing .

The concern is not necessarily how quickly everything runs, but that it's in the correct order.

In SwingWorker, you'll read that there are two main concerns in multi-threaded Swing apps:

  • Time-consuming tasks should not be run on the Event Dispatch Thread. Otherwise the application becomes unresponsive.

    Swing components should be accessed on the Event Dispatch Thread only.

To answer your specific question.

If I want to make an animation(1 minute) with some panels, via moving them, change their transparency and...and the user is just going to see the panels and is not going to work with them (no I/O), now is Timer a good idea for such situation ?

Yes, a Timer is required for a smooth animation.

Let's assume you want to display 40 frames per second. That means you have 25 milliseconds to prepare and display each panel.

Obviously, the more calculations you can move outside the animation loop, the better. It only takes a few milliseconds on modern personal computers to render a panel. Calculating the panel, can easily take more than 25 milliseconds.

So, for a minute of animation, you need 2,400 panels. Pre-calculating these panels, and just rendering them, will lead to a smoother animation.

There is no fixed time limit for quick in this context. However, you should assume that any task taking more than a few dozens of milliseconds is usually not considered as quick. Be aware that any code that runs on the EDT can create all sorts of problems with your application's GUI. That's why the documentation suggest you use a SwingWorker instead.

It also depends on the delay of your Timer . If it runs every 50ms, then a task which runs for 30ms is way to slow. However, if your task runs every 5-10 minutes for only 30ms, then it can be acceptable. You will have to run some tests to see if your Timer has any visible impact on your application.

Check this article for more information on the EDT. Also, this answer provides a lot of information on how to use SwingWorker objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM