Junior — Middle
Do you have experience developing with multithreading or asynchronous task execution?
sobes.tech AI
Answer from AI
Yes, I have experience developing with both multithreading and asynchronous methods in Java.
For multithreading, I used classes from the java.util.concurrent package, for example, ExecutorService to manage thread pools:
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(() -> {
// task code
System.out.println("Executing in a separate thread");
});
executor.shutdown();
For asynchronous programming, I used CompletableFuture, which allows writing non-blocking code with result and error handling:
CompletableFuture.supplyAsync(() -> {
// long-running operation
return "Result";
}).thenAccept(result -> {
System.out.println("Received result: " + result);
});
This approach helps efficiently utilize resources and improve application responsiveness.