Blocking IO thread-per-connection model: possible to avoid polling?
Hello,
I'm firstly implementing this "thread-per-connection model" on the Server
component where one thread is responsible for reading the requests and
sending results back to the client, it is not responsible for actually
processing the requests though. I can gracefully (cleanup) stop the thread
by doing socket.getChannel().close() see the snippet below. However, in
order to send data, I also need to interrupt the Thread while it is blocked
waiting for input. Apparently the only way to do this without closing the
channel as side effect is to do polling?
TIA,
Best regards,
Giovanni
ObjectInputStream in = null;
try {
in = new ObjectInputStream(clientSocket.getInputStream());
while (true) {
try {
// >>> it is blocked here <<<
MessageData data = (MessageData) in.readObject();
// add requests to the BlockingQueue for processing
requestQueue.add(new Request(data, this));
// >>> send stuff here <<<
// if (resultAvailable()) {
// out.writeObject(result);
// }
}
catch (ClosedChannelException exception) {
// stop requested
break;
}
}
}
catch (IOException exception) {
exception.printStackTrace();
throw new RuntimeException(exception);
}
catch (ClassNotFoundException exception) {
exception.printStackTrace();
throw new RuntimeException(exception);
}
finally {
try {
in.close();
}
catch (IOException exception) {
exception.printStackTrace();
}
}