Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
yogessh chavaan
Greenhorn
Posts: 13
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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.

 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic