Re: streams and threads... why?

From: Matt Humphrey (matth_at_ivizNOSPAM.com)
Date: 01/29/04


Date: Wed, 28 Jan 2004 20:23:00 -0500


"Steve Horsley" <shoot@the.moon> wrote in message
news:bv9bu3$gjb$1@news.freedom2surf.net...
> Frank wrote:
> > I acknowledge that in general, thread safety is a very important issue
> > when designing a module, but I'm curious how often do people really
> > concern themselves with thread safety regarding stream input?
> >
> > This question comes to mind particularly with regard to java.io.Reader.
> > While I don't concern myself with multithreaded reads, I'm writing this
> > message because I can't even fathom why anyone would?
> >
> > It seems (logical) to me that one would generally have a thread that
> > took your stream to read in fully descriptive "packets", then selected a
> > thread to delegate the packet to. Or perhaps, in the case of files,
> > allow concurrent InputStreams per each thread, even when they go to the
> > same file. Neither of which require locking of the stream itself.
> > Perhaps one could write a stream that encapsulated java.util.Random...
> > but that's not exactly a stream :) But incorporating locking semantics
> > so that multiple threads can compete to read from the same stream....
> > Why??? Has anyone actually done this?
> >
> > Cheers,
> >
> > Frank
>
> I would expect the whole java.io API to be thread-safe. Otherwise the
> whole think would be crash-prone if used by multiple threads, and teh
> failure of the API to perform as documented would clearly be a bug in
> the API. I can't think of a reason to read a stream with multiple
> threads either, but you have to write the API safely anyway.

I agree that if the API requirements state that some methods be thread-safe
(or synchronized) it would be a bug if they were not. However, I think the
question is really about the API requirements themselves. It would be nice
if all APIs were thread-safe, but some are not for good reasons. Rather,
APIs like Swing and some others (HashMap, TreeMap, etc) are not thread-safe,
but give you some techniques for making your program thread-safe. It's up
to the designer / developer to properly structure safety at a higher level.

The OP posed an interesting question: given that the Reader class includes a
deliberate locking mechanism, what are some real cases where multiple
threads read from the same stream? I havn't thought of any myself, but it's
also not clear to me that any Reader subclasses are necessarily thread-safe
either as it's up to the Reader implementation to use the locking object on
the critical sections itself (as indicated by the API docs). I would think
that if the documentation does not explicitly state that a class is
thread-safe, it is not (although it should really say one way or the other.)
How much of java.io really is thread-safe?

Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/



Relevant Pages

  • Re: How do I use the Java API in a Thread-Safe Manner?
    ... assume that the API is *not* thread-safe unless it states that it is (such ... If the API is ... method, call it UnsafeAPI() that is not thread-safe, therefore ... ... stated above when you said "There's no effective way to synchronize access ...
    (comp.lang.java.programmer)
  • Re: Simultaneously writing to a file
    ... Marcin 'Qrczak' Kowalczyk ha escrito: ... This depends on what API do they use to write to the file. ... I'm using the thread-safe msvcrt.dll library, ... (and solo of writing threads) ...
    (comp.programming.threads)
  • Re: streams and threads... why?
    ... Frank wrote: ... > I acknowledge that in general, thread safety is a very important issue ... Neither of which require locking of the stream itself. ... I would expect the whole java.io API to be thread-safe. ...
    (comp.lang.java.programmer)
  • Re: Reading whole text files
    ... I'd prefer getc in case you're fortunate enough ... > In thread-safe libraries getc() family functions can actually ... > be quite inefficient, because they must lock the stream object, ... So, if I open the stream as text stream, everything should be ...
    (comp.lang.c)
  • Re: Net threads and Windows OS file system
    ... all initializing thread-safe wrappers for different Stream instances; ... wrapper around the stream. ... then there would need to be synchronization around that access. ...
    (microsoft.public.dotnet.languages.csharp)