0

I've looked all over but I could not find a good example explaining NIO2 or how to do asynchronous IO with Java sockets. For example, if I want to speed up a web crawler by allowing threads to use async IO to read from sockets instead of regular synchronous IO, how would I achieve this?

1 Answer 1

1

NIO2 is not faster than synchronous I/O. It allows to run many connections (tens of thousands) with a few threads. If you can afford to spend a thread for each connection, use synchronous I/O - it is simpler to program.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for the late reply. Can you clarify when would it be appropriate to use NIO2?
In other words, when would it be better to have fewer threads with many connections, verses more threads with a connection for each thread?
Threads take a lot of memory, so it is unpractical to have >1000 threads on a computer, and even powerful server machine hardly can bear more than 10000 threads at once. So the memory consumption is the main criteria. I would say that for <100 connections I would definitely use one thread per connection, and for >100 I would think if I can spend so much memory for threads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.