• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threaded File Checker

 
Mark Mero
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking to create a file checker that will check the availability of multiple files. I want this to be threaded so that multiple files can be checked for at the same time.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To run a task on multiple threads, encapsulate the task code in its own class. Make the task class implement Runnable and put the task code in the run() method. Then you can do something like this:

I'll let you work out Task ... the run() method contains the file check code and saves the result in a member variable.

That's real sketchy ... is it enough to go on?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Mero:
I am looking to create a file checker that will check the availability of multiple files. I want this to be threaded so that multiple files can be checked for at the same time.


Forgive me if you have already looked into this issue, but it seems to me that this might not be a good application for multi-threading. If all the files are on the same file system, then checking for lots of files "all at once" may not achieve improved performance.
 
Mark Mero
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if i didn't explain myself very well.

The thread will target a file checking class that will loop through the file definitions until all files have been received.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic I showed above would do the trick if each task looped inside its run method until the file existed. But it would be overkill. You don't need a thread per file, just one thread for the checker. Here's a possibility not quite in Java ...

At any moment the collection contains a list of files not yet found if anybody else is interested. You might have to synchronize any get on the remaining list.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic