title: StreamCorruptedException when trying to pass InputStream
body: <p>Getting error when trying to pass socket <code>InputStream</code> variable to thread to write object. I have client and two thread: first reading text input, second reads serialized objects. They run simultaneusly, therefore I set <strong>volatile</strong> <code>static InputStream socketIs;</code> . Is that right? How to fix it?</p>
  
  <pre><code>java.io.StreamCorruptedException: invalid stream header: 57656C63
      at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
      at java.io.ObjectInputStream.&lt;init&gt;(Unknown Source)
      at com.client.ClientThreadInObj.&lt;init&gt;(ClientThreadInObj.java:25)
      at com.client.Client.main(Client.java:77)
  </code></pre>
  
  <hr>
  
  <pre><code>    public class Client { 
          static ClientThreadIn threadIn;
          static ClientThreadInObj threadInObj;
          volatile static InputStream socketIs; 
          volatile static PrintStream ps;
          volatile static BufferedReader in; 
          static PrintWriter out;
          static Socket s;
  
      public static void main(String[] args){
               in = null; 
               out = null; 
               socketIs =null;
               threadIn = null;
               threadInObj = null;
               try{ 
                  socketIs = s.getInputStream();
                  in = new BufferedReader(new InputStreamReader(socketIs)); 
                  out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
                  JPanelOne gui= startGUI();
                  gui.setOutputPrintWriter(out);
                  OutputStream outStream = gui.getRefToOutputStream(); 
                  ps =  new PrintStream(outStream); 
  
                  threadIn = new ClientThreadIn(in, ps, gui);
                  threadInObj = new ClientThreadInObj(socketIs, ps, gui); // ERROR
  
                  threadIn.start();
                  threadInObj.start();
              catch(IOException ioe) 
          { ..}
  ...
      }
  </code></pre>
  
  <hr>
  
  <p>Server:</p>
  
  <pre><code>...
  Timer timer = new Timer();
                  timer.schedule(new TimerTask() {
                      @Override
                      public void run() {
                          try {
                              FlattenLot fL = new FlattenLot(clientThread.m_clientSocket.getOutputStream(), currentLot);
                              fL.sendObj();
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                      }
                  }, 100);
  ...
  </code></pre>
  
  <p>Debug shows error on server side while trying <code>to out.writeObject(lot);</code>
  package serial;</p>
  
  <pre><code>import java.io.ObjectOutputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.*;
  
  import com.server.Lot;
  
  public class FlattenLot {
  
      OutputStream oStream;
      Lot lot;
  
      public FlattenLot(OutputStream os, Lot l){
          lot = l;
          oStream = os;
      }
  
      public void sendObj(){
          ObjectOutputStream out = null;
          try {
              out = new ObjectOutputStream(oStream);
              out.writeObject(lot); // ERROR: java.io.NotSerializableException
              out.close();
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>Lot:</p>
  
  <pre><code>import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  
  import java.util.Date;
  import java.util.Calendar;
  
  public class Lot implements Serializable{
      private static final long serialVersionUID = 1L;
      private  String NAME;
      public synchronized String getName(){return NAME;}
  }
  </code></pre>
  
  <hr>
  
  <p>Lot implements <code>Serializable</code>. Why was thrown <code>java.io.NotSerializableException</code>?
  I have got Lot object successfully from first thread which used BufferReader(Input stream) to get data. Second thread cannot read object probably because it was red by first thread. Seems like two threads race for socket input stream despite the fact that the <code>InputStream is = socket.getInputStream()</code> variable defined as volatile and have to assure thread safe collaboration....  </p>