Jump to content

Listening to several InputStreams at the same time

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
I have a problem that I'm having trouble coming up with a good idea for.

I have an array of InputStreams, and I want to make a method called listen, that listens to all of these InputStreams. Whenever an InputStream receives data, it's stored into a byte array called input.

Now the problem here is two-fold. I want to run this listen method constantly, while I also run the rest of my program, that is, my program doesn't lock up due to waiting for some input. But I'm not sure how to achieve this. I'm using an anonymous inner class Thread right now, but it seems to me that there must be a better, easier way.

Also, since I'm storing the data into the same variable regardless of which InputStream receives it, I naturally want any InputStreams to wait with overwriting the input variable, until the InputStream currently accessing it is done with it. How would I accomplish this?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Threads with resource locks.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
So there's no other way than using Threads to achieve this? No keyword or anything I can add instead?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If you want them to all be able to listen without pausing the program, you're looking at doing either threads or some form of frequent polling.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog