mic*_*nko 5 java multithreading web-services com.sun.net.httpserver
套接字积压是什么意思?
例如我有网络服务
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
和网络服务客户端
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();
}
Run Code Online (Sandbox Code Playgroud)
在服务器上,只有10个工作线程并将backlog设置为10,因此可以有10个接受的连接和10个在backlog中等待的连接,其他任何连接都将被拒绝,对吗?
可能不是,因为我没有获得ClientTransportException。
您能否给我解释一下连接发生了什么以及为什么没有ClientTransportException。
accept()它是在您在应用程序中调用以了解它们之前已被 TCP 接受的连接队列。将其设置为像 10 这样的低值是毫无意义的:TCP 会将其增加到自己的最小值,这可能是 50 或 500,具体取决于平台,如果没有,它所做的只是导致毫无意义ConnectExceptions.。 off 将此值保留为默认值。
| 归档时间: |
|
| 查看次数: |
2765 次 |
| 最近记录: |