Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

closing PrintWriter cuts output

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I uncomment the last lines, I only get one or two results (out of 3000 or so). The rest never print even though the run method is called (started) on a new StreamEater each time through the loop. I don't understand why. How can I close my PrintWriter without this problem?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marilyn,

PrintWriter.close(), like all good Writers, calls close() on the underlying stream. If you wrap a PrintWriter around System.out then closing it will close System.out -- not usually what you want. Once System.out is closed, it's gone -- no more output.

Since sometimes StreamEater should close the PrintWriter (assuming sometimes 'os' isn't System.out), and sometimes not, there are several different ways you could go. You could give StreamEater a boolean flag/constructor argument telling it whether the stream should be closed or not. Or you could move the responsibility for closing the stream to the class that constructs the StreamEater (tricky due to threads).

Or, since PrintWriter is always a "derived" stream, you could never close it; just flush it, and let the constructing code manage 'os'.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. Closing a PrintWriter (or any PrintStream, FilterWriter, FilterReader, or FilterXXXStream) has the effect of closing the underlying stream. The docs aren't 100% clear here (they just say it "releases any resources") but that's what happens.

I would say that your StreamEater really shouldn't close the PrintWriter here, because that closes the underlying os stream. StreamEater did not create this stream, and is not in a position to "know" whether it's OK to close that stream now or not. StreamEater should just flush the PrintWriter, and let whoever created the stream decide what to do with it later. In this case that's ClassFinder, which should close the FileStream just once after all the others are done with it.

That may seem odd to intentionally not close the PrintWriter in the class that created the PrintWriter. But the PrintWriter doesn't have any resources that need to be freed, except for (a) it needs to be flushed, and (b) the underlying stream may need to be closed.

I once had a situation where some other code outside my control was closing a stream I had sent it, and I needed to prevent that. I overrode FilterWriter to create a NonclosingFilterWriter which I used as a decorator around the stream I needed to protect. I overrode close() to simply have no effect. Possibly you might do something like that here if you have problems with other code closing something it shouldn't. But as long as you control all the pertinent code, that shouldn't be necessary.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Curse EFH and his faster typing!!!
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
I don't even know how to spell CIA. But this tiny ad does:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic