954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I pause a thread indefinitely?

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.

esy928
Light Poster
42 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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;
}
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

thanks =) ill give it a try

esy928
Light Poster
42 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

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.[INDENT]<>
[/INDENT]

ankurgecr
Newbie Poster
5 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
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)

ankurgecr
Newbie Poster
5 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You