Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

delay in setVisible( true )

 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to simulate dice rolling as follows:

You Rolled:

< 2 seconds pass >

You Rolled: 3

< 1 more second passes >

You Rolled: 3 + 4 = 7

Instead, the whole line displays at once after 3 seconds. Please tell me what is wrong.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call rollDice() from an event handler, then the event thread -- which is also responsible for painting the screen -- is blocked as long as rollDice() runs. Nothing can change on the screen until the event thread becomes free.

For this reason, you should never do anything that takes longer than a few milliseconds on the event thread. If you need to start a long-running operation from the GUI, always do it by spawning a new thread -- i.e.,

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic