• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

End a child thread when parent thread dies

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to figure out the best way to handle this situation. I have two threads one using a PipedInputStream and the child a PipedOutputStream, if the thread reading the input stream has a runtime exception the thread writing to the output stream locks while it waits for the input stream to be read, but obviously it will never read as the thread has already died. How can I have the writer thread to detect the termination of reader thread? I am already able to handle the other by using UncaughtExceptionHandler.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.

Could you show main fragments of your implementation?

Adam
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The writer can simply store a reference to the reader thread and check it's status with 'isAlive'.
Or you can do it in a more sophisticated way using observer design pattern (look at Observer/Observable in the java API).
Reader can wrap an Observable object, and Writer can register to the reader as Observer.
When Reader finishes it fires a message to all Observers.
 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Smolnik wrote:Could you show main fragments of your implementation?



This is the class that is doing the piping



And the following is written by others to read from my class




[edit]

Ireneusz Kordal wrote:The writer can simply store a reference to the reader thread and check it's status with 'isAlive'.


I am not sure how I would be able to do this as when I do a write to the output it can lock the thread until the buffer is freed up by reading the input.

Ireneusz Kordal wrote:Or you can do it in a more sophisticated way using observer design pattern (look at Observer/Observable in the java API).
Reader can wrap an Observable object, and Writer can register to the reader as Observer.
When Reader finishes it fires a message to all Observers.


I am not sure how I would be able to do this as I would depend on others to add the observer, plus it would remove the simplicity of just having them read from the input stream.
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.

Could you enforce ending the run method by invoking output.close in case of receiving exception during the read (java.io.IOException: Pipe closed)?

Adam
 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Smolnik wrote:Could you enforce ending the run method by invoking output.close in case of receiving exception during the read (java.io.IOException: Pipe closed)?



I am closing the output in the finally block. Or do you mean having others be sure to catch for any exception and to be sure to call close() on my input stream?
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.

I think about the simplest solution as follows (but of course there are drawbacks of the one):






[edit]

Invoking output.close(); from the parent thread results in completing the run method in the child thread.

Adam
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW: Why do you use this kind of inheritance, what are the reasons:
?
and further you take advantage of a classical delegation:


Cannot be used the more flexible and safer approach showed below(composition vs. inheritance)?

Or if you want to use inheritance and thus for example reduce amount of variables and somewhat simplify your code:

It is only my question not any remark.

Adam
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic