forked from DhanushNehru/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithreading.java
More file actions
45 lines (39 loc) · 1.63 KB
/
Multithreading.java
File metadata and controls
45 lines (39 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Multithreading {
public static void main(String[] args) {
// Create a fixed thread pool with 4 threads
ExecutorService executorService = Executors.newFixedThreadPool(4);
// List to hold the Future objects
List<Future<Integer>> futures = new ArrayList<>();
// Submit 10 tasks to the executor
for (int i = 1; i <= 10; i++) {
final int taskId = i;
Callable<Integer> task = () -> {
// Simulate some computation
int result = taskId * 2; // Simple task: double the task ID
Thread.sleep(1000); // Simulate delay
System.out.println("Task " + taskId + " completed with result: " + result);
return result;
};
futures.add(executorService.submit(task));
}
// Shutdown the executor service
executorService.shutdown();
// Retrieve results from Future objects
for (Future<Integer> future : futures) {
try {
// Blocking call to get the result
Integer result = future.get();
System.out.println("Result: " + result);
} catch (InterruptedException | ExecutionException e) {
System.err.println("Error occurred while retrieving result: " + e.getMessage());
}
}
}
}