You are on page 1of 3

6/13/13

Breaking out of forever loop (Threads forum at JavaRanch)

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Java Threads and Synchronization

Author

Breaking out of forever loop


posted 10/28/2010 2:12:24 AM

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

posted 10/28/2010 2:17:37 AM

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(); }

www.coderanch.com/t/515387/threads/java/Breaking-loop

1/3

6/13/13
}

Breaking out of forever loop (Threads forum at JavaRanch)

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3081

posted 10/28/2010 3:27:32 AM

You loop will end when the Thread gets interrupted. So all you would have to do was interrupt the thread running the long loop:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

5
I like...

0 1 .

t h r e a d R u n n i n g W i d g e t s . i n t e r r u p t ( ) ;

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:

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 .

p r i v a t ev o l a t i l eb o o l e a nk e e p R u n n i n g=t r u e ; p r i v a t ev o l a t i l eT h r e a dr u n n i n g T h r e a d ; p u b l i cv o i dr u n ( ){ / / K e e par e f e r e n c et ot h r e a dr u n n i n gt h i sc o d e r u n n i n g T h r e a d=T h r e a d . c u r r e n t T h r e a d ( ) ; t r y{ D a ow i d g e t D a o=n e wW i d g e t D a o I m p l ( ) ; / /d ou n t i lt o l dt os t o p w h i l e ( k e e p R u n n i n g ){ / /g e tw i d g e t sw i t haR E A D Ys t a t u s C o l l e c t i o n < W i d g e t >w i d g e t s=w i d g e t D a o . g e t R e a d y W i d g e t s ( ) ; f o r ( W i d g e tw i d g e t:w i d g e t s ){ / /s e tt h es t a t u st oP R O C E S S I N G w i d g e t D a o . s e t W i d g e t S t a t e ( w i d g e t . g e t I d ( ) ,W i d g e t S t a t e . P R O C E S S I N G ) ; / /R u nt h ew i d g e t m o d e l S v c . r u n W i d g e t ( w i d g e t ) ; / /s e tt h es t a t u st oC O M P L E T E w i d g e t D a o . s e t W i d g e t S t a t e ( w i d g e t . g e t I d ( ) ,W i d g e t S t a t e . C O M P L E T E ) ; } t r y{ T h r e a d . s l e e p ( 5 0 0 0 ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ni e ){ i f( k e e p R u n n i n g ){/ / T h i se x c e p t i o nw a sN O Tc a u s e db yac a n c e l W i d g e t s ( )c a l l ,s or e p o r tt h e e x c e p t i o n t h r o w ( i e ) ; }e l s e{/ / T h i se x c e p t i o nw a sc a u s e db yc a n c e l W i d g e t s ( )s ow ec a ni g n o r ei t . . . / / D on o t h i n g } } } }c a t c h( S Q L E x c e p t i o ne ){ t h r o wn e wR u n t i m e E x c e p t i o n ( " F a i l e d" ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ne ){ / /o n l yh a p p e n sw h e nt h ei n t e r r u p t i o nd o e s n ' tc o m ef r o mc a n c e l W i d g e t s . . . e . p r i n t S t a c k T r a c e ( ) ; } } / / S i g n a l st h ew i d g e t ss h o u l ds t o pb e i n gr u n . I fc a n c e l F o r c e f u l l yi st r u e ,t h em e t h o dw i l ls t o pt h e t h r e a dm o r e / / a g g r e s s i v e l y-i n t e r r u p t i n gt h eT h r e a dp o s s i b l ym a k i n gi tc o m et oa ne n ds o o n e r . p u b l i cv o i dc a n c e l W i d g e t s ( b o o l e a nc a n c e l F o r c e f u l l y ){ k e e p R u n n i n g=f a l s e ; i f( r u n n i n g T h r e a d! =n u l l& &c a n c e l F o r c e f u l l y ){ r u n n i n g T h r e a d . i n t e r r u p t ( ) ; } }

www.coderanch.com/t/515387/threads/java/Breaking-loop

2/3

6/13/13

Breaking out of forever loop (Threads forum at JavaRanch)

Steve

Bob Lawson Ranch Hand Joined: Jul 20, 2010 Posts: 34

posted 10/28/2010 4:39:20 AM

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

Bob Lawson Ranch Hand Joined: Jul 20, 2010 Posts: 34

posted 10/28/2010 8:31:02 PM

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: 3081

posted 10/28/2010 9:51:09 PM


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?

5 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
I like...

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).

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: Breaking out of forever loop

Similar Threads printing on an applet JUnit and Cactus using strutstestCase Assert stmt in Eclipse Dynamic Data Types in Eclipse Automatically reconnect to server ?
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 12, 2013 22:36:16 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/515387/threads/java/Breaking-loop

3/3

You might also like