Note that there are some explanatory texts on larger screens.

plurals
  1. POJava readObject from Inputstream taking up 50% of CPU
    primarykey
    data
    text
    <p>I am running into a weird problem where this thread which reads from an inputstream, specifically readObject. The thread is blocking on the call like it is suppose to because I have tried putting log debugging statements and it shows its blocking. The problem is this thread is still marked as running in the profiler and it takes up 50% of my cpu usage. I have a similar thread to this which blocks properly and when blocked takes up 0% cpu. I am baffled at what can be going wrong here.</p> <p>Since I am a new user I can't post image, please see. Green in the image means running, yellow is blocked or waiting.</p> <p><img src="https://i.stack.imgur.com/Tg1sb.png" alt="http://i.imgur.com/5FPyZ.png"> Image also available unscaled <a href="https://i.imgur.com/5FPyZ.png" rel="nofollow noreferrer">here</a>:</p> <p>Main</p> <pre><code> { SocketFactory factory = SocketFactory.getDefault(); Socket tcpSocket = factory.createSocket("localhost", 5011); IoTcpReadRunnable ioTcpReadRunnable = new IoTcpReadRunnable(new MessageProcessor() { @Override public void enqueueReceivedMessage(Object message) { System.out.println("MessageReceived Enqueued."); } @Override public void enqueueMessageToWrite(Envelope message) { System.out.println("Message Enqueued to Write."); } }, tcpSocket); new Thread(ioTcpReadRunnable, "ClientExample IoTcpRead").start(); </code></pre> <p>}</p> <p>TcpRead Runnable</p> <pre><code>public final class IoTcpReadRunnable implements Runnable { public static final Logger logger = LoggerFactory.getLogger(IoTcpReadRunnable.class); protected MessageProcessor&lt;MessageType&gt; messageProcessor = null; protected Socket tcpSocket = null; protected ObjectOutputStream outputStream = null; protected ObjectInputStream inputStream = null; protected boolean connected = false; public IoTcpReadRunnable(MessageProcessor&lt;MessageType&gt; messageProcessor, Socket tcpSocket) { this.messageProcessor = messageProcessor; this.tcpSocket = tcpSocket; this.init(); } protected void init() { try { this.outputStream = new ObjectOutputStream(tcpSocket.getOutputStream()); this.outputStream.flush(); this.inputStream = new ObjectInputStream(tcpSocket.getInputStream()); } catch (IOException ex) { logger.error("Tcp Socket Init Error Error ", ex); } } public boolean isConnected() { return connected; } protected synchronized Object readObject() throws IOException, ClassNotFoundException { Object readObject = null; //blocks here logger.trace("{} About to block for read Object"); readObject = this.inputStream.readObject(); logger.trace("{} Read Object from Stream: {} ", "", readObject); return readObject; } public void close() { try { //todo this.connected = false; this.outputStream.flush(); this.outputStream.close(); this.inputStream.close(); synchronized (tcpSocket) { this.tcpSocket.close(); } } catch (IOException ex) { logger.error("Error closing Socket"); } } @Override public void run() { this.connected = true; while (this.connected) { try { Object readObject = readObject(); if (readObject != null) { this.messageProcessor.enqueueReceivedMessage((MessageType) readObject); } else { logger.error("Read Object is null"); } } catch (IOException ex) { logger.error("TcpRecieveThread IOException", ex); } catch (ClassNotFoundException ex) { logger.error("TcpRecieveThread ClassnotFound", ex); } } this.close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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