如何等到从Java的FTP服务器下载整个文件?
一个ThreadPool是从FTP服务器下载文件,另一个线程池是从中读取文件。
两个ThreadPool都同时运行。因此,到底会发生什么,我将以一个例子来解释您。
假设,我有一个带有100个记录的CSV文件。
当threadpool-1正在下载并将其写入待处理文件夹中的文件中,同时threadpool-2从该文件中读取内容,但是在1秒内假设只能将10个记录写入 /待定文件夹中的文件中ThreadPool -2仅读取10个记录。
ThreadPool -2不知道目前正在下载的90个记录。现在,ThreadPool -2不会读取90个记录,因为它不知道整个文件是否已下载。阅读后,它将在另一个文件夹中移动该文件。因此,我的90条记录将进一步继续。
我的问题是,如何等到下载整个文件,然后只有ThreadPool 2才能从文件中读取内容。
另一件事是,这两个线索都使用 scheplefixedrate 方法,并在每10秒内运行。
请指导我。
One ThreadPool is downloading files from the FTP server and another thread pool is reading files from it.
Both ThreadPool are running concurrently. So exactly what happens, I'll explain you by taking one example.
Let's assume, I've one csv file with 100 records.
While threadPool-1 is downloading and writing it in a file in pending folder, and at the same time threadpool-2 reads the content from that file, but assume in 1 sec only 10 records can be written in a file in /pending folder and threadpool - 2 reads only 10 record.
ThreadPool - 2 doesn't know about that 90 records are currently in process of downloading. Now, threadPool - 2 will not read 90 records because it doesn't know that whole file is downloaded or not. After reading it'll move that file in another folder. So, my 90 records will be proceed further.
My question is, how to wait until whole file is downloaded and then only threadPool 2 can read contents from the file.
One more thing is that both threadPools are use scheduleFixedRate method and run at every 10 sec.
Please guide me on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是Mark Rotteveel的#6建议的粉丝(在上面的评论中):
看起来像:
.pending
- 但无论您想要什么名称。some.pdf
-   ftp下载线程将文件写入some.pdf.pend.pending
some.pdf.perding
,然后将其重命名为some.pdf
。*。待处理
我使用此方法构建了系统,它们效果很好。相比之下,我还与更复杂的系统合作,这些系统试图跨线程协调,并且这些系统通常无法正常工作。
随着时间的流逝,任何软件系统都会有错误。 Edsger Dijkstra很好地抓住了这一点:
但是,很难理解程序正确性现在 -  当该程序仍处于设计阶段时,
而且尚未建立 -  当生产中损坏的情况时,要理解正确性(由于错误)时,将是更难的。
也就是说,当事情被打破并且您在时间的压力下找到根本原因(并修复它!),即使我们中最好的人都会处于不利地位
具有复杂的(与简单)系统。
使用临时名称的方法很容易理解,这应该最大程度地减少代码复杂性,从而使其更容易实现。
反过来,维护和错误修复也应该更容易。
保持简单 - 让文件系统为您提供帮助。
I'm a fan of Mark Rotteveel's #6 suggestion (in comments above):
That looks like:
.pending
– but name it whatever you want.some.pdf
– the FTP download thread writes the file tosome.pdf.pending
some.pdf.pending
, then at the end, renames it tosome.pdf
.*.pending
I've built systems using this approach and they worked out well. In contrast, I've also worked with more complicated systems that tried to coordinate across threads and.. those often did not work so well.
Over time, any software system will have bugs. Edsger Dijkstra captured this so well:
However difficult it is to reason about program correctness now – while the program is still in design phase,
and has not yet been built – it will be harder to reason about correctness when things are broken in production (which will happen, because bugs).
That is, when things are broken and you're under time pressure to find the root cause (and fix it!), even the best of us would be at a disadvantage
with a complicated (vs. simple) system.
The approach of using temporary names is simple to reason about, which should minimize code complexity and thus make it easier to implement.
In turn, maintenance and bug fixes should be easier, too.
Keep it simple – let the filesystem help you out.