如何让 Java 每分钟将 ArrayList 的内容写入文件一次?

发布于 2024-10-17 18:32:00 字数 121 浏览 7 评论 0原文

关于上述主题的一个简单问题。基本上,我正在编写一个软件,它从网络捕获数据并将其写入外部文件以进行进一步处理。

我想知道的是最好使用什么代码才能获得所需的效果。

感谢您抽出时间

大卫

Just a quick question about the above subject. Basically, I'm writing a piece of software which captures data from the network and writes it to an external file for further processing.

What I want to know is what code would be the best to use in order to get this desired effect.

Thanks for your time

David

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

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

发布评论

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

评论(3

我做我的改变 2024-10-24 18:32:01

我可能会使用 TimerTask 来实现它。例如,

int hour = 1000*60*60;
int delay = 0;
Timer t = new Timer();

t.scheduleAtFixedRate(new TimerTask() {
    public void run() {
        // Write to disk ...
    }
}, delay, hour);

否则 quarts 是一个功能强大的 java 调度程序,能够处理更高级的调度需求。

I'd probably implement it using a TimerTask. E.g.,

int hour = 1000*60*60;
int delay = 0;
Timer t = new Timer();

t.scheduleAtFixedRate(new TimerTask() {
    public void run() {
        // Write to disk ...
    }
}, delay, hour);

Otherwise quarts is a powerful java scheduler which is capable of handling more advanced scheduling needs.

黎歌 2024-10-24 18:32:01

您可以使用 Executor 框架,这里是一个示例实现:

final List<String> myData = new ArrayList<String>();
final File f = new File("some/file.txt");

final Runnable saveListToFileJob = new Runnable(){

    @Override
    public void run(){ /* this uses Guava */
        try{
            Files.write(
                Joiner.on('\n').join(myData),
                f, Charsets.UTF_8);
        } catch(final IOException e){
            throw new IllegalStateException(e);
        }

    }
};
Executors
    .newScheduledThreadPool(1) /* one thread should be enough */
    .scheduleAtFixedRate(saveListToFileJob,
        1000 * 60 /* 1 minute initial delay */,
        1, TimeUnit.MINUTES /* run once every minute */);

You can use the Executor Framework, here is a sample implementation:

final List<String> myData = new ArrayList<String>();
final File f = new File("some/file.txt");

final Runnable saveListToFileJob = new Runnable(){

    @Override
    public void run(){ /* this uses Guava */
        try{
            Files.write(
                Joiner.on('\n').join(myData),
                f, Charsets.UTF_8);
        } catch(final IOException e){
            throw new IllegalStateException(e);
        }

    }
};
Executors
    .newScheduledThreadPool(1) /* one thread should be enough */
    .scheduleAtFixedRate(saveListToFileJob,
        1000 * 60 /* 1 minute initial delay */,
        1, TimeUnit.MINUTES /* run once every minute */);
青丝拂面 2024-10-24 18:32:01

这个问题可能需要更多细节。解决方案的哪一部分给您带来了麻烦?

  • 日程安排?
  • 是从网络上读到的吗?
  • 内存数据结构的持久性?
  • 写入文件?

您还应该更详细地描述问题域,因为它可能会显着影响可能提供的任何解决方案。例如:

  • 通过网络传输的数据的性质是什么?
  • 必须是每分钟吗?如果网络尚未完成发送并且时间已到并且您开始阅读怎么办?
  • ArrayList到底包含什么?
  • 你能描述一下文件输出吗? (文本文件?序列化对象?等等...)

只是我的初步预感——在为您描述的问题设计解决方案时我的一些想法将涉及某种生产者-消费者方法。

  • 一个 Runnable/Thread 对象,其唯一职责是连续从网络读取、组装数据并将其放入同步队列中。
  • 处于 wait() 状态的 Runnable/Thread 对象观察同步队列。当收到队列信号时,它将开始读取队列的内容以持久保存到文件中。
  • 当队列中有项目时(或达到某个阈值时),它将通知()等待队列读取器开始消耗队列中的对象以进行持久化。

我可能完全没有根据,但您从网络上阅读的事实意味着某种不可预测性和不可靠性。因此,我不依赖计时器,而是依赖数据生产者(从网络读取的对象)向消费者(将使用从网络读取的数据的对象)发出信号以对数据执行某些操作。

The question probably needs a few more details. What part of the solution is causing you trouble?

  • The scheduling?
  • The reading from the network?
  • The persistence in a memory data structure?
  • The writing to a file?

You should also describe the problem domain a little bit more in detail since it might significantly affect any solution that might be offered. For example:

  • What is the nature of the data going over the network?
  • Does it have to be per minute? What if the network isn't done sending and the minute is up and you start reading?
  • What exactly does the ArrayList contain?
  • Can you describe the file output? (text file? serialized object? etc...)

Just an initial hunch on my part --some thoughts I would have when designing a solution for the problem you described would involve some kind of Producer-Consumer approach.

  • A Runnable/Thread object whose sole responsibility is to continuously read from the network, assemble the data and place it in a synchronized queue.
  • A Runnable/Thread object in a wait() state observing the synchronized queue. When signaled by the queue, it will start reading the contents of the queue for persistence to a file(s).
  • When there are items in the queue (or when a certain threshold is reached) it will notify() the waiting queue reader to start consuming the objects from the queue for persistence.

I maybe completely off base, but the fact that you're reading from the network implies some sort of unpredictability and unreliability. So instead of relying on timers, I would rely on the Producer of data (object reading from the network) signal the Consumer (the object that will use the data read from the network) to do something with the data.

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