我可以并行处理 Java HttpServer 线程/处理请求吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14729475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I make a Java HttpServer threaded/process requests in parallel?
提问by Humunculus84
I have built a simple HttpServer following tutorials i have found online, using Sun's lightweight HttpServer.
我按照我在网上找到的教程构建了一个简单的 HttpServer,使用 Sun 的轻量级 HttpServer。
Basically the main function looks like this:
基本上,主要功能如下所示:
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
//Create the context for the server.
server.createContext("/", new BaseHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
And I have implemented the BaseHandler Interface's method to process the Http request and return a response.
我已经实现了 BaseHandler 接口的方法来处理 Http 请求并返回响应。
static class BaseHandler implements HttpHandler {
//Handler method
public void handle(HttpExchange t) throws IOException {
//Implementation of http request processing
//Read the request, get the parameters and print them
//in the console, then build a response and send it back.
}
}
I have also created a Client that sends multiple requests via threads. Each thread sends the following request to the server:
我还创建了一个通过线程发送多个请求的客户端。每个线程向服务器发送以下请求:
http://localhost:8000/[context]?int="+threadID
http://localhost:8000/[context]?int="+threadID
On Each client run, The requests seem to arrive in different order to the server, but they are served in a serial manner.
在每个客户端运行时,请求似乎以不同的顺序到达服务器,但它们以串行方式提供服务。
What i wish to acomplish is for the requests to be processed in a parallel manner if that is possible.
如果可能的话,我希望以并行方式处理请求。
Is it possible, for example, to run each handler in a seperate thread, and if so, is it a good thing to do.
例如,是否可以在单独的线程中运行每个处理程序,如果可以,这样做是否是一件好事。
Or should i just drop using Sun's lightweight server altogether and focus an building something from scratch?
或者我应该完全放弃使用 Sun 的轻量级服务器并专注于从头开始构建一些东西?
Thanks for any help.
谢谢你的帮助。
回答by twillouer
As you can see in ServerImpl, the default executor just "run" the task :
正如您在ServerImpl中看到的,默认执行程序只是“运行”任务:
157 private static class DefaultExecutor implements Executor {
158 public void execute (Runnable task) {
159 task.run();
160 }
161 }
you must provide a real executor for your httpServer, like that :
你必须为你的 httpServer 提供一个真正的执行器,就像这样:
server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
and your server will run in parallel. Carefull, this is a non-limited Executor, see Executors.newFixedThreadPoolto limit the number of Thread.
并且您的服务器将并行运行。小心,这是一个不受限的Executor,参见Executors.newFixedThreadPool来限制Thread的数量。
回答by user3322581
You used server.setExecutor(null) that runs the handler in the same caller thread. In this case, the main thread which runs the server.
您使用了在同一调用者线程中运行处理程序的server.setExecutor( null)。在这种情况下,运行服务器的主线程。
You only need to change the line as
您只需要将行更改为
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
//Create the context for the server.
server.createContext("/", new BaseHandler());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
}