• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stopping a loop without stopping execution?

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say i have a do-while loop which runs until some criteria is met. If it takes too long on some occasions for this criteria to be met then the only way i seem to be able to break out of it is by Ctrl+c , which stops the entire program running (It's only a command line program).

Is there a way to break out of this loop if i decide that on some occasions it is takeing to long to finish, whilst at the same time allowing the loop to take as long as it wants unless i try and stop it? I am tyring to implement a Genetic Algorithm you see...

Cheers
[ March 27, 2006: Message edited by: Sam Bluesman ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you comfortable with threads? You can run the long-running task on its own thread and check a boolean variable every time through the loop. Say your loop runs while there is more to do, and you'd like to tell it to stop before it runs out of things to do:

Now your main thread can set keepGoing to false and stop the loop. The main loop might use a timer to do this or keyboard input. Read up on the "volatile" keyword. I left it out but I bet you'll find you want it on the boolean.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a cases where you know the conditions is met -- some boolean condition made true/false some where else in program (with a different thread of execution), you can follow Stan's method of breaking out of loop. I think your case is mostly of CPU consuming task(with no blocking calls in code).

But there are cases, where you are blocked in JVM in following cases:
* Thread.sleep(x)
* Object.wait()
* Some OS specific blocking calls like IO, network sockets,etc.

In these cases, even if one detect a boolean condition, one cannot come out of the loop -- as calling thread is blocked.

One way out for such conditons is:
* Put the code for the long-time-consuming job in Runnable.run method
* Give this job to a Thread-Pool with a timeout parameter. Note down the Thread which is doing the job
* If thread pool does not support timeout of a task, write your timeout handler something like:
* Run a separate Timer task to monitor time for each long-task that has been submitted and after timeout
* Interrupt the blocked thread with proper checks

Hope this helps
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You can run the long-running task on its own thread and check a boolean variable every time through the loop.


This is known as the "busy/wait antipattern" - another clever euphemism designed to hide reality. A busy/wait is almost always the result of the inability (either technical limitation or limitations of the competence of the developer) to register for notification of a state change, and so a "poll" is performed for the state.

For example, you cannot be notified of a change to a file through the J2SE API Specification, and so a busy/wait is the only solution. An appropriate abstraction can be used to hide this detail so that if the API spec. ever does support it, clients can take advantage of it without breaking. This requires minimalisation (I prefer to call it hitting requirements without omission, addition or deviation).

Almost always a busy/wait is "fixed" with the use of Object wait/notify, however, in this case, it sounds like the need for a simple thread interrupt. If not, more information required...
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so it looks like i better read up on threads!

Cheers guys
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it sounds like the need for a simple thread interrupt.



Much better, indeed, since that's exactly what it was designed for. For some reason, Tony, that's always the last thing I think of. Gotta push that up the mental queue somehow.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic