• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sleep() and repaint()

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code portion
for(int i=0;i<10;i++)
{ repaint();
Thread.sleep(1000);
}
what I expect this code to do is repaint then wait for 1 second and repaint again. But this code never goes to the paint method at all. it waits till the for loop is execited ie. for 10 secondes and then repaints. No intermediate steps are displayed.
The same code with a print statement instead of a repaint() works perfectly.
Can anyone explain why this happens, and how to get around it.
TY
viren
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Viren,
You are close! What you are doing is you are calling repaint() which launches a new thread then you are placing that thread to sleep because Thread.sleep is static (i.e., it executes on the currently running thread).
Since you want your thread to sleep and the paint one to work you need to call repaint after the sleep method.

Regards,
Manfred.
 
viren vaz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred,
Thanks for replying,
but, it still does the same thing.
Heres the actual code structure now
for(......)
{ try
{ Thread.sleep(1000);
}catch(.....)
t.calculateNext();
}
in class of object t
public void calculateNext()
{ //some zanny calculations
.
.
.
//and the last line before the return
repaint();
}

Now according to me the added function call shouldn't make a difference, but it does somewhere, and like all programmers I'm learning patience.
another clarification. I thought, and this is what the Complete Reference led me to believe, that the thread in which sleep() is called goes to sleep, and not the currently running thread. Is this wrong, or is it a half truth??
Also would it make a difference if I acquired the main thread and explicitly put it to sleep.
Thread mainThread=currentThread();
for(...)
{ try
{ mainThread.sleep(1000)
}catch(){}
calculateNext();
//which calls repaint() as the last statement
}
thanks again Manfred,
viren
ps. Nope I just tried that last bit, and it did the same thing.
[This message has been edited by viren vaz (edited August 10, 2001).]
 
viren vaz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem seems to be putting the repaint() in the for loop.
I've removed the sleep(), but it still doesnt work. paint() is not called till the for loop completes.
Viren
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic