简体   繁体   English

使用多线程处理子目录中的文件

[英]Processing files in sub directories with multithreading

We have multiple admin directories inside that multiple files, I want to use multhreading in following way for processing files.我们在多个文件中有多个管理目录,我想以以下方式使用多线程处理文件。 1) There will be a single thread for each admin directories. 1) 每个管理目录将有一个线程。 2) The each admin assigned thread will be process the files sequentially in sub directories. 2)每个管理员分配的线程将依次处理子目录中的文件。

Well, I'm not going to do your homework for you, but I'll get you started.好吧,我不会为你做功课,但我会让你开始。

What you want is to do is to use an ExecutorService, which will manage the threads for you.您想要做的是使用 ExecutorService,它将为您管理线程。

First create a class that implements Runnable, with a member variable of a java.io.File, which will point to the subdirectory you are going to process Inside the class's run method, do whatever is you want to do for each subdirectory.首先创建一个实现Runnable的类,一个java.io.File的成员变量,它会指向你要处理的子目录。在类的run方法里面,对每个子目录做你想做的事情。

Then create your main class, which will first create an ExecutorService, then interrogate your top level directory to find the subdirectories.然后创建您的主类,它将首先创建一个 ExecutorService,然后查询您的顶级目录以查找子目录。 For each subdirectory, create a new instance of your class above and ask the ExecutorService to execute it.对于每个子目录,为上面的类创建一个新实例并要求 ExecutorService 执行它。

Here's a good example: https://examples.javacodegeeks.com/core-java/util/concurrent/executorservice/java-executorservice-example-tutorial/这是一个很好的例子: https : //examples.javacodegeeks.com/core-java/util/concurrent/executorservice/java-executorservice-example-tutorial/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM