in what cases do I need to use treads when writing a UDP server in java and in what other cases it's not necessary ?
2 Answers
You need threads when the requests are non-trivial.
For example, an echo or date or message-of-the-day server can produce the response practically instantaneously, so it doesn't need threads.
A DNS server on the other hand may have to delegate the request, and can't keep other clients waiting while it does so, so it would need threads, or select(), or async I/O.
2 Comments
You'll need threads if you want to process multiple client requests at the same time i.e. high throughput. Let's say for each incoming UDP request, you need to access the database and send a signal to another service. This processing can take a long time and can block new requests, affecting performance, if you are doing it all in the same thread. With a multithreaded approach, each incoming DatagramPacket will be given to a thread as soon it is received which does the processing in parallel with other requests.