如何用Java实现多线程池
我有一个使用线程的场景。
首先,我有一个文件夹,里面有经常更新的文件。 因此,我编写了一个线程,它读取文件夹的内容并将文件名写入静态列表,并在有新文件进入时更新该列表。
其次,我编写了另一个线程,它从列表中获取文件名并进行一些处理文件。
这两个线程连续运行,一个检查新文件,一个处理新文件。
现在我需要在运行三个线程的情况下一次处理三个文件。当一个线程完成处理时,另一个线程从列表中获取另一个文件名并启动该进程。
因此,我需要某种机制来拥有三个线程并检查它们是否还活着,并相应地启动一个新线程,并且文件列表也会经常更新。
我还研究了 ExecutorService,但是当列表更新时我无法提供更新的列表。
谢谢, 桑迪普
I have a scenario where I use threads.
Firstly I have a folder where there are files which get updated frequently.
So, I wrote a thread which reads the contents of the folder and writes the file names to a static list and updates the list if new files come in.
Secondly i wrote another thread which takes the file names from the list and do some processing with the files.
These two threads run continuously, one checking for new files, one processing the new files.
Now I need to process three files at a time with three threads running. When one thread completes processing another thread takes another file name from the list and starts the process.
So I need some mechanism to have three threads and checking them whether they are alive or not and accordingly starts a new thread and the file list also gets updated frequently.
I also looked into ExecutorService
but while the list get updated I could not provide it updated list.
Thanks,
Sandeep
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基于现有的答案,您的代码看起来像这样:
假设您的处理器可以跟上轮询器,这很好,否则您需要在提交给执行器之前添加一些限制机制。
Building on the existing answers, your code would look something like:
Assuming your processors can keep up with the poller this would be fine, otherwise you'd want to put in some throttling mechanism before submitting to the executor.
不要使用列表;相反,使用 java.util.concurrent.ThreadPoolExecutor 并将代表要处理的文件的 Runnable 放入执行器中,而不是将它们放入全局列表中。
Don't use a list; instead use a java.util.concurrent.ThreadPoolExecutor and just drop Runnable's representing the file to be processed into the executor instead of putting them into your global list.
与@SimonC的建议类似,但我没有一个很长的评论,而是有一个答案。
Similar to @SimonC's suggestion but instead of a really long comment I have an answer.
假设通知更改为您提供了文件夹中的更改列表,那么监视文件夹中的更改并生成一个线程/文件怎么样?
How about watching for changes in the folder and spawn a thread/file, assuming that the notification change is giving you a list of changes in the folder?