Code: Select all
class Example {
@Autowired
private Service service;
public static void main(String[] args) {
long totalRows = service.getTotalRows(); // E.g., 976451 rows
int totalPages = (int) Math.ceil((double) totalRows / 20 ); //48823
ExecutorService executorService = Executors.newFixedThreadPool(20); //lets say 20 threads
// Fetch rows for each page and process
for (int pageNumber = 0; pageNumber < totalPages; pageNumber++) {
// so now i want to fetch each page with (0,48823 ) - assign to 1 thread
// than (1, 48823 ) - assign to another thread
// ..... same for all 20 threads....
// and simultaneously do the tasks analyzeMethod(row); processMethod(row);
Page all = service.getRowsByPagination(pageNumber, pageSize);
all.get().toList().forEach(row -> {
analyzeMethod(row);
processMethod(row);
});
}
}
public void analyzeMethod(Row row) {
// Perform database-related analysis
}
public void processMethod(Row row) {
// Additional processing
}
}
Ich habe die obige Methode ausprobiert und bin neu in Multithreading-Konzepten. Ich erhalte nicht die erwartete Ausgabe und erhalte Ausnahmen. Wie führt man also das obige Szenario durch?