开发者

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:

  1. Allow each thread sequentially to collect set of files to add in their data structure such as arraylist and process parallel.

  2. Take the filename , anyways the last 5 digits are numbers , do a mod(last5digit,5) +1 will give 1,2,3,4,5 and respectively those file with mod result will be processed by corresponding Thread 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜