How to do select() (from C library) in Java ?

C

ctt

Hi,

I'd like to do something similar to select() from C
where I have lots of open file descriptors (actually
sockets in my case) that I'd like to listen for read
ready condition.

From looking at Java's docs & examples, the easy way
to do this is to have a thread service each socket.
But I cannot have a thread wait on each of my socket
connections since I'm expecting 1000 (or maybe more)
sockets needing my attention.

I thought about decreasing threads' stack size to
give me more threads, but (1) that complicates my
design unnecessarily where my threads don't have too
much stack and have to ask another thread just to make
a few function calls (these functions allocates lots
of stack storage); (2) I'm working with JDK 1.3 and
can't seem to set Thread stacks individually on my
threads; (3) if there's something like the select(),
then all my problems is gone.

Thanks,
Ching Tai
 
J

John Davison

ctt said:
Hi,

I'd like to do something similar to select() from C
where I have lots of open file descriptors (actually
sockets in my case) that I'd like to listen for read
ready condition.

From looking at Java's docs & examples, the easy way
to do this is to have a thread service each socket.
But I cannot have a thread wait on each of my socket
connections since I'm expecting 1000 (or maybe more)
sockets needing my attention.

I thought about decreasing threads' stack size to
give me more threads, but (1) that complicates my
design unnecessarily where my threads don't have too
much stack and have to ask another thread just to make
a few function calls (these functions allocates lots
of stack storage); (2) I'm working with JDK 1.3 and
can't seem to set Thread stacks individually on my
threads; (3) if there's something like the select(),
then all my problems is gone.

Thanks,
Ching Tai

That's one of the things the NIO package solves.

java.nio.channels.Selector (see select())
java.nio.channels.SelectionKey

Of course this means you'll have to use SocketChannel instead of just
Socket.

John Davison
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top