*
The moose likes Threads and Synchronization and the fly likes PipedInputStreams for Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "PipedInputStreams for Threads" Watch "PipedInputStreams for Threads" New topic
Author

PipedInputStreams for Threads

Dora Smith
Greenhorn

Joined: Oct 22, 2011
Posts: 3
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
wood burning stoves
 
subject: PipedInputStreams for Threads