#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;
}

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 | Cafe au Lait |Cafe con Leche

Copyright 1999, 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 30, 2002