• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Child Thread continues to run when parent process exits when exception occurs

 
Greenhorn
Posts: 13
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a child thread that continues to run when its parent exits after some exception.I dont want this child thread to continue running when the parent exits either normally or abnormally.
I cant add code to stop child thread at every exception in parent because the parent throws exceptions in many cases . Could any one please comment on this.

thanks in advance
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the thread is still running, then the process is still running too, so it's not totally clear what you mean.

If you mean what I think you mean, however then look into Thread's setDaemon() method.
 
yogessh chavaan
Greenhorn
Posts: 13
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jeff,

let me clear it.
in my parent process's constructor i start a child thread.
At first time when i start the parent ...both the parent and child will start successfully..
but while this is running and i start the parent again i get address already bind exception(as the parent is connecting to a port) and the 2nd parent exits.but the child thread started with the second parent doesn't exit.

what i want is when i start the parent second time the parent should exit with the Address already bind exception but the child thread should also exit.


thanks.

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yogessh chavaan wrote:in my parent process's constructor i start a child thread.


I guess you mean to say that in parent thread's constructor, you start a child thread (I'm not sure, but does parent-child relationship exist in threads? It does between processes, but not sure about threads)

Secondly, as now you mention about address already bind exception, I guess this has something to do with networking. I would suggest to check if address is available before starting a thread(especially if your second thread's behavior is dependent on successful binding).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yogessh chavaan wrote:
in my parent process's constructor i start a child thread.



As Anayonkar points out, that is a parent thread, not a parent process.


but while this is running and i start the parent again i get address already bind exception(as the parent is connecting to a port) and the 2nd parent exits.but the child thread started with the second parent doesn't exit.



Yes, as I suggested initially, use setDaemon().

If you do this:


then main() can end but your java process keeps running. The JVM doesn't automatically die when main ends.

So, like I said, check out Thread.setDaemon().


thanks.

 
reply
    Bookmark Topic Watch Topic
  • New Topic