Bael 7641 (#16099)
* BAEL-7490 read write file in separate thread * Change the to try resources * Update the code to sync with article * code demostration for ExecutorService vs CompletableFuture
This commit is contained in:
parent
30b95b0161
commit
1d289c30b9
@ -28,8 +28,8 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>9</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>9</maven.compiler.target>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.baeldung.executorservicevscompletablefuture;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class CompletableFutureDemo {
|
||||||
|
|
||||||
|
public static void completableFutureMethod() {
|
||||||
|
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
|
||||||
|
return 42;
|
||||||
|
});
|
||||||
|
|
||||||
|
System.out.println(future.join());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void chainingTaskExample() {
|
||||||
|
CompletableFuture<Integer> firstTask = CompletableFuture.supplyAsync(() -> {
|
||||||
|
return 42;
|
||||||
|
});
|
||||||
|
|
||||||
|
CompletableFuture<String> secondTask = firstTask.thenApply(result -> {
|
||||||
|
return "Result based on Task 1: " + result;
|
||||||
|
});
|
||||||
|
|
||||||
|
System.out.println(secondTask.join());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void exceptionHandlingExample() {
|
||||||
|
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
|
||||||
|
// Simulate a task that might throw an exception
|
||||||
|
if (true) {
|
||||||
|
throw new RuntimeException("Something went wrong!");
|
||||||
|
}
|
||||||
|
return "Success";
|
||||||
|
})
|
||||||
|
.exceptionally(ex -> {
|
||||||
|
System.err.println("Error in task: " + ex.getMessage());
|
||||||
|
// Can optionally return a default value
|
||||||
|
return "Error occurred";
|
||||||
|
});
|
||||||
|
|
||||||
|
future.thenAccept(result -> System.out.println("Result: " + result));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void timeoutExample() {
|
||||||
|
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.err.println("Task execution timed out!");
|
||||||
|
}
|
||||||
|
return "Task completed";
|
||||||
|
});
|
||||||
|
|
||||||
|
CompletableFuture<String> timeoutFuture = future.completeOnTimeout("Timed out!", 2, TimeUnit.SECONDS);
|
||||||
|
String result = timeoutFuture.join();
|
||||||
|
System.out.println("Result: " + result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
timeoutExample();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
package com.baeldung.executorservicevscompletablefuture;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
public class ExecutorServiceDemo {
|
||||||
|
|
||||||
|
public static void executorServiceMethod() throws ExecutionException, InterruptedException {
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(3);
|
||||||
|
Future<Integer> future = executor.submit(() -> {
|
||||||
|
return 42;
|
||||||
|
});
|
||||||
|
|
||||||
|
System.out.println(future.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void chainingTaskExample() {
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||||
|
Future<Integer> firstTask = executor.submit(() -> {return 42;});
|
||||||
|
|
||||||
|
Future<String> secondTask = executor.submit(() -> {
|
||||||
|
try {
|
||||||
|
Integer result = firstTask.get();
|
||||||
|
return "Result based on Task 1: " + result;
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
// Handle exception
|
||||||
|
System.err.println("Error occured: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
executor.shutdown();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Wait for the second task to complete and retrieve the result
|
||||||
|
String result = secondTask.get();
|
||||||
|
System.out.println(result); // Output: Result based on Task 1: 42
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
// Handle exception
|
||||||
|
System.err.println("Error occured: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void exceptionHandlingExample() {
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||||
|
Future<String> future = executor.submit(() -> {
|
||||||
|
// Simulate a task that might throw an exception
|
||||||
|
if (true) {
|
||||||
|
throw new RuntimeException("Something went wrong!");
|
||||||
|
}
|
||||||
|
return "Success";
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
// This might block the main thread if the task throws an exception
|
||||||
|
String result = future.get();
|
||||||
|
System.out.println("Result: " + result);
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
// Handle exceptions thrown by the task or during retrieval
|
||||||
|
System.err.println("Error occured: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void timeoutExample() {
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||||
|
Future<String> future = executor.submit(() -> {
|
||||||
|
try {
|
||||||
|
System.out.println("Start");
|
||||||
|
Thread.sleep(5000);
|
||||||
|
System.out.println("End");
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.err.println("Error occured: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return "Task completed";
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
String result = future.get(2, TimeUnit.SECONDS);
|
||||||
|
System.out.println("Result: " + result);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
System.err.println("Task execution timed out!");
|
||||||
|
future.cancel(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error occured: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||||
|
timeoutExample();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user