我的 java 代码经常陷入循环 "while ((line = rd.readLine()) != null) {"。如何优化代码
My java code often stuck in a loop "while ((line = rd.readLine()) != null) {". How to optimize the code
我的 java 代码经常卡在一个循环中 "while ((line = rd.readLine()) != null) {"
我猜这是因为 java 垃圾回收没有回收一些内存。变量 "line" 将在每次迭代中指向新内容,并且 "line" 旧内容的内存由于某种原因未被 java GC 回收。
那么如何优化代码来解决问题呢?
代码如下:
for (String url : urlList) { // a loop that process ~1000 urls
HttpResponse response = fetchSomeUrl(url);
// I already confirmed that response code is 200 OK, which means HttpResponse is generated successfully.
BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
StringBuilder raw_content = new StringBuilder();
String line;
int i = 0;
System.out.println("=====start processing rd.readLine()");
while ((line = rd.readLine()) != null) {
// my code often stuck here when there are more than 1000 lines in rd
System.out.println("=====processing " + i + "th line");
raw_content.append(line);
raw_content.append("\n");
i++;
}
System.out.println("=====finished processing rd.readLine()");
}
你的循环直到达到 null 才会停止,因为代码 written.You 应该添加一个条件,告诉程序在达到 1000 时停止。而且,你的内容可能比你多 1000预期这就是它被卡住的原因。
My java code often stuck in a loop "while ((line = rd.readLine()) != null) {"
这个词是'blocked'。它被阻塞等待另一条线路从对等方到达,或等待对等方关闭连接。
I guess it is because java garbage collection did not get back some memory.
它与垃圾收集、Java 或内存没有任何关系。它与同伴正在做什么或没有做什么有关。
The variable "line" will point to new content in every iteration and the memory of old content of "line" is not collected back by java GC for some reason.
这是胡说八道,即使是真的也解释不了现象。
So How to optimize the code to fix the problem?
这不是编码问题,即使是,再多的 'optimization' 也无法解决。您的程序按编码工作。此循环将迭代直到对等方断开连接,并且当没有完整的行或流的末尾要读取时它将阻塞。
您应该在每一行到达时对其进行处理,而不是尝试 assemble 它们并在对等方断开连接时处理它们。
编辑您可以考虑设置读取超时以捕获有问题的情况。
我的 java 代码经常卡在一个循环中 "while ((line = rd.readLine()) != null) {"
我猜这是因为 java 垃圾回收没有回收一些内存。变量 "line" 将在每次迭代中指向新内容,并且 "line" 旧内容的内存由于某种原因未被 java GC 回收。
那么如何优化代码来解决问题呢?
代码如下:
for (String url : urlList) { // a loop that process ~1000 urls
HttpResponse response = fetchSomeUrl(url);
// I already confirmed that response code is 200 OK, which means HttpResponse is generated successfully.
BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
StringBuilder raw_content = new StringBuilder();
String line;
int i = 0;
System.out.println("=====start processing rd.readLine()");
while ((line = rd.readLine()) != null) {
// my code often stuck here when there are more than 1000 lines in rd
System.out.println("=====processing " + i + "th line");
raw_content.append(line);
raw_content.append("\n");
i++;
}
System.out.println("=====finished processing rd.readLine()");
}
你的循环直到达到 null 才会停止,因为代码 written.You 应该添加一个条件,告诉程序在达到 1000 时停止。而且,你的内容可能比你多 1000预期这就是它被卡住的原因。
My java code often stuck in a loop "while ((line = rd.readLine()) != null) {"
这个词是'blocked'。它被阻塞等待另一条线路从对等方到达,或等待对等方关闭连接。
I guess it is because java garbage collection did not get back some memory.
它与垃圾收集、Java 或内存没有任何关系。它与同伴正在做什么或没有做什么有关。
The variable "line" will point to new content in every iteration and the memory of old content of "line" is not collected back by java GC for some reason.
这是胡说八道,即使是真的也解释不了现象。
So How to optimize the code to fix the problem?
这不是编码问题,即使是,再多的 'optimization' 也无法解决。您的程序按编码工作。此循环将迭代直到对等方断开连接,并且当没有完整的行或流的末尾要读取时它将阻塞。
您应该在每一行到达时对其进行处理,而不是尝试 assemble 它们并在对等方断开连接时处理它们。
编辑您可以考虑设置读取超时以捕获有问题的情况。