I have a loop which need to be turned off so that it can continue to next statements with some values else it crash. What is the best way to do so? For example:
if (data.startsWith("tcp") )
{
/* main.strings should have a value but it may take a while */
main.start("i generate some encoding/decoding, i take uncertain time...");
int i = 0;
while(main.strings != null)
{
/* Maximum 20 time waiting is accepted, else not */
if (i > 20 ) break;
Thread.sleep(1000);
i++;
}
/* It crash, if main.start() is not creating the main.strings
* so there is a uncertain specific time, for this but we however can't wait long
*/
main.NowUDPSend(main.strings);
...
}
Is this the best way to handle the realtime while loop, so that i can exit exactly after 20 times limit over to the next.
will do it, but I would really look into using
main.wait(20000)and have the thing that setsmain.stringsdomain.notifyAll()afterwards.See http://download.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html for a good tutorial.