I'm building an HTTPServer that needs to handle multiple requests at the same time.
The main function of what I built looks like this:
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/", new MyRequestDispatcher());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
}
Now I'm thinking on how this Executors.newCachedThreadPool()
works regarding the number of threads created. As I have read that the number of threads to be created is not limited, if I get a thousand of requests at the same time, would it create a thousand of threads?
I'm thinking on limiting the number of threads created at the same time in order to be handled properly in the machine where it is being run. I thought on something like this:
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
The goal is to only create the given number of threads depending on the available processors in the system.
Would this work?
Thanks in advance!!
Yes, it will work, and it's what you'll see recommended most often.
Depending on your specific use-case you still may want to use a different number.