This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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.
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.