1

I have an application (a servlet, but that's not very important) that downloads a set of files and parse them to extract information. Up to now, I did those operations in a loop : - fetching new file on the Internet - analyzing it.

A multi-threaded download-manager seems a better solution for this problem and I would like to implement it in the fastest way possible.

Some of the downloads are dependant from others (so, this set is partially ordered).

Mutli-threaded programming is hard and if I could find an API to do that I would be quite happy. I need to put a group of files (ordered) in a queue and get the first group of files that is completely downloaded.

Do you know of any library I could use to achieve that ?

Regards, Stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173

1 Answers1

2

You could do something like:

BlockingQueue<Download> queue = new BlockingQueue<Download>();
ExecutorService pool = Executors.newFixedThreadPool(5);
Download obj = new Download(queue); 
pool.execute(obj); //start download and place on queue once completed
Data data = queue.take(); //get completely downloaded item

You may have to use a different kind of queue if the speed of each download is not the same. BlockingQueue is first in first out I believe.

You may want to look into using a PriorityBlockingQueue which will order the Download objects according to their Comparable method. See the API here for more details.

Hope this helps.

adamjmarkham
  • 2,150
  • 2
  • 18
  • 26
  • Thanks, that's exactly what I was hoping for, some oen who would redirect me to the new thread API... Old school java programmers have to recycle sometimes... :) – Snicolas Jun 25 '11 at 22:16