如何用Java实现多线程池

发布于 2024-10-19 07:03:17 字数 373 浏览 5 评论 0原文

我有一个使用线程的场景。

首先,我有一个文件夹,里面有经常更新的文件。 因此,我编写了一个线程,它读取文件夹的内容并将文件名写入静态列表,并在有新文件进入时更新该列表。

其次,我编写了另一个线程,它从列表中获取文件名并进行一些处理文件。

这两个线程连续运行,一个检查新文件,一个处理新文件。

现在我需要在运行三个线程的情况下一次处理三个文件。当一个线程完成处理时,另一个线程从列表中获取另一个文件名并启动该进程。

因此,我需要某种机制来拥有三个线程并检查它们是否还活着,并相应地启动一个新线程,并且文件列表也会经常更新。

我还研究了 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 技术交流群。

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

发布评论

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

评论(4

吃颗糖壮壮胆 2024-10-26 07:03:17

基于现有的答案,您的代码看起来像这样:

    final ExecutorService executor = Executors.newFixedThreadPool(2);

    final FilePoller poller = ...
    final FileProcessor processor = ...

    new Thread(new Runnable() 
      { 
        @Override
        public void run() 
        {
          while (true)
          {
            final File file = poller.pollForFile();
            executor.submit(new Runnable() { public void run() { processor.processFile(file); } } );
          }
        }
      });

假设您的处理器可以跟上轮询器,这很好,否则您需要在提交给执行器之前添加一些限制机制。

Building on the existing answers, your code would look something like:

    final ExecutorService executor = Executors.newFixedThreadPool(2);

    final FilePoller poller = ...
    final FileProcessor processor = ...

    new Thread(new Runnable() 
      { 
        @Override
        public void run() 
        {
          while (true)
          {
            final File file = poller.pollForFile();
            executor.submit(new Runnable() { public void run() { processor.processFile(file); } } );
          }
        }
      });

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.

仄言 2024-10-26 07:03:17

不要使用列表;相反,使用 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.

长伴 2024-10-26 07:03:17

与@SimonC的建议类似,但我没有一个很长的评论,而是有一个答案。

final FilePoller poller = ...
final FileProcessor processor = ...

final ExecutorService executor = Executors.newFixedThreadPool(4);

executor.submit(new Runnable() { 
    public void run() {
        final File file = poller.pollForFile();
        executor.submit(new Runnable() { public void run() { processor.processFile(file); } } );
        // repeat, but wait for a free thread.
        executor.submit(this);
    }
  });
 // to stop the whole thing
 executor.shutdown();

Similar to @SimonC's suggestion but instead of a really long comment I have an answer.

final FilePoller poller = ...
final FileProcessor processor = ...

final ExecutorService executor = Executors.newFixedThreadPool(4);

executor.submit(new Runnable() { 
    public void run() {
        final File file = poller.pollForFile();
        executor.submit(new Runnable() { public void run() { processor.processFile(file); } } );
        // repeat, but wait for a free thread.
        executor.submit(this);
    }
  });
 // to stop the whole thing
 executor.shutdown();
知你几分 2024-10-26 07:03:17

假设通知更改为您提供了文件夹中的更改列表,那么监视文件夹中的更改并生成一个线程/文件怎么样?

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?

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