Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Are concurrent reads and writes possible on a blocking SocketChannel via Object(In|Out)putStreams?
    text
    copied!<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>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload