I am trying to implement a really simple multi-threaded echo-back server.
I used a thread pool created using newFixedThreadPool, but it looks like the number of concurrent connections is fixed at nThreads(passed to newFixedThreadPool). For example, if I set nThreadsto 3, then the fourth client that connects cannot communicate with the server.
This is rather strange because the documentation says: "Creates a pool of threads in which it reuses a fixed number of threads working with a common unlimited queue."
Because the documentation also says that "If additional tasks are transferred when all threads are active, they will wait in line until the thread is available." I suspect my threads are never "freed", so they never become reusable.
I think this may be a stupid mistake, but I could not understand what happened. Here is my client handler method run(), which, I think, is very similar to the code found here ( clientbelow - it is just Socketconnected to the client):
@Override
public void run() {
try(BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()))) {
String input;
while(true) {
input = readLine();
if(input == null)
break;
out.write(input);
out.newLine();
out.flush();
}
} catch(IOException e) {
System.err.println(e, "Error from socket IO.");
System.exit(1);
}
try {
client.close();
} catch(IOException e) {
System.err.println(e, "Error when closing socket.");
System.exit(1);
}
System.out.println("Client " + client.getInetAddress().getHostAddress() +
" closed connection.\n");
}
1: , , . .
EDIT 2: , (listen - ServerSocket):
while(true) {
Socket client = null;
try {
client = listen.accept();
} catch(IOException e) {
System.err.println(e, "Error when waiting for connection.");
System.exit(1);
}
System.out.println("New connection from client: " +
client.getInetAddress().getHostAddress() + "\n"
);
threadPool.execute(new ConnectionHandler(client));
}
( ): this.threadPool = Executors.newCachedThreadPool();.
3: ( nThread = 3, ):
New connection from client: 127.0.0.1
Client 127.0.0.1 closed connection.
New connection from client: 127.0.0.1
Client 127.0.0.1 closed connection.
New connection from client: 127.0.0.1
Client 127.0.0.1 closed connection.
New connection from client: 127.0.0.1
user8680580