I'm creating server that execute some queries. Max number of threads is set with Executors.newFixedThreadPool(2) but when I run 3 clients all of them are connected to that server at the same time. It is important to have only 2 threads active at the same time. Any ideas whats wrong? There is part of server code:
ServerSocket serverSocket = new ServerSocket(port);
ExecutorService server = Executors.newFixedThreadPool(2);
while (true) {
Socket clientSocket = serverSocket.accept();
Query query = new Query(clientSocket, allLogs);
server.submit(query);
}
When I run 3 clients first got answer: Server response: pool-1-thread-1, second Server response: pool-1-thread-2 and third Server response: pool-1-thread-1. As far as I know third thread should go in queue and wait until one is done.
I've seen this happen when a ThreadPoolExecutor is configured with CallerRunsPolicy, which means when they queue is full the task is executed on the calling thread.