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.
Since I am a new user I can't post image, please see. Green in the image means running, yellow is blocked or waiting.
Image also available unscaled here:
Main
{
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();
}
TcpRead Runnable
public final class IoTcpReadRunnable implements Runnable {
public static final Logger logger = LoggerFactory.getLogger(IoTcpReadRunnable.class);
protected MessageProcessor<MessageType> messageProcessor = null;
protected Socket tcpSocket = null;
protected ObjectOutputStream outputStream = null;
protected ObjectInputStream inputStream = null;
protected boolean connected = false;
public IoTcpReadRunnable(MessageProcessor<MessageType> 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();
}
}