0

I have an application that needs to read hundreds of socket communications.

I am using a ThreadPool, with a upper limit on the number of threads, to service these sockets. This causes blocking on all threads if the sockets do not have incoming messages.

I am currently using a soTimeout of 100ms to avoid a permanent blocking. I do not like this approach as it might timeout just as it starts receiving input.

Is there anyway other to approach this?

I tried checking with ObjectInputStream.isAvailable(), but that always returns 0, whether there is data in the stream or not.

I can't find any other way to check whether there is data on the stream. This would be ideal, as then I could check if there is data, if not then move on to next stream.

4
  • Is there any reason you don't have one thread per connection. It does in cure some overhead but works fine for hundreds of threads. Commented Jul 31, 2011 at 13:15
  • That is true, but yes there is actually, my fourth and fifth comment on Sanjay's answer states why I don't want one thread per connection. Commented Jul 31, 2011 at 13:39
  • I just thought about peeking into the ObjectInputStream, if there is something, to push it back and read, else unread. Perhaps using something like PushBackStreams What do you guys think? Commented Jul 31, 2011 at 13:44
  • 1
    If you have 10K threads, you can have about one whole core busy with swapping threads. You cannot use PushBackStreams with ObjectInputStream as the stream is stateful. Commented Jul 31, 2011 at 17:30

2 Answers 2

1

This is exactly the kind of problem NIO frameworks are meant to solve. Unfortunately, using raw NIO is a bit more difficult than using blocking IO. If you can, my recommendation would be to try out a framework like Netty which would ease the job for you.

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

9 Comments

I will most definitely do so. As I am limited on time, and cannot for my current situation go read up on NIO and re-implement the entire application in time, isany approach other than this, that you could recommend?
Netty is pretty well documented and comes with a good amount of sample code. If that still is a problem timewise, Peter brings up a good point. Just use a cachedThreadPool instead of a pool with hard limits. Is there any problem you are facing with a large number of threads in your system?
Well, no not specifically. The reason was that I tested, and found some articles online that showed the memory overhead of creating threads to manage a large number of connections can become alarmingly large quite quickly.
I'm thinking along the lines of, say 10 000 clients. That means I've got 10 000 thread objects. Possible multiple thousands might be unblocking at nearly the exact same time. Since each client can broadcast to many others (say a chatroom), I will have to manage massive contention issues when I have such a large number of threads, which I of course want to avoid.
OK, first off, are you sure it's 10K? Secondly, 10K "active" clients is some serious traffic. On my 4 core machine with 3GiB RAM and 32 bit Windows OS, I can spawn around 11K threads having 128K stack size for each thread. If you have some sort of awesome hardware (at least 8Gib RAM and 8 cores), spawning 10K threads shouldn't be a big deal. But some food for thought for you: do you really have a requirement of 10K "active" clients?
|
1

You can give NIO a chance. Use Selector and SocketChannels to wait for data instead of creating thread for each socket.

Selector

SocketChannel

2 Comments

I will most definitely do so. As I am limited on time, and cannot for my current situation go read up on NIO and re-implement the entire application in time, isany approach other than this, that you could recommend?
I have a pretty wild idea. When you open a socket, take it's InputStream, and wrap it with your own implementation that takes timeout and returns if no data arrived. That way you could run serial on every socket's InputStream and check if there is an available data. (I guess thats a bit like NIO..)

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.