Я пытаюсь читать файлы в нескольких потоках. Я хочу прочитать их, а затем обработать файлы. В каждом файле содержится около 30 файлов с 10000 JSON записями. Я хочу ограничить количество потоков до 3 или 4. Ниже приведен код:
public class FileByMultiThread implements RemoteAccess, Serializable {
private static final long serialVersionUID = 1L;
public void main(String args) throws WTException, InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(3);
System.out.println("Execution Started");
File[] files = new File("D:\\datafiles\\").listFiles();
List<File> fileList = Arrays.asList(files);
SessionContext session = SessionContext.getContext();
for (File file : fileList) {
executor.execute(() -> new SessionThread(new FileList(file), session).start());
}
executor.shutdown();
executor.awaitTermination(2, TimeUnit.HOURS);
}
}
public class FileList implements Runnable {
File fileLoc;
FileList(File file) {
this.fileLoc = file;
}
public void run() {
readFile(fileLoc);
}
private void readFile(File fileLoc2) {
try {
System.out.println("Thread Name is: " + Thread.currentThread().getName());
FileReader fr = new FileReader(fileLoc2);
BufferedReader br = new BufferedReader(fr);
readBatch(br);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Я запускаю этот код в конкретном c приложении. SessionContext - получить сеанс в этом приложении. Несмотря на то, что я ограничил потоки до 3 в ExecutorService, он создает около 30 потоков.
2020-03-02 04:25:51,664 INFO [RMI TCP Connection(33)] wt.system.out - Execution Started
2020-03-02 04:25:51,669 INFO [SessionThread-1] - Thread Name is: SessionThread-1
2020-03-02 04:25:51,669 INFO [SessionThread-3] - Thread Name is: SessionThread-3
2020-03-02 04:25:51,674 INFO [SessionThread-5] - Thread Name is: SessionThread-5
2020-03-02 04:25:51,674 INFO [SessionThread-8] - Thread Name is: SessionThread-8
2020-03-02 04:25:51,674 INFO [SessionThread-10] - Thread Name is: SessionThread-10
2020-03-02 04:25:51,674 INFO [SessionThread-4] - Thread Name is: SessionThread-4
2020-03-02 04:25:51,674 INFO [SessionThread-2] - Thread Name is: SessionThread-2
2020-03-02 04:25:51,685 INFO [SessionThread-9] - Thread Name is: SessionThread-9
2020-03-02 04:25:51,689 INFO [SessionThread-6] - Thread Name is: SessionThread-6
2020-03-02 04:25:51,689 INFO [SessionThread-21] - Thread Name is: SessionThread-21
2020-03-02 04:25:51,694 INFO [SessionThread-12] - Thread Name is: SessionThread-12
2020-03-02 04:25:51,694 INFO [SessionThread-11] - Thread Name is: SessionThread-11
2020-03-02 04:25:51,694 INFO [SessionThread-30] - Thread Name is: SessionThread-30
2020-03-02 04:25:51,694 INFO [SessionThread-16] - Thread Name is: SessionThread-16
2020-03-02 04:25:51,699 INFO [SessionThread-29] - Thread Name is: SessionThread-29
2020-03-02 04:25:51,699 INFO [SessionThread-28] - Thread Name is: SessionThread-28
2020-03-02 04:25:51,704 INFO [SessionThread-25] - Thread Name is: SessionThread-25
2020-03-02 04:25:51,704 INFO [SessionThread-23] - Thread Name is: SessionThread-23
2020-03-02 04:25:51,709 INFO [SessionThread-20] - Thread Name is: SessionThread-20
2020-03-02 04:25:51,709 INFO [SessionThread-7] - Thread Name is: SessionThread-7
2020-03-02 04:25:51,709 INFO [SessionThread-15] - Thread Name is: SessionThread-15
2020-03-02 04:25:51,714 INFO [SessionThread-17] - Thread Name is: SessionThread-17
2020-03-02 04:25:51,714 INFO [SessionThread-27] - Thread Name is: SessionThread-27
2020-03-02 04:25:51,719 INFO [SessionThread-14] - Thread Name is: SessionThread-14
2020-03-02 04:25:51,724 INFO [SessionThread-26] - Thread Name is: SessionThread-26
2020-03-02 04:25:51,724 INFO [SessionThread-24] - Thread Name is: SessionThread-24
2020-03-02 04:25:51,739 INFO [SessionThread-13] - Thread Name is: SessionThread-13
2020-03-02 04:25:51,744 INFO [SessionThread-18] - Thread Name is: SessionThread-18
2020-03-02 04:25:51,744 INFO [SessionThread-19] - Thread Name is: SessionThread-19
2020-03-02 04:25:51,769 INFO [SessionThread-22] - Thread Name is: SessionThread-22