Reputation: 486
Thread Pool Executor uses number of threads for Future Task. It assigns at least one thread until run() or call() returns.
So, I am confused on how to use Thread Pool for JAVA NIO HTTP requests.
1) Which thread will run the event loop
2) As threads won't block on IO, They will exit the run/call method. Then who takes care of invoking their handlers.
My Question is how to use Java NIO based HTTP POST client requests with thread pools ( Because of the High number of HTTP requests that we need to make), Or with Java NIO it is really not needed to run them on multiple threads, as the thread will never sleeps ( It always executes as there is nothing to block)
Upvotes: 1
Views: 2559
Reputation: 13525
Okay, this is the philosophy. Multithreading can be used in 2 flavours (may be more, but it is not significant in this case). Traditional multithreading uses threads and blocking I/O. Task-grained multithreading (please suggest better term) is built upon traditional one and exploits following restrictions:
Runnable
is a unit of work;Runnable.run()
).Tasks are submitted for execution to a thread pool.
Asynchronous I/O perfectly fits in task-oriented model. Futures are glue between thread-oriented and task-oriented programming styles. They can be used to pass information from tasks to threads, but not in opposite direction, because tasks may not block. So you need not to use Futures at all when designing fully asynchronous server.
NIO1 requires a selector thread. Create and run it separately of the thread pool. NIO2 (java7) does not require selector thread (it maintains it in the background so that user does not to bother).
It is easy to find examples of NIO servers. df4j has examples of echo-servers (but not http servers) for both NIO1 and NIO2.
Upvotes: 3