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.

this might be an option

public static void pauze(){
     while(!isContinue()){
           //keep the duration small, so the game will be resumed shortly after 
          // the user presses resume
           sleep(500);
     }
}

public static boolean isContinue(){
boolean returnVal = false;
// this method checks in the rest of your code, whether or not your user pressed resume
//returnVal = ....

return returnVal;
}

this might be an option

public static void pauze(){
     while(!isContinue()){
           //keep the duration small, so the game will be resumed shortly after 
          // the user presses resume
           sleep(500);
     }
}

public static boolean 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..

import java.util.logging.Level;
import java.util.logging.Logger;
public class PauseThreadDemo extends Thread{
    boolean isStop = false;
    boolean isCompleted = false;
    int i = 0;

    void setIsStop(boolean value){
        isStop = value;
    }
    boolean chkIsStop(){
        return isStop;
    }

    public void run(){
        while(!isCompleted){
            if(chkIsStop()){
                while(chkIsStop()){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(PauseThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            else{
                System.out.println("i = " + ++i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(PauseThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
                }
                if(i==10){
                    isCompleted=true;
                }
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {

		System.out.println("Main Thread Started..");

        PauseThreadDemo threadPauseDemo = new PauseThreadDemo();
        //Joined the Thread with the MAIN Thread
        try {
            threadPauseDemo.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(PauseThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
        }
        threadPauseDemo.start();
        System.out.println("Class Thread Started..");

        //Now after thread MAIN stop for 3 Seconds, we send the command to "threadPauseDemo" for waiting.
        Thread.sleep(3000);
        threadPauseDemo.setIsStop(true);
        System.out.println("Class thread stoped for 3 second..");

        //Now again after 3 seconds of MAIN thread, we send the command to "threadPauseDemo" to continue.
        Thread.sleep(3000);
        threadPauseDemo.setIsStop(false);
        System.out.println("Class thread Continues..");
    }
}

Most welcome for any type of Queries and Questionnaires and Suggestion.

<<snip>>

Edited 6 Years Ago by Nick Evan: Removed plug + mail

How about the wait() method?
The API doc says: Causes the current thread to wait until ...

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)

or you could also try ...

Thread.sleep(2 years);
letSHopeTheOriginalPosterStillReadsAndNeeds(this);

if you have a new angle, or a good approach to solve a problem, create a new thread, don't revive threads that haven't been posted in for two years

To it will better

You thinks that is a better way? Looping and checking is a waste of CPU.

will be difficult to implement

What about your code being complicated and difficult.

This article has been dead for over six months. Start a new discussion instead.