Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Thread.sleep() in while loop not working

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    38
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Thread.sleep() in while loop not working

    I need some help with the following:

    The idea is that it keeps looping until the state = false. then i need to restart it once state is true again, but i have a Thread.sleep(50) in there that seems to stop it from restarting..... any idea why or how i can get around this.

    goFlag is a boolean variable that keeps the outer loop looping so it can keep checking the state variable.

    So when state = false the System.out.println() stops which is correct but when the state = true again it doesnt restart.

    If i comment out the try catch and Thread.sleep() method it works as expected


    boolean goFlag = true;
    boolean state = true;
     
    while(goFlag)
    {
          while(state)
          {
                 for(int i = 0; i < 10; i++)
                 {
                              System.out.println("looping");
                              try
                              {
                                    Thread.sleep(50);
                               }
                               catch(InterruptedException e)
                               {
                                        System.exit(0);
                               }
                 }
          }
     
     
     
    }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Posts
    4,569
    Thanks
    136
    Thanked 701 Times in 655 Posts
    Blog Entries
    5

    Default Re: Thread.sleep() in while loop not working

    Can you clarify a but as to what behavior you are getting, and what is the behavior you want? (an SSCCE might help as well). Are any exceptions caught and the program exits?

  3. #3
    Member
    Join Date
    Nov 2009
    Posts
    38
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Thread.sleep() in while loop not working

    Quote Originally Posted by copeg View Post
    Can you clarify a but as to what behavior you are getting, and what is the behavior you want? (an SSCCE might help as well). Are any exceptions caught and the program exits?
    Sorry, when i change 'state' to false it stops printing out (which is correct), but when then changing 'state' back to true (so it restarts printing) it actually doesnt restart.....


    This is just an example code block that replicates the error within the program i'm writing.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,760
    Thanks
    21
    Thanked 579 Times in 526 Posts
    Blog Entries
    18

    Default Re: Thread.sleep() in while loop not working

    I'm guessing it's because of the compiler's optimization steps.

    Try declaring state and goFlag as volatile. If that doesn't work, please post more complete code so we can try it out and see what's wrong.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    mds1256 (January 13th, 2011)

  6. #5
    Member
    Join Date
    Nov 2009
    Posts
    38
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Thread.sleep() in while loop not working

    Quote Originally Posted by helloworld922 View Post
    I'm guessing it's because of the compiler's optimization steps.

    Try declaring state and goFlag as volatile. If that doesn't work, please post more complete code so we can try it out and see what's wrong.
    Thats it!

    Thanks!

Similar Threads

  1. [SOLVED] Loop Breaking Not Working
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2010, 09:06 PM
  2. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM
  3. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 09:05 AM
  4. A thread as game loop
    By maikeru in forum Threads
    Replies: 0
    Last Post: December 25th, 2009, 08:01 PM