#8: Streams are thread safe

Here's the writeInt() method from java.io.DataOutputStream:

public final void writeInt(int v) throws IOException {
  OutputStream out = this.out;
  out.write((v >>> 24) & 0xFF);
  out.write((v >>> 16) & 0xFF);
  out.write((v >>>  8) & 0xFF);
  out.write((v >>>  0) & 0xFF);
  written += 4;
}
What happens if this method is interrupted by another thread that proceeds to write data onto the same stream?

Solution:

  1. Never use the same stream in more than one thread
  2. Don't chain multiple filters in parallel to one underlying stream (series is OK)

Previous | Next | Top
Other Presentations | Cafe au Lait Home
Last Modified May 16, 1999
Copyright 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu