Skip to content

Commit 2b8d56e

Browse files
committed
feat(executors): implement factorial computation using ExecutorService with fixed thread pool
WHAT was added: - Java demo program (`Main`) showcasing `ExecutorService` with `Executors.newFixedThreadPool(3)`. - Computes factorials of numbers 1–9 concurrently across 3 worker threads. - Introduced `executor.submit()` for task submission instead of manual thread creation. - Used `executor.shutdown()` and `awaitTermination()` to gracefully stop the pool. - Added simulated workload via `Thread.sleep(1000)` to demonstrate concurrent execution. - Printed total execution time for performance comparison. WHY this matters: - Demonstrates how to offload CPU-bound or time-consuming tasks (factorial calculation) to a managed thread pool. - Highlights benefits of thread pooling: • Efficient reuse of worker threads. • Controlled concurrency (3 threads max at a time). • Automatic queuing of extra tasks until threads free up. - Shows best practices with graceful shutdown (`shutdown()` + `awaitTermination()`). HINTS / NEXT STEPS: - Try replacing `newFixedThreadPool(3)` with: • `newSingleThreadExecutor()` → sequential execution. • `newCachedThreadPool()` → dynamic thread allocation. - Use `Future<Long>` with `submit(Callable<Long>)` to capture and return factorial results. - Experiment with different pool sizes to measure execution time differences. KEYWORDS: executors executorservice threadpool factorial concurrency fixedthreadpool java-multithreading graceful-shutdown. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent c412915 commit 2b8d56e

File tree

1 file changed

+52
-0
lines changed
  • Section19MultiThreading/ThreadPooling/ExecutorsFramework/src

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* Author: Somesh Diwan
7+
* Date: 16-Sep-2025
8+
* Time: 19:49
9+
* Description: Demonstration of using ExecutorService with a fixed thread pool
10+
* to compute factorials concurrently.
11+
* © Somesh Diwan. All rights reserved.
12+
*/
13+
14+
public class Main {
15+
public static void main(String[] args) {
16+
long startTime = System.currentTimeMillis();
17+
18+
ExecutorService executor = Executors.newFixedThreadPool(3); //creating thread pool.
19+
20+
//Executors.newSingleThreadExecutor(); //there is lot's of method like this.
21+
22+
for (int i = 1; i < 10; i++) {
23+
int finalI = i;
24+
25+
executor.submit( () -> {
26+
long result = factorial(finalI);
27+
System.out.println(result);
28+
});
29+
}
30+
executor.shutdown(); //Manually shutdown.
31+
32+
try {
33+
executor.awaitTermination(100, TimeUnit.SECONDS);
34+
} catch (InterruptedException e) {
35+
throw new RuntimeException(e);
36+
}
37+
System.out.println("Total time: " + (System.currentTimeMillis() - startTime));
38+
}
39+
40+
private static long factorial(int n) {
41+
try {
42+
Thread.sleep(1000);
43+
} catch (InterruptedException e) {
44+
throw new RuntimeException(e);
45+
}
46+
long result = 1;
47+
for (int i = 1; i <= n; i++) {
48+
result *= i;
49+
}
50+
return result;
51+
}
52+
}

0 commit comments

Comments
 (0)