Java Thread Loading mulitple files from same directory
i want the strategy for loading files by multiple threads from a single directory,without accidentally loading same file by multiple threads.
Solution:
Allow each thread sequentially to collect set of files to add in their data structure such as arraylist and process parallel.
Take the filename , anyways the last 5 digits are numbers , do a
mod(last5digit,5) +1
will give1,2,3,4,5
and respectively those file with mod result will be processed by correspondingThread 1
,...Thread 5
.
i want a solution which should n开发者_运维百科ot have mutual exclusion problem.kindly let me know your comments from your experience.
Essentially any thread-safe access to a list should work. I think I would:
- put the list of files in a BlockingQueue
- have each thread continually take the next item (file) from the queue and process it.
Push all the files onto a BlockingQueue
and have your pool of threads pull files off the queue.
That way, no file will be accessed concurrently and there is no need for any mutual exclusion.
精彩评论