com.sun.HttpServer套接字积压

时间:2011-07-18 11:30:33

标签: java multithreading web-services com.sun.net.httpserver

socket backlog是什么意思?

例如我有网络服务

@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);
}
}

和webservice客户端

 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。

1 个答案:

答案 0 :(得分:2)

在您调用accept()以在应用程序中识别它们之前,TCP已经接受了一个连接队列。将它设置为像10这样的低值是非常没有意义的:TCP会将其增加到自己的最小值,根据平台可能会增加到50或500,如果没有,那么它所完成的只会导致无意义{{1}你最好把这个值保留为默认值。

相关问题