gpt4 book ai didi

递归线程中的 java.lang.OutOfMemoryError

转载 作者:行者123 更新时间:2023-12-02 05:26:59 27 4
gpt4 key购买 nike

我正在创建一个用于压力测试的应用程序。通过 1500 个线程来完成此操作,并通过受审核方的内部网络使 1500 个线程同时连接。在 1500 个线程中轮流调用一个线程,每个线程调用一个套接字,当该套接字关闭时重新打开并重新发送相同的数据。问题是应用程序给我错误,说有无限循环并引发堆栈溢出,但想法是创建 java 递归调用,但并没有离开我。我能做什么?

主类:

public void startStres(){

for(int n = 1; n <= Integer.parseInt(valueThreads.getText().toString()); n++)
robots.add(new robot(this, n, valueUrl.getText().toString()));

valueTotal.setText(Integer.toString(robots.size()));

for(final robot bot : robots)
new Thread(new Runnable() {
public void run() {
bot.start();
}
}).start();
}

在机器人类中:

public void start(){
context.robotOnConnecting(idUnique);

try {
socketHandle = new Socket(InetAddress.getByName(dataConnection.host), dataConnection.port);
context.robotOnConnect(idUnique);

PrintWriter writer = new PrintWriter(socketHandle.getOutputStream());
writer.print(
"GET " + dataConnection.query + " HTTP/1.1\n" +
"Host: " + dataConnection.host + "\n" +
"User-Agent: " + dataConnection.userAgent + "\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n" +
"Accept-Language: en-US,en;q=0.5\n" +
"Accept-Encoding: gzip, deflate\n" +
"Connection: keep-alive\n\n"
);
writer.flush();

BufferedReader br = new BufferedReader(new InputStreamReader(socketHandle.getInputStream()));

char[] bt = new char[1];
br.read(bt, 0, 1);
br.close();
socketHandle.close();

context.robotOnFinish(idUnique);
context.robotOnClose(this, idUnique, true);
//start(); <-- Error

} catch (IOException e) {
context.robotOnError(idUnique, e.getMessage());
context.robotOnClose(this, idUnique, false);

//start(); <-- Error
// e.printStackTrace();
}
}

错误:

526-1073/com.x.audits.strestesting D/dalvikvm﹕ GC_FOR_ALLOC freed 16K, 21% free 50827K/63572K, paused 41ms, total 41ms
526-1095/com.x.audits.strestesting I/dalvikvm﹕ threadid=25: stack overflow on call to Ljava/lang/ThreadLocal;.get:L
526-1095/com.x.audits.strestesting I/dalvikvm﹕ method requires 28+20+8=56 bytes, fp is 0x761f9314 (20 left)
526-1095/com.x.audits.strestesting I/dalvikvm﹕ expanding stack end (0x761f9300 to 0x761f9000)
526-1095/com.x.audits.strestesting I/dalvikvm﹕ Shrank stack (to 0x761f9300, curFrame is 0x761f9488)
526-1089/com.x.audits.strestesting I/dalvikvm﹕ threadid=19: stack overflow on call to Ljava/lang/ThreadLocal;.get:L
526-1089/com.x.audits.strestesting I/dalvikvm﹕ method requires 28+20+8=56 bytes, fp is 0x76189314 (20 left)
526-1089/com.x.audits.strestesting I/dalvikvm﹕ expanding stack end (0x76189300 to 0x76189000)
526-1089/com.x.audits.strestesting I/dalvikvm﹕ Shrank stack (to 0x76189300, curFrame is 0x76189488)
526-1089/com.x.audits.strestesting W/dalvikvm﹕ threadid=19: thread exiting with uncaught exception (group=0x418a8898)
526-1095/com.x.audits.strestesting W/dalvikvm﹕ threadid=25: thread exiting with uncaught exception (group=0x418a8898)

最佳答案

处理多线程时,应该使用线程池来避免此类问题。

查看ThreadPoolExecutor.execute(Runnable) 。它将使您能够更好地处理多线程。

关于递归线程中的 java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25923700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com