How do I pause a thread indefinitely? im creating a game and i need a thread to be paused when the user pauses the game and the thread will resume when the user unpauses the game. =) im using thread.sleep right now but it requires an integer to determine how long it will pause, but since i dont know how long the user is gonna pause the game, i think that method wont do me any good.
//keep the duration small, so the game will be resumed shortly after
// the user presses resume
sleep(500);
}
}
publicstaticboolean isContinue(){
boolean returnVal = false;
// this method checks in the rest of your code, whether or not your user pressed resume
//returnVal = ....
return returnVal;
}
That's a very expensive method call. Your primary thread operation will have to wait for the calculation of isContinue() if you were to do it that way.
You may instead want to have a set method that sets the boolean expression in the loop to false so that the process can stop when it is desired for it to stop.
Actually there no ready maid method in Java to Pause the Thread directly by a single statement.
So, we have to do this by applying some Logic.
Here I apply that Run method chaked status of thread in every iteration of while loop untill all process has been completed And if in any iteration, PAUSE commnad is given than Current thread is sent in to sleep state for infinite time And when an invocation is given from MAIN thread (or Any other thread) the infinite look breaks and the Class thread continues to Run..
Look at this..
How about the wait() method?
The API doc says: Causes the current thread to wait until ...
Of Course you can use wait() method, but then you have to use notify() method to continue. And it will be difficult to implement Pause-Continue logic in program. To it will better to use and loop checking continuously before executing your main Logic (which you want to PAUSE) and implement the above logic (which i've posted)
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.