Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Breaking out of forever loop

 
Bob Lawson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method running inside a thread that is doing some work and sleeping repeatedly. From a web page, I want the user to be able to tell it to start/stop. How do I do that?
 
Bob Lawson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, here is the code in its current state:


public void run() {

try {
Dao widgetDao = new WidgetDaoImpl();

// do forever
for (;;) {

// get widgets with a READY status
Collection<Widget> widgets = widgetDao.getReadyWidgets();

for(Widget widget : widgets) {

// set the status to PROCESSING
widgetDao.setWidgetState(widget.getId(), WidgetState.PROCESSING);

// Run the widget
modelSvc.runWidget(widget);

// set the status to COMPLETE
widgetDao.setWidgetState(widget.getId(), WidgetState.COMPLETE);
}

Thread.currentThread();
Thread.sleep(5000);

}

} catch (SQLException e) {
throw new RuntimeException("Failed ");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You loop will end when the Thread gets interrupted. So all you would have to do was interrupt the thread running the long loop:

That will cause an InterruptedException the next time the thread sleeps, and break out of the loop - ending the Thread.

Another way might be to introduce a boolean value which you check on in the infinite loop, and only keep running while it is true. You then use a method to set that value to false, which kills the thread. Perhaps something like this:


 
Bob Lawson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,

That is EXACTLY what I was looking for. Thank you SO MUCH!!

Bob
 
Bob Lawson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I thought it was exactly what I wanted. But now it turns out that it must run as a new thread, rather than run under the current thread as above. How would you do that?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Lawson wrote:Well, I thought it was exactly what I wanted. But now it turns out that it must run as a new thread, rather than run under the current thread as above. How would you do that?


What do you mean? What has to run as a new thread? The run() method can run on any given thread (assuming the class is a Runnable). The thread which wants to cancel the task needs to have access to the same object and calls the cancelWidgets() method, but can also be called from any thread (presumably not the same one as does the processing).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic