Skip to content

Commit efb0046

Browse files
committed
[BAEL-7073] change location of code
1 parent 596c10d commit efb0046

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.concurrent.executorservice;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
public class ExecuteExample {
7+
public static void main(String[] args) {
8+
ExecutorService executorService = Executors.newFixedThreadPool(2);
9+
// Task using Runnable
10+
Runnable task = () -> {
11+
int[] numbers = { 1, 2, 3, 4, 5 };
12+
int sum = 0;
13+
for (int num : numbers) {
14+
sum += num;
15+
}
16+
System.out.println("Sum calculated using execute:" + sum);
17+
};
18+
// Submit the task using execute
19+
executorService.execute(task);
20+
executorService.shutdown();
21+
22+
}
23+
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.concurrent.executorservice;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.Future;
8+
9+
public class SubmitExample {
10+
public static void main(String[] args) {
11+
ExecutorService executorService = Executors.newFixedThreadPool(2);
12+
Callable<Integer> task = () -> {
13+
int[] numbers = { 1, 2, 3, 4, 5 };
14+
int sum = 0;
15+
for (int num : numbers) {
16+
sum += num;
17+
}
18+
return sum;
19+
};
20+
// Submit the task and obtain a Future
21+
Future<Integer> result = executorService.submit(task);
22+
try {
23+
// Get the result
24+
int sum = result.get();
25+
System.out.println("Sum calculated using submit:" + sum);
26+
} catch (InterruptedException | ExecutionException e) {
27+
e.printStackTrace();
28+
}
29+
executorService.shutdown();
30+
}
31+
}

0 commit comments

Comments
 (0)