入力ストリーム、特にreadObjectから読み取るこのスレッドで奇妙な問題が発生しています。ログデバッグステートメントを配置しようとしたところ、スレッドがブロックを示しているため、スレッドは想定どおりに呼び出しをブロックしています。問題は、このスレッドがまだプロファイラーで実行中としてマークされており、CPU使用率の50%を占めていることです。私はこれと同様のスレッドを持っており、適切にブロックされ、ブロックされると0%のCPUを使用します。私はここで何がうまくいかないのか困惑しています。
私は新規ユーザーですので画像を投稿できませんので、ご覧ください。画像の緑色は実行中、黄色はブロックまたは待機中を意味します。
ここで
拡大縮小されていない画像も利用できます:
主要
{
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();
}
}