Does anyone have any good suggestions for creating a Pipe object in Java which is both an InputStream and and OutputStream since Java does not have multiple inheritance and both of the streams are abstract classes instead of interfaces?
4 Answers
1 Comment
Matthieu
The Javadoc explicitely states "Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread". What happens is that
PipedInputStream.read() will block the calling thread until the associated PipedOutputStream.write() is called in another thread.check Pipe class for a ready made implementation
or the PipedInputStream and PipedOutputStream pair
Comments
This is a good case where you don't need multiple inheritance, and the fact that you asked the question that way concerns me a bit.
In this case you would have a class that had an input stream, and an output stream. No need to extend anything or use an interface.
After changing your code to do this, read this article about composition vs inheritance: http://www.artima.com/lejava/articles/designprinciples4.html