public void run() { try { List<InterHubMessage> messages = new ArrayList<InterHubMessage>(); try { while (true) { lock.lock(); try { queue.take(messages); } finally { lock.unlock(); } for (InterHubMessage message : messages) { connection.dispatch(message); if (message instanceof EndOfStream) { connection.flush(); return; } } connection.flush(); messages.clear(); } } finally { lock.lock(); try { connectionState.dispatchFinished(); } finally { lock.unlock(); } } } catch (Throwable t) { errorHandler.execute(t); } }