import java.util.concurrent.*;
public class ConcurrentExample {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
int numPerThread = (endNum - startNum + 1) / numThreads;
CountDownLatch latch = new CountDownLatch(numThreads);
AtomicInteger sum = new AtomicInteger(0);
for (int i = 0; i < numThreads; i++) {
int start = startNum + i*numPerThread;
int end = (i == numThreads - 1) ? endNum : start + numPerThread - 1;
executorService.execute(() -> {
int localSum = 0;
for (int j = start; j <= end; j++) {
localSum += j * j;
}
sum.addAndGet(localSum);
latch.countDown();
});
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
executorService.shutdown();
}
}