如何让 Java 每分钟将 ArrayList 的内容写入文件一次?
关于上述主题的一个简单问题。基本上,我正在编写一个软件,它从网络捕获数据并将其写入外部文件以进行进一步处理。
我想知道的是最好使用什么代码才能获得所需的效果。
感谢您抽出时间
大卫
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能会使用 TimerTask 来实现它。例如,
否则 quarts 是一个功能强大的 java 调度程序,能够处理更高级的调度需求。
I'd probably implement it using a TimerTask. E.g.,
Otherwise quarts is a powerful java scheduler which is capable of handling more advanced scheduling needs.
您可以使用
Executor
框架,这里是一个示例实现:You can use the
Executor
Framework, here is a sample implementation:这个问题可能需要更多细节。解决方案的哪一部分给您带来了麻烦?
您还应该更详细地描述问题域,因为它可能会显着影响可能提供的任何解决方案。例如:
只是我的初步预感——在为您描述的问题设计解决方案时我的一些想法将涉及某种生产者-消费者方法。
我可能完全没有根据,但您从网络上阅读的事实意味着某种不可预测性和不可靠性。因此,我不依赖计时器,而是依赖数据生产者(从网络读取的对象)向消费者(将使用从网络读取的数据的对象)发出信号以对数据执行某些操作。
The question probably needs a few more details. What part of the solution is causing you trouble?
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:
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.
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.