• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PipedInputStreams for Threads

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic