title: Tcp server WritePendingException although thread locks
body: <p>I've written a simple asynchronous tcp server with the nio.
  The server should be able to read and write at the same time for each client.
  This I've implemented with a simple packet queue. </p>
  
  <pre><code>public class TcpJobHandler {
  
  private BlockingQueue&lt;TcpJob&gt; _packetQueue = new LinkedBlockingQueue&lt;TcpJob&gt;();
  private Thread _jobThread;
  private final ReentrantLock _lock = new ReentrantLock();
  
  public TcpJobHandler(){
      _jobThread = new Thread(new Runnable() {
          @Override
          public void run() {
              jobLoop();
          }       
      });
  
      _jobThread.start();
  }
  
  private void jobLoop(){
      while(true){
          try {
              _lock.lock();
              TcpJob job = _packetQueue.take();
              if(job == null){
                  continue;
              }
              job.execute();
          } catch (Exception e) {
              AppLogger.error("Failed to dequeue packet from job queue.", e);
          }finally{
              _lock.unlock();
          }
      }
  }
  
  public void insertJob(TcpJob job){
      try{
          _packetQueue.put(job);
      }catch(InterruptedException e){
          AppLogger.error("Failed to queue packet to the tcp job queue.", e);
      }
  }
  }
  </code></pre>
  
  <p>What this code do, is just checking for a new packet. If a new packet is available, this packet will be send to the client.
  In the class tcp job, there is just the packet to send and a write class whichs writes the packet into the client stream. 
  As you can see, only one thread should be able to write a packet into a client stream. </p>
  
  <p>This is the point, why I dont understand, why I'm getting this error? If I'm right, this exception says, that I try to send data into a stream, but there is already a thread which is writing data into this stream. But why? </p>
  
  <p>//Edit:
  I'm getting this exception:</p>
  
  <pre><code>19:18:41.468 [ERROR] - [mufisync.server.data.tcp.handler.TcpJobHandler] : Failed to dequeue packet from job queue. Exception: java.nio.channels.WritePendingException
  at sun.nio.ch.AsynchronousSocketChannelImpl.write(Unknown Source)
  at sun.nio.ch.AsynchronousSocketChannelImpl.write(Unknown Source)
  at mufisync.server.data.tcp.stream.OutputStreamAdapter.write(OutputStreamAdapter.java:35)
  at mufisync.server.data.tcp.stream.OutputStreamAdapter.write(OutputStreamAdapter.java:26)
  at mufisync.server.data.tcp.stream.BinaryWriter.write(BinaryWriter.java:21)
  at mufisync.server.data.tcp.TcpJob.execute(TcpJob.java:29)
  at mufisync.server.data.tcp.handler.TcpJobHandler.jobLoop(TcpJobHandler.java:40)
  at mufisync.server.data.tcp.handler.TcpJobHandler.access$0(TcpJobHandler.java:32)
  at mufisync.server.data.tcp.handler.TcpJobHandler$1.run(TcpJobHandler.java:25)
  at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>The TcpJob looks like this:</p>
  
  <pre><code>public class TcpJob {
  
  private BasePacket _packet;
  private BinaryWriter _writer;
  
  public TcpJob(BasePacket packet, BinaryWriter writer){
      _packet = packet;
      _writer = writer;
  }
  
  public void execute(){
      try {
          if(_packet == null){
              AppLogger.warn("Tcp job packet is null");
              return;
          }
  
          _writer.write(_packet.toByteArray());
      } catch (IOException e) {
          AppLogger.error("Failed to write packet into the stream.", e);
      }
  }
  
  public BasePacket get_packet() {
      return _packet;
  }   
  }
  </code></pre>
  
  <p>The BinaryStream is just coupled to a AsynchronousSocketChannel which calls the write(byte[]) method from the socket channel.</p>