title: Java: Are concurrent reads and writes possible on a blocking SocketChannel via Object(In|Out)putStreams?
body: <p>I created an <code>ObjectInputSteam</code> and <code>ObjectOutputStream</code> on a blocking <code>SocketChannel</code> and am trying to read and write concurrently.  My code is something like this:</p>
  
  <pre><code>socketChannel = SocketChannel.open(destNode);
  objectOutputStream = new ObjectOutputStream(Channels.newOutputStream(socketChannel));
  objectInputStream = new ObjectInputStream(Channels.newInputStream(socketChannel));
  
  Thread replyThread = new Thread("SendRunnable-ReplyThread") {
      @Override
      public void run() {
          try {
              byte reply = objectInputStream.readByte();//(A)
              //..process reply
          } catch (Throwable e) {
              logger.warn("Problem reading receive reply.", e);
          }
      }
  };
  replyThread.start();
  
  objectOutputStream.writeObject(someObject);//(B)
  //..more writing
  </code></pre>
  
  <p>Problem is the write at line (B) blocks until the read at line (A) completes (blocks on the object returned by <code>SelectableChannel#blockingLock()</code> ).  But app logic dictates that the read will not complete until all the writes complete, so we have an effective deadlock.</p>
  
  <p><code>SocketChannel</code> javadocs say that concurrent reads and writes are supported.</p>
  
  <p>I experienced no such problem when I tried a regular Socket solution:</p>
  
  <pre><code>Socket socket = new Socket();
  socket.connect(destNode);
  final OutputStream outputStream = socket.getOutputStream();
  objectOutputStream = new ObjectOutputStream(outputStream);
  objectInputStream = new ObjectInputStream(socket.getInputStream());
  </code></pre>
  
  <p>However, then I cannot take advantage of the performance benefits of <code>FileChannel#transferTo(...)</code></p>