在Java中以一定的速率读取文件 [英] Read file at a certain rate in Java

查看:123
本文介绍了在Java中以一定的速率读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有一篇关于如何以特定比率读取长文件的文章/算法?假设我不想在发行时传递10 KB /秒阅读。

解决方案

粗略的解决方案是只读一块,然后睡觉,例如10K,然后睡一秒钟。但是我要问的第一个问题是:为什么?有几个可能的答案:


  1. 您不希望创造更快的工作;



  2. 您不希望在系统上创建太大的负载。我的建议是不要在阅读水平上进行控制。这是一种混乱和不准确的。而是在工作结束时进行控制。 Java有很多很棒的并发工具来处理这个问题。有几种方法可以做到这一点。



    我倾向于使用生产者消费者解决这类问题的模式。它给你很多选项,可以通过一个报告线程等来监视进度,它可以是一个非常干净的解决方案。



    类似于 ArrayBlockingQueue 可以用于那种(1)和(2)都需要节流。在容量有限的情况下,当队列满的时候读者最终会阻塞,所以不会太快。工人(消费者)可以被控制只能工作得如此之快,以遏制(2)的覆盖率。

    Is there an article/algorithm on how I can read a long file at a certain rate?

    Say I do not want to pass 10 KB/sec while issuing reads.

    解决方案

    The crude solution is just to read a chunk at a time and then sleep eg 10k then sleep a second. But the first question I have to ask is: why? There are a couple of likely answers:

    1. You don't want to create work faster than it can be done; or
    2. You don't want to create too great a load on the system.

    My suggestion is not to control it at the read level. That's kind of messy and inaccurate. Instead control it at the work end. Java has lots of great concurrency tools to deal with this. There are a few alternative ways of doing this.

    I tend to like using a producer consumer pattern for soling this kind of problem. It gives you great options on being able to monitor progress by having a reporting thread and so on and it can be a really clean solution.

    Something like an ArrayBlockingQueue can be used for the kind of throttling needed for both (1) and (2). With a limited capacity the reader will eventually block when the queue is full so won't fill up too fast. The workers (consumers) can be controlled to only work so fast to also throttle the rate covering (2).

    这篇关于在Java中以一定的速率读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆