A non-blocking server with java.io

前端 未结 1 845
春和景丽
春和景丽 2021-01-22 15:33

Everybody knows that java IO is blocking, and java NIO is non-blocking. In IO you will have to use the thread per client pattern, in NIO you can use one thread

1条回答
  •  温柔的废话
    2021-01-22 16:07

    It is possible but pointless. There is no select() in java.net, so you are reduced to polling the sockets, which implies sleeping between polls, and you can't tell how long to sleep for, so you will sleep for longer than necessary, so you will waste time, add latency, etc; or else you must sleep for stupidly short intervals and so consume pointless CPU.

    For a mere few hundred clients there is no possible objection to the conventional use of a thread per connection.

    I don't know what 'the client will always be ready for reading data' means. You can't tell that from the server, and if it isn't ready, writes to it can block, which will upset your applecard completely.

    0 讨论(0)
提交回复
热议问题