Hello. I am trying to make a thread pool for a server that will process incoming socket connections. The purpose is to have limited available connections at a time.
I can't grasp this concept fully to implement it. Perhaps some of you can help me! Please try.
Below is my current thread "launcher"
while (listening){
System.out.println("Listening...");
new MyServerThread(serverSocket.accept()).start();
}
But i don't understand how to do it through a fixedThreadPool.
My speculations
As far as i understand, though, is that i need to satisfy the following steps: (Correct me if i'm wrong!)
* Design a ThreadFactory that will create my threads.
* Make a pool of desired maxNr of threads; associate my ThreadFactory to it.
* On an incoming connection: invoke any thread, sending to it the client's socket connection. (after the thread will finish it will wipe client's socket connection)
But the problem here is that i can't find a way to send the client's socket to the thread.
Possbile solution
I could instantiate all the threads beforehand, then launch them all to race for the first client. But that would mean that i have to constantly launch the ones that have just finished, thus currentRunningThreads=maxNrOfThreads, always. Totally not elegant.