This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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: 6054
154
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?
 
It's just a flesh wound! Or a tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!