-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutorsExample.java
More file actions
73 lines (60 loc) · 2.42 KB
/
ExecutorsExample.java
File metadata and controls
73 lines (60 loc) · 2.42 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.interview;
import java.util.Arrays;
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;
/*
Executors are part of the java.util.concurrent package and provide a high-level API for managing threads and concurrency.
Instead of manually creating and starting threads,
you can use an Executor to manage them more efficiently. No need to use Thread and Runnable
*/
public class ExecutorsExample {
// 1.
public void multipleTaskExecutors() {
ExecutorService executor = Executors.newFixedThreadPool(3); // 3 threads
//At most 3 threads will run in parallel at any given time.
//If you submit more than 3 tasks, the extra tasks will wait in a queue until a thread is free.
for (int i = 1; i <= 5; i++) {
int taskId = i;
executor.execute(() -> {
System.out.println("Running Task " + taskId + " by " + Thread.currentThread().getName());
});
}
executor.shutdown(); // Don't forget to shutdown
}
// 2.
public void invokeAllExample() throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newFixedThreadPool(3);
List<Callable<String>> tasks = Arrays.asList(
() -> "Task A by " + Thread.currentThread().getName(),
() -> "Task B by " + Thread.currentThread().getName(),
() -> "Task C by " + Thread.currentThread().getName()
);
List<Future<String>> results = executorService.invokeAll(tasks);
for (Future<String> future : results) {
System.out.println(future.get()); // blocking call
}
executorService.shutdown();
}
// 3.
public void multipleCallableTasks() throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newFixedThreadPool(3);
Callable<String> task1 = () -> {
return "Task1 completed by " + Thread.currentThread().getName();
};
Callable<String> task2 = () -> {
return "Task2 completed by " + Thread.currentThread().getName();
};
Future<String> future1 = executorService.submit(task1);
Future<String> future2 = executorService.submit(task2);
System.out.println(future1.get());
System.out.println(future2.get());
executorService.shutdown();
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
new ExecutorsExample().multipleCallableTasks();
}
}