943,696 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6773
  • Java RSS
Aug 25th, 2008
0

How do I pause a thread indefinitely?

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
esy928 is offline Offline
42 posts
since Feb 2008
Aug 25th, 2008
0

Re: How do I pause a thread indefinitely?

this might be an option
Java Syntax (Toggle Plain Text)
  1. public static void pauze(){
  2. while(!isContinue()){
  3. //keep the duration small, so the game will be resumed shortly after
  4. // the user presses resume
  5. sleep(500);
  6. }
  7. }
  8.  
  9. public static boolean isContinue(){
  10. boolean returnVal = false;
  11. // this method checks in the rest of your code, whether or not your user pressed resume
  12. //returnVal = ....
  13.  
  14. return returnVal;
  15. }
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007
Aug 25th, 2008
0

Re: How do I pause a thread indefinitely?

thanks =) ill give it a try
Reputation Points: 10
Solved Threads: 0
Light Poster
esy928 is offline Offline
42 posts
since Feb 2008
Aug 25th, 2008
0

Re: How do I pause a thread indefinitely?

Click to Expand / Collapse  Quote originally posted by stultuske ...
this might be an option
Java Syntax (Toggle Plain Text)
  1. public static void pauze(){
  2. while(!isContinue()){
  3. //keep the duration small, so the game will be resumed shortly after
  4. // the user presses resume
  5. sleep(500);
  6. }
  7. }
  8.  
  9. public static boolean isContinue(){
  10. boolean returnVal = false;
  11. // this method checks in the rest of your code, whether or not your user pressed resume
  12. //returnVal = ....
  13.  
  14. return returnVal;
  15. }
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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Sep 14th, 2010
0

Logic to Pause Thread in JAVA

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..
Java Syntax (Toggle Plain Text)
  1. import java.util.logging.Level;
  2. import java.util.logging.Logger;
  3. public class PauseThreadDemo extends Thread{
  4. boolean isStop = false;
  5. boolean isCompleted = false;
  6. int i = 0;
  7.  
  8. void setIsStop(boolean value){
  9. isStop = value;
  10. }
  11. boolean chkIsStop(){
  12. return isStop;
  13. }
  14.  
  15. public void run(){
  16. while(!isCompleted){
  17. if(chkIsStop()){
  18. while(chkIsStop()){
  19. try {
  20. Thread.sleep(100);
  21. } catch (InterruptedException ex) {
  22. Logger.getLogger(PauseThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
  23. }
  24. }
  25. }
  26. else{
  27. System.out.println("i = " + ++i);
  28. try {
  29. Thread.sleep(1000);
  30. } catch (InterruptedException ex) {
  31. Logger.getLogger(PauseThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
  32. }
  33. if(i==10){
  34. isCompleted=true;
  35. }
  36. }
  37. }
  38. }
  39. public static void main(String[] args) throws InterruptedException {
  40.  
  41. System.out.println("Main Thread Started..");
  42.  
  43. PauseThreadDemo threadPauseDemo = new PauseThreadDemo();
  44. //Joined the Thread with the MAIN Thread
  45. try {
  46. threadPauseDemo.join();
  47. } catch (InterruptedException ex) {
  48. Logger.getLogger(PauseThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
  49. }
  50. threadPauseDemo.start();
  51. System.out.println("Class Thread Started..");
  52.  
  53. //Now after thread MAIN stop for 3 Seconds, we send the command to "threadPauseDemo" for waiting.
  54. Thread.sleep(3000);
  55. threadPauseDemo.setIsStop(true);
  56. System.out.println("Class thread stoped for 3 second..");
  57.  
  58. //Now again after 3 seconds of MAIN thread, we send the command to "threadPauseDemo" to continue.
  59. Thread.sleep(3000);
  60. threadPauseDemo.setIsStop(false);
  61. System.out.println("Class thread Continues..");
  62. }
  63. }
Most welcome for any type of Queries and Questionnaires and Suggestion.

<<snip>>
Last edited by Nick Evan; Sep 15th, 2010 at 4:37 am. Reason: Removed plug + mail
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ankurgecr is offline Offline
5 posts
since Sep 2010
Sep 14th, 2010
0
Re: How do I pause a thread indefinitely?
How about the wait() method?
The API doc says: Causes the current thread to wait until ...
Reputation Points: 925
Solved Threads: 504
Posting Expert
NormR1 is offline Offline
5,085 posts
since Jun 2010
Sep 14th, 2010
0

Reply: Using wait() method

Click to Expand / Collapse  Quote originally posted by NormR1 ...
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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ankurgecr is offline Offline
5 posts
since Sep 2010
Sep 14th, 2010
0
Re: How do I pause a thread indefinitely?
or you could also try ...
Java Syntax (Toggle Plain Text)
  1. Thread.sleep(2 years);
  2. 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
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007
Sep 14th, 2010
0
Re: How do I pause a thread indefinitely?
Quote ...
To it will better
You thinks that is a better way? Looping and checking is a waste of CPU.
Quote ...
will be difficult to implement
What about your code being complicated and difficult.
Reputation Points: 925
Solved Threads: 504
Posting Expert
NormR1 is offline Offline
5,085 posts
since Jun 2010

This thread is more than three months old

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.
Message:
Previous Thread in Java Forum Timeline: need of abstract class in java
Next Thread in Java Forum Timeline: how to hide toolbar in a pdfbox using java api





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC