排队多个下载,寻找生产者消费者 API

发布于 2024-11-16 23:01:00 字数 295 浏览 5 评论 0原文

我有一个应用程序(一个 servlet,但这不是很重要),它下载一组文件并解析它们以提取信息。到目前为止,我循环执行了这些操作: - 在互联网上获取新文件 - 分析它。

多线程下载管理器似乎是解决此问题的更好方法,我想以尽可能最快的方式实现它。

某些下载依赖于其他下载(因此,此集合是部分订购的)。

多线程编程很困难,如果我能找到一个 API 来做到这一点,我会很高兴。我需要将一组文件(已排序)放入队列中,并获取第一组完全下载的文件。

你知道我可以用什么库来实现这一目标吗?

问候, 史蒂芬

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2024-11-23 23:01:00

您可以执行以下操作:

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

如果每次下载的速度不同,您可能必须使用不同类型的队列。我相信 BlockingQueue 是先进先出的。

您可能需要考虑使用 PriorityBlockingQueue,它将根据下载对象的 Comparable 方法对它们进行排序。请参阅API 了解更多详细信息。

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文