aspose file tools*
The moose likes Threads and Synchronization and the fly likes Breaking out of forever loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Breaking out of forever loop" Watch "Breaking out of forever loop" New topic
Author

Breaking out of forever loop

Bob Lawson
Ranch Hand

Joined: Jul 20, 2010
Posts: 34
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

Joined: Jul 20, 2010
Posts: 34
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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:




Steve
Bob Lawson
Ranch Hand

Joined: Jul 20, 2010
Posts: 34
Steve,

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

Bob
Bob Lawson
Ranch Hand

Joined: Jul 20, 2010
Posts: 34
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Breaking out of forever loop