[BAEL-7073] fix code style

This commit is contained in:
uzma 2023-12-03 22:23:51 +00:00
parent 1c5e81fa34
commit 596c10de0a
2 changed files with 35 additions and 39 deletions

View File

@ -4,22 +4,21 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
public class ExecuteExample { public class ExecuteExample {
public static void main(String[] args) public static void main(String[] args) {
{ ExecutorService executorService = Executors.newFixedThreadPool(2);
ExecutorService executorService = Executors.newFixedThreadPool(2); // Task using Runnable
// Task using Runnable Runnable task = () -> {
Runnable task = () -> int[] numbers = { 1, 2, 3, 4, 5 };
{ int sum = 0;
int[] numbers = {1, 2, 3, 4, 5}; for (int num : numbers) {
int sum = 0; sum += num;
for (int num : numbers) { }
sum += num; System.out.println("Sum calculated using execute:" + sum);
} };
System.out.println("Sum calculated using execute:" + sum); // Submit the task using execute
}; executorService.execute(task);
// Submit the task using execute executorService.shutdown();
executorService.execute(task); executorService.shutdown();
} }
} }

View File

@ -7,28 +7,25 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
public class SubmitExample { public class SubmitExample {
public static void main(String[] args) public static void main(String[] args) {
{ ExecutorService executorService = Executors.newFixedThreadPool(2);
ExecutorService executorService = Executors.newFixedThreadPool(2); Callable<Integer> task = () -> {
Callable<Integer> task = () -> int[] numbers = { 1, 2, 3, 4, 5 };
{ int sum = 0;
int[] numbers = {1, 2, 3, 4, 5}; for (int num : numbers) {
int sum = 0; sum += num;
for (int num : numbers) { }
sum += num; return sum;
} };
return sum; // Submit the task and obtain a Future
}; Future<Integer> result = executorService.submit(task);
// Submit the task and obtain a Future try {
Future<Integer> result = executorService.submit(task); // Get the result
try { int sum = result.get();
// Get the result System.out.println("Sum calculated using submit:" + sum);
int sum = result.get(); } catch (InterruptedException | ExecutionException e) {
System.out.println("Sum calculated using submit:" + sum); e.printStackTrace();
} catch (InterruptedException | ExecutionException e) }
{ executorService.shutdown();
e.printStackTrace();
} }
executorService.shutdown();
}
} }