javasocketsdatagram

How can I emulate non-blocking i/o in Java using threads


I'm writing a simple application involving a server and many clients at a time. I have to use DataGramSocket. The application is simple exchange of messages through console like in chatting.

However, both operations in.readLine() and ds.receive() are blocking and putting them in separate threads didn't help as blocking i/o will block the thread too. How can I do that without nio?


Solution

  • If you have threads which are dedicated only to sending or receiving data over network, then thread blocking won't be a problem, as only that dedicated thread will be blocked.

    Then, let's consider impact of this solution on number of threads in your application:

    If you interested in general task of handling lots of clients, here is the classic web paper on this question.