• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How do you close java program?

 
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: 6597
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • 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?
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic