• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you close java program?  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, there is a project, rather big, not mine.
There is a test case/example. However the code doesn't finish. Or rather after finishing the application still runs.
I did classical debug - it just went to the end and that's it. Yet the application runs. I know for certain, that there are couple threads running concurrently (but they shouldn't have to). How to close all threads? Or to be more precise: how to close all threads instantiated by a class?

 
Saloon Keeper
Posts: 4038
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JVM quits when there are no more daemon threads running. Maybe some of the threads that are still running should be daemon threads, but aren't?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A classic approach for such scenarios is to use join method in the end of method which spawns the threads. That way, you could exit after all the threads(or the threads which you join) are finished.

By the way, can you provide some more details about the behavior of project? Is there any specific reason to keep some threads alive for a long time?

Or it might be a straightforward scenario in which threads do take more time to finish their work, right? In that case, even if you debug main thread, it will finish first and spawned threads would still be running.

I hope this helps.
 
Justin Thomas
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You gave me a good idea about what and where to look.

And I found 2 bugs in the code :)

The ugliest
I have ever seen.

And this:
java.util.concurrent.ExecutorService;

so I read the javadoc of this class and added this line in apropriate place:

Now it closes well.

I have an itchy feeling that I will come back to that javadoc of ExecuteService more than once in my future coding life.....

Nevertheless, clicked "Resolved"
Thanks
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Thomas wrote:You gave me a good idea about what and where to look.

And I found 2 bugs in the code :)

The ugliest
I have ever seen.


That's not necessarily a bad thing. It's a pretty common idiom, although you do need to provide some other way to end the loop. (Personally, I generally avoid that idiom, in favor of something like while (!done()).

And this:
java.util.concurrent.ExecutorService;


That's a bug?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!