Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #24779

WatchService race condition?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject WatchService race condition?
Date Sat, 20 Jul 2013 15:09:15 -0400
Organization A noiseless patient Spider
Lines 35
Message-ID <ksemte$sut$1@dont-email.me> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sat, 20 Jul 2013 19:03:10 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="0d73d8cc209bff1c6395088b400d0605"; logging-data="29661"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Hv0MvgEGclE3IfeBIFBkb"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
Cancel-Lock sha1:Ih1MTSL2KFCSq1f0Er6DeIcTdUk=
Xref csiph.com comp.lang.java.programmer:24779

Show key headers only | View raw


     I had occasion recently to use java.nio.file.WatchService
for the first time, and have run across a problem I hope more
experienced practitioners can solve.  How do I iterate over the
current contents of a directory and then get notified of new
arrivals?  If I do

	iterate_over_the_files();
	register_with_watchservice();

... then there's a race: Any file created after iteration (or
after the directory gets read) but before registration gets
missed.  But if I do

	register_with_watchservice();
	iterate_over_the_files();

... then there's a different race: Any file created after
registration but before the iteration slurps the directory
is processed twice.

     I wound up keeping a Set of the files already processed,
and using the WatchService only as a "This would be a good
time to run another iteration" notice.  But that's clumsy: It
feels like there ought to be a sleeker way.  Any ideas?

     (Application: One program periodically fetches data from a
URL and dumps it into a time-stamped file in a known directory.
Another program reads the time-stamped files and extracts data
into a JFreeChart, which it would like to update whenever the
first program deposits a new file.  It's working, but ... ick.)

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

WatchService race condition? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-20 15:09 -0400
  Re: WatchService race condition? Arne Vajhøj <arne@vajhoej.dk> - 2013-07-20 16:46 -0400

csiph-web