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 8 of 8

Thread: while(true){ section of code runs 200 times}

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default while(true){ section of code runs 200 times}

    hi
    in my thread run() method,


    i need a particular section to wait till 200 iterations are done and ,that section of code is never executed again ..

    please suggest me the logic ..
    thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: while(true){ section of code runs 200 times}


  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: while(true){ section of code runs 200 times}

    while(true)
    {
     
    for()
    {
     
    This loop will b executed every time 
    }
     
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: while(true){ section of code runs 200 times}

    Don't use a infinite while loop. Use a boolean that you can control instead.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: while(true){ section of code runs 200 times}

    JUNKY,

    i need those threads to keep on running so i got to use infinite while loop

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: while(true){ section of code runs 200 times}

    Why not a for loop if you want 200 iterations?

    Can you write some code or pseudo code showing what you want to do?

    Perhaps this:
    int lpCnt = 0;
    while(true) {
      lpCnt++;  // count the loops
      if(lpCnt == 200) {
        // do only when 200th loop
      }
    ...
    }// end while()
    Last edited by Norm; June 22nd, 2011 at 01:04 PM.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: while(true){ section of code runs 200 times}

    Quote Originally Posted by jack_nutt View Post
    JUNKY,

    i need those threads to keep on running so i got to use infinite while loop
    If I understand you correctly you want a bunch of stuff to loop X number of times ONCE. So you can have your outer infinite loop for the thread and have an inner while loop controlled by a boolean.
    boolean onlyRunOnce = true;
    while(true) {
        // some code
        while(onlyRunOnce) {
            // code runs 200 times
            onlyRunOnce = false;
        }
        //some other code
    }

  8. #8
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: while(true){ section of code runs 200 times}

    Quote Originally Posted by jack_nutt View Post
    hi
    in my thread run() method,

    i need a particular section to wait till 200 iterations are done and ,that section of code is never executed again ..

    please suggest me the logic ..
    thanks
    This may be a bit late however, if you have a class that extends Thread for your run() create a method that inserts a boolean that starts the code you want to run. This may be one way to to wait the 200 iterations and not have the thread use the much needed processor time.

    Multi-threading is tricky at times but using interrupts to stop the thread to wait may be optional. A really good book for Threads is java concurrency check it out I am half way done with it.

Similar Threads

  1. while(true){ section of code runs only once}
    By jack_nutt in forum Java Theory & Questions
    Replies: 5
    Last Post: June 19th, 2011, 06:15 PM
  2. help with user selected section has no available seat applet
    By chonch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:39 AM
  3. Replies: 27
    Last Post: February 17th, 2011, 05:42 PM
  4. Does this still hold true?
    By April in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 2nd, 2010, 09:28 AM
  5. runs once only
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2009, 03:31 AM