Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to create a thread?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to create a thread that probes that directory and then process when a file appears?
Thanks.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Kok Hwa Koh!
I'm moving this thread to the Threads and Synchronization forum...
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the only way to do this in pure Java would be to write a Thread with a run() method like
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though that only works if you already know the name of the file you're looking for. If you're looking for any new files in a directory, you can use listFiles() (in the File class) to see all the files in a given directory, then check the lastModified() method of each one to see if it's been recently changed (since the last time you checked that directory).
On some systems (Unix I think) you can shorcut this process by testing the lastModified() time of the directory itself - if new files have been added to the directory, the lastModified() time of the directory will show the time at whic files were added or deleted. So if that time has not changed, you don't need to check each individual file. However this doesn't work the same on all systems. So only rely on this if you've tested carefully on your system and you're sure the probram won't need to be run on a different system later. Hmmm, truth is, that's probably a dangerous assumption, so maybe you should just forget this paragraph unless it turns out that you need to detect new files really quickly and performance of the listFiles() method is a problem. Which is pretty unlikely I'd guess, but you never know.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also may find java.util.Timer to be convenient for setting up a TimerTask which checks your directory periodically.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!