gpt4 book ai didi

java - com.sun.HttpServer 套接字积压

转载 作者:搜寻专家 更新时间:2023-10-31 20:30:34 26 4
gpt4 key购买 nike

套接字积压是什么意思?

例如我有网络服务

@WebService
public class Calculator {
public int sum(int a, int b) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return a + b;
}

public static final int threadCount = 10;

public static void main(String[] args) throws IOException {
Executor manyThreads = Executors.newFixedThreadPool(threadCount);
HttpServer server = HttpServer.create(new InetSocketAddress(8989), threadCount);
server.setExecutor(manyThreads);
server.start();

Endpoint ep = Endpoint.create(new Calculator());
HttpContext context = server.createContext("/calc");
ep.publish(context);
}
}

和网络服务客户端

 public static void main(String[] args) throws IOException {
CalculatorService service = new CalculatorService();
final Calculator calc = service.getCalculatorPort();

Executor executor = Executors.newFixedThreadPool(100);

for (int i = 0; i < 1000000; ++i) {
executor.execute(new Runnable() {
public void run() {
try {
int currentThreads = runningThreads.incrementAndGet();
int res = calc.sum(10, 10);
System.out.println("Running threads: " + currentThreads + " Result: " + res);
} catch (ClientTransportException e) {
System.out.println("connection refused");
} finally {
runningThreads.decrementAndGet();
}
}
});
}

System.in.read();
}

在服务器上只有 10 个工作线程,积压设置为 10,因此可以有 10 个已接受的连接和 10 个等待积压的连接,任何其他连接都将被拒绝,对吗?

可能不是,因为我没有得到 ClientTransportException。

你能给我解释一下连接发生了什么以及为什么没有 ClientTransportException 吗。

最佳答案

这是一个连接队列,在您调用 accept() 以在您的应用程序中了解它们之前,TCP 已经接受了这些连接。将它设置为像 10 这样的低值是毫无意义的:TCP 会将其增加到它自己的最小值,根据平台的不同,它可能是 50 或 500,如果不是,它所做的只是导致毫无意义的 ConnectExceptions . 您最好将此值保留为默认值。

关于java - com.sun.HttpServer 套接字积压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6732210/

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