# [java] Help with Thread.Sleep() after render

## Recommended Posts

Hi all, I am working on a game where objects are scrolling down and up the display at fixed rate. So my "run" method looks like: I use the Sleep because although I am incrementing by 1 each time, it is very fast movement and I want it to be slower, or at least something which I can set in my game i.e. easy level = sleep more, advance level = sleep less = move faster. When objects are on the same Y index in the display I want the animation to freeze for more time, so the player can check this situation. So I tried to use two sleep_time values. One for regular small time sleep and the other for long period of sleep. The problem, it does not work. My guess is I am not using the sleep function in a right way. Can any one help ???? Thanks public void run() { long excess = 0L; long wait_time = 0; running = true; while(running) { wait_time = gameUpdate(); gameRender(); paintScreen(); if (wait_time == 1) sleepTime = 5000L; else sleepTime = 200L; wait_time = 0; if (sleepTime > 0) try { // Thread.sleep(sleepTime/100000L); // nano -> ms Thread.sleep(sleepTime); // nano -> ms } catch(InterruptedException ex) { ex.printStackTrace(); System.exit(1); } } // while System.exit(0); // so window disappears } // end of run()

##### Share on other sites
Try replacing the thread.sleep with a Object.wait. The reason why you do this is because your not specifying a thread with the other method so you might not be making the right thread sleep when you want it to. If you need to wake it up early just do a waiter.notify(). synchronized is fine because when the call to wait is done, the code block releases its lock.

Object waiter;synchronized (waiter) {try {waiter.wait(100);} catch (Exception ex) {}}

##### Share on other sites
Hi,

I have one thread which is the animator. I worked it out by assigning values to sleep based on x and y coords.

Tnx

Elisheva