• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Animation with Thread.sleep()  RSS feed

 
Daniele Barell
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm studying Head First Java. At the chapter on GUI there is an example of simple animation made via Thread.sleep() within a loop.
It's more or less like the following:



It doesn't work. Actually, when startAnim() is called no animation succeded but the JFrame freezes for a while and then the ball go off at the final position.
What goes wrong?
Is there a way to produce the animation?
 
Dave Tolls
Ranch Foreman
Posts: 3001
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing (we'd need a bit more code) that the Thread.sleep() above is actually being called on the EDT.

The EDT is the Swing gui thread (Event Dispatch Thread), and causing that to sleep would cause the GUI to lock up.
 
Daniele Barell
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave.
Thanks for the answer!

The whole code is the following:


I cannot understand if Thread is called by EDT. Is it?
What's the possible solution in your opinion?
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniele Barell wrote:
I cannot understand if Thread is called by EDT. Is it?
What's the possible solution in your opinion?


All Swing callbacks are called by the event dispatching thread. So, make sure all swing callbacks are short, and returns in a reasonable time. Otherwise, the GUI will be starved as the event dispatching thread will not have enough CPU to handle all the events.

The solution, of course, is to *not* use the EDT. Use a different thread. Or if you need / want to use the EDT, don't sleep() -- instead, do one cycle, and schedule the next one.

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3001
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your case above, the event called is the ActionEvent for the button.
All the code in that listener is executed on the EDT, including the sleep.

Are you sure that's the code as presented in the book?
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, you should not use Thread.sleep(). For periodic background tasks, use java.util.concurrent.ScheduledExecutorService, or if you need to perform periodic tasks on Swing components, use javax.swing.Timer.
 
Daniele Barell
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:In your case above, the event called is the ActionEvent for the button.
All the code in that listener is executed on the EDT, including the sleep.

Are you sure that's the code as presented in the book?


Well...actually it was simpler: The animation wasn't called by the button, it started as the JFrame was visualized.
I myself added a bit of interaction. 
You mean that if I take the button away the animation works??


In general, you should not use Thread.sleep(). For periodic background tasks, use java.util.concurrent.ScheduledExecutorService, or if you need to perform periodic tasks on Swing components, use javax.swing.Timer.


Yeah. I made a second animation version with  javax.swing.Timer and it works.
Only I tried to understand why the previous version taken by the book didn't...
 
Rob Camick
Ranch Foreman
Posts: 2821
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to understand Threads.

Swing has its own special Thread called the Event Dispatch Thread (EDT), which responds to mouse and keyboard events etc.

When you invoked code from within a listener the code executes on the EDT. If you tell the EDT to sleep, then it can no longer respond to events and the GUI can't repaint itself.

I myself added a bit of interaction.


So you added the problem.

Read the section from the Swing tutorial on Concurrency for more information.

So the moral of the story is first get the demo code working. Then you make a change. If is stops working then you know the problem is your change. It makes it easier to problem solve when you know what code is causing the problem. If you don't understand why it causes a problem, then at least when you ask a question you can tell us what you changed.
 
Daniele Barell
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the moral of the story is first get the demo code working. Then you make a change. If is stops working then you know the problem is your change. It makes it easier to problem solve when you know what code is causing the problem. If you don't understand why it causes a problem, then at least when you ask a question you can tell us what you changed.


Ok Rob.
I'm just beginning to learn.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!