• 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

How to create a thread?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
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
I think the only way to do this in pure Java would be to write a Thread with a run() method like
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic