aspose file tools*
The moose likes Threads and Synchronization and the fly likes Child Thread continues to run when parent process exits when exception occurs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Child Thread continues to run when parent process exits when exception occurs" Watch "Child Thread continues to run when parent process exits when exception occurs" New topic
Author

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

yogessh chavaan
Greenhorn

Joined: Apr 08, 2011
Posts: 13

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 08, 2011
Posts: 13

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

Joined: Dec 08, 2010
Posts: 1509
    
    5

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).


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Child Thread continues to run when parent process exits when exception occurs