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
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.
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
Have you tried a for loop: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
while(true) { for() { This loop will b executed every time } }
Don't use a infinite while loop. Use a boolean that you can control instead.
JUNKY,
i need those threads to keep on running so i got to use infinite while loop
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.
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 }
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.