title: java multithreading two thread try to readobject() from same object input stream at the same time
body: <p>I try to make a card game.I have a thread problem. I construct objectinputstream only once and then just send a reference to it to the other Thread and two thread try to read an object from same objectinputstream at the same time.The same problem is <a href="https://stackoverflow.com/questions/10775709/java-io-streamcorruptedexception-invalid-type-code-00">java.io.StreamCorruptedException: invalid type code: 00</a> but it didn't solved.</p>
  
  <p>ClientService Thread:</p>
  
  <pre><code>    while (true) {
          try {
              Message message = (Message) in.readObject();
              handleClientMessage(message);
              if (message.getType() == Message.messageType.ClientDisconnect) {
                  break;
              }
          } catch (IOException ex) {
              Logger.getLogger(ClientServiceThread.class.getName()).log(Level.SEVERE, null, ex);
          } catch (ClassNotFoundException ex) {
              Logger.getLogger(ClientServiceThread.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  </code></pre>
  
  <p>Game Thread</p>
  
  <pre><code>public void run() {
      startGame();
  
      while (true) {
          try {
              Message msg = (Message) inPlayer1.readObject();
              handlePlayerMessage(msg);
          } catch (IOException ex) {
              Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
          } catch (ClassNotFoundException ex) {
              Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>
  
  <p>in and inplayer1 the same objectinputstream.I get an exception : java.io.StreamCorruptedException: invalid type code: 00 .Two classes make a different job only this part is similar.Please give an advise how I solve this problem.Is there any way ? </p>