Код: Выделить всё
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
}
}
Я попробовал описанный выше способ и новичок в концепциях многопоточности, я не получаю ожидаемого результата и получаю исключения. Как реализовать описанный выше сценарий?
Подробнее здесь: https://stackoverflow.com/questions/793 ... ding-in-ja