Reputation: 1837
My question is regarding the shutdown()
method from the ExecutorService class. If i am calling the submit()
method in an infinite loop... do i ever need to call shutdown()
? Essentially i have something like this (pseudo-code here):
for(;;){
for(some event){
if(some event does something i want to capture){
threadPool.submit()
}
}
}
So with a set up like the above when is it appropriate to call shutdown()
? In my actual code when i try to call shutdown() outside the infinite loop i get unreachable code error - which i understand. But it seems to me that i would probably not call the shutdown()
method. What do you guys think?
Upvotes: 0
Views: 665
Reputation: 121971
Suggest passing a stop event type that instructs the loop to break
:
boolean have_received_stop_event = false;
while (!have_received_stop_event)
{
for(some event)
{
if(stop-event)
{
have_received_stop_event = true;
break;
}
if(some event does something i want to capture)
{
threadPool.submit()
}
}
}
Then call shutdown()
after main loop exits.
Upvotes: 2
Reputation: 2216
If there is a try-catch block enclosing this infinite loop, you could call shutdown in the catch block. This would allow you to tidy up when an exception is thrown.
Upvotes: 0