• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PipedInputStreams for Threads

 
Dora Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My java web server creates a thread to service each request. When the thread is finished, it needs to send the information about the request it handled (the requesting host's IP, timestamp, etc.) to a special logging thread that writes everything to a file. I'm trying to do this with Piped Input/Output Streams:


But nothing is making it to the PipedInputStream in the loggingthread. Can anyone help me with this, or detail a better way to get information from one thread to another? I'm using the Executor class and a cachedthreadpool.
Thanks in advance.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you're using the available() method, which is almost always a bad idea in the first place. And you're using it badly, too. If your LoggingThread tries to read before the ServerThread has written anything, then the while loop you have there says "Anything available? No? Then we're done." Any data which comes in later doesn't get read.

Which is your problem description, right?

So get rid of available(). Just read from the input stream until you reach the end of the stream.

By the way, here's the Ranch's FAQ entry about that: Available Doesnt Do What You Think It Does.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic