Gopal
Gopal

Reputation: 1340

How are selectors implemented internally?

I've just started exploring java NIO, non-blocking IO. I'm interested to know the fundamentals behind the implementation. How is communication between Java selector and physical socket is established? Is there a operating system level thread that polls underlying resource continuously? And is there any java thread per selector continously polling to receive these events? Can someone of you kindly point me to this.

Upvotes: 16

Views: 3702

Answers (4)

Ivan
Ivan

Reputation: 776

I think it's better first give you a picture(take from other guy's blog)
old IO and NIO
(source: csdn.net)


Also some information get from that blog,

  1. For select implementation,it depends on OS. For epoll/select in *nix ENV, you can get more information from 《Unix network programming》
  2. And for notify/wakeup the select, the JVM also use different implementation, like TCP/IP on windows, pipes on *nix.

Upvotes: 0

Michael Barker
Michael Barker

Reputation: 14398

It depends on the operation system used. On Linux the current implementation use's the kernel's epoll mechanism.

Typically the underlying kernel network system is filling or draining buffers for the socket, probably on it's IRQ handling threads. So what you are waiting for is the kernel to tell you that a buffer is ready to be filled (writing) or read to be draining (reading).

Upvotes: 3

Kilian Foth
Kilian Foth

Reputation: 14386

No, the point of select is that you don't have to waste cycles polling when nothing is happening. Every OS implements this capability in some way or other (usually through hardware interrupts) and makes it available to user-space programs through the select() system call. The connection to the Java language is that the JVM now contains code that will call the OS's select for you if you use the right NIO classes and methods. But this required changes to the JVM code itself, it isn't something that you could have done purely within Java before NIO.

Upvotes: 7

aioobe
aioobe

Reputation: 421170

Since it is not specified in the documentation, I'd assume that (strictly speaking) this is implementation dependent.

However in *NIX and Windows the implementation typically relies directly on the select system call. This system call is not implemented by spawning multiple threads.

Upvotes: 4

Related Questions