我需要不断地监听来自我的 C# TCP 服务器的消息,所以我在单独的线程中进行:
private void StartMessageReceivingLoop()
{
new Thread(){
public void run()
{
String msg = null;
try
{
msg = inputStream.readLine(); // inputStream is a BufferedReader instance.
}
catch (IOException e)
{
e.printStackTrace();
}
if (msg != null && msg != "")
NotifyAndForwardMessage(msg); // Notify listeners about the message.
run(); // Next iteration.
}
}.start();
}
我的方法有什么问题?为什么我会收到 StackOverflowError?我猜它run()
的调用速度非常快,因为BufferedReader.readLine()
它是非阻塞的,但我能做些什么呢?