-2

I need to run an infinite loop until the user tells it to break. For example...

while(true)
{
   someFunction();
   Thread.sleep(4000);
}

As you can see I want to continuously run the function every 4 seconds. But how do I get out of the loop when I want it to stop?

Thanks in advance!

  • it is nice to ask questions on forums/sites like this when you have troubles, but a question like this can be answered by Google much faster. Try googleing the headline of your thread here + java for example and you will see tons of good answers. – Vegard Jun 01 '13 at 17:04
  • Vegard - thanks BUT I don't think you are getting what I need... of course I've been trying to find the answer on google for several hours but can't and that's why I'm here. Once again, I need it to continue looping UNTIL user wants to break it. I think Junad may offer an answer but not sure yet. – user2443634 Jun 01 '13 at 17:29
  • if these are not answers to your question I think you need to rewrite your question, is your question perhaps how to read user input and transform it to a boolean value? – Vegard Jun 01 '13 at 17:34
  • OK perhaps you could tell me this... how do I have that loop above running uninterrupted, and have the program concurrently be checking to see if user has inputted a string into the System? – user2443634 Jun 01 '13 at 17:41
  • make a new thread that has a shared boolean variable. for each runthru of the loop check the status of the boolean variable. but now the question suddenly became much more complex, I suggest reposting the question in a new thread with a more in depth description of what the problem is, and provide some sample code of what you have tried. – Vegard Jun 01 '13 at 17:46
  • thanks yes that's exactly what I had in mind, but I suppose my issue is not knowing multithreading well enough (coming to java from c++), I'll do some more digging and start a new thread if need be. – user2443634 Jun 01 '13 at 18:37

5 Answers5

1

someFunction can be boolean? if so

if (someFunction()) break;
Vegard
  • 1,802
  • 2
  • 21
  • 34
  • 3
    Yes, but I would at least personally much rather see `while(someFunction()) Thread.sleep(4000);` – lc. Jun 01 '13 at 16:55
  • I agree, but since the question is something he could have Googled very quickly I did not elaborate too much on best practice and just answered the question. – Vegard Jun 01 '13 at 16:57
  • No, I need it to continue looping without user input UNLESS user wants to break it. – user2443634 Jun 01 '13 at 17:08
  • @lc s suggestion will work just fine, just make sure the function returns false once the user enters whatever should end the loop – Vegard Jun 01 '13 at 17:10
1

You have two options, you can use break; or add a boolean to your while loop

1:

while(userWantsToKeppGoing)
{
    someFunction();
    Thread.sleep(4000);
}

2:

while(true)
{
    if(userDoesntWantToKeepGoing) break;
    someFunction();
    Thread.sleep(4000);
}
ug_
  • 11,267
  • 2
  • 35
  • 52
0

Use a static volatile variable in while and set it from other thread when you want to stop the thread:

while(Someclass.staticVariable)
{
   someFunction();
   Thread.sleep(4000);
}

from the other thread to break this loop

Someclass.staticVariable = false;
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

you can use a global boolean variable as flag, that someFunction() can change, and do something like:

while(globalFlag)
{
   someFunction();
   Thread.sleep(4000);
}
BeNdErR
  • 17,471
  • 21
  • 72
  • 103
0

Check out the break statement.

Have a look at this SO q/a.

Community
  • 1
  • 1
Ohad Dan
  • 1,929
  • 5
  • 17
  • 22