[concurrency-interest] Contract of java.io.OutputStream.write(byte b[])

Joe Bowbeer joe.bowbeer at gmail.com
Wed May 31 19:48:08 EDT 2006


New annotations have been created for the purpose of documenting which lock
guards which state.

Watch this space for @GuardedBy and friends:

  http://jcip.net/

In the near term, I recommend providing your own locks unless you're
intimate with the details of the specific OutputStream subclass
implementation (on your specific platform).

  synchronized (outLock) {
    out.write(buffer);
  }

If is often the case that the outputstream object can serve as this lock.

In other cases, no explicit locking is needed.  The write method is
synchronized in the ByteArrayOutputStream implementation I'm looking at
now..

-- 
Joe Bowbeer ~ http://joebowbeer.thruhere.net



On 5/31/06, Elias Ross <eross at m-qube.com> wrote:
>
>
> I know this isn't really the forum for asking this, but I've spent a
> number of hours looking into a definite yes or no...
>
> I wanted to know if OutputStream.write(byte b[]) was considered atomic.
> If two threads are writing to the same file, would the output from
> OS.write(...) overlap with another OS.write(...)?
>
> I eventually downloaded the Java source to answer this question.  The
> JavaDoc did not specify.  As it turns out, the answer is "no" -- which
> is something that you'd only be able to determine by looking at the C
> source code for the native calls.
>
> The JDK interpretation of write() is slightly different than the UNIX
> one, which looks like this:
>
>        ssize_t write(int fd, const void *buf, size_t count);
>
> so it makes sense that a Java write() call may have to do more than one
> system write() call.
>
> I wonder if there some better way that "concurrent" and "atomic" methods
> can be documented in the JDK?  It's also very helpful as a user to know
> if I should be locking on the OutputStream itself or create my own
> locks.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060531/ec38dab2/attachment.html 


More information about the Concurrency-interest mailing list