gpt4 book ai didi

java线程似乎没有被释放

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:01 25 4
gpt4 key购买 nike

我正在尝试实现一个非常简单的回显多线程服务器。
我使用了用 newFixedThreadPool 创建的线程池,但看起来并发连接数固定在 nThreads(传递到 newFixedThreadPool)。例如,如果我将 nThreads 设置为 3,那么第四个连接的客户端将无法与服务器进行交互。

这很奇怪,因为文档说“创建一个线程池,该线程池重用固定数量的线程在共享无界队列中运行。
由于文档还说“如果在所有线程都处于 Activity 状态时提交其他任务,它们将在队列中等待直到线程可用。”我怀疑我的线程永远不会“释放”,因此它们从未可供重复使用。

我认为这可能是一个愚蠢的错误,但我无法弄清楚哪里出了问题。这是我的客户端处理程序的 run() 方法,我认为它与找到的代码非常相似 here (下面的client只是一个连接到客户端的Socket):

@Override
public void run() {
try(BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()))) {

String input;
while(true) {
input = readLine();
if(input == null)
break;

out.write(input);
out.newLine();
out.flush();
}
} catch(IOException e) {
System.err.println(e, "Error from socket IO.");
System.exit(1);
}

try {
client.close();
} catch(IOException e) {
System.err.println(e, "Error when closing socket.");
System.exit(1);
}
System.out.println("Client " + client.getInetAddress().getHostAddress() +
" closed connection.\n");
}

编辑 1:我想我应该澄清一下,客户端在收到响应后会立即断开连接。因此线程应该很快可用。

编辑 2:这是处理连接的代码(listen 是一个 ServerSocket):

while(true) {
Socket client = null;
try {
client = listen.accept();
} catch(IOException e) {
System.err.println(e, "Error when waiting for connection.");
System.exit(1);
}
System.out.println("New connection from client: " +
client.getInetAddress().getHostAddress() + "\n"
);

threadPool.execute(new ConnectionHandler(client));
}

这是线程池初始化(在构造函数中):this.threadPool = Executors.newCachedThreadPool();

编辑 3:记录消息(当 nThread = 3 时,因此从未真正处理第四个连接):

New connection from client: 127.0.0.1

Client 127.0.0.1 closed connection.

New connection from client: 127.0.0.1

Client 127.0.0.1 closed connection.

New connection from client: 127.0.0.1

Client 127.0.0.1 closed connection.

New connection from client: 127.0.0.1

最佳答案

我不知道为什么第四个连接挂起,除了说这是一个确实有效的代码版本...

public class Test {
public static void main(String[] args) throws Exception {
ServerSocket listen = new ServerSocket(9999);
ExecutorService threadPool = Executors.newFixedThreadPool(3);
while(true) {
Socket client = null;
try {
client = listen.accept();
} catch(IOException e) {
System.err.println(e);
}
System.out.println("New connection from client: " +
client.getInetAddress().getHostAddress() + "\n"
);

threadPool.execute(new ConnectionHandler(client));
}
}
}

class ConnectionHandler implements Runnable {
private Socket client;

public ConnectionHandler(Socket client) {
this.client = client;
}

@Override
public void run() {
try(BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()))) {

String input;
while(true) {
input = in.readLine();
if(input == null)
break;

out.write(input);
out.newLine();
out.flush();
}
} catch(IOException e) {
System.err.println("Error from socket IO.");
}

try {
client.close();
} catch(IOException e) {
System.err.println("Error when closing socket.");
}
System.out.println("Client " + client.getInetAddress().getHostAddress() +
" closed connection.\n");
}
}

我用 telnet 连接,回显一些字符然后断开连接。我这样做了 5 次。

    New connection from client: 0:0:0:0:0:0:0:1

Client 0:0:0:0:0:0:0:1 closed connection.

New connection from client: 0:0:0:0:0:0:0:1

Client 0:0:0:0:0:0:0:1 closed connection.

New connection from client: 0:0:0:0:0:0:0:1

Client 0:0:0:0:0:0:0:1 closed connection.

New connection from client: 0:0:0:0:0:0:0:1

Client 0:0:0:0:0:0:0:1 closed connection.

New connection from client: 0:0:0:0:0:0:0:1

Client 0:0:0:0:0:0:0:1 closed connection.

唯一的区别是问题中的代码有一行...

input = readLine();

我需要把它改成

input = in.readLine();

有效。我想知道未发布到问题的代码中是否包含静态变量或其他一些恶作剧。

关于java线程似乎没有被释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46440171/

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