0
0
Spring Bootframework~20 mins

Why async processing matters in Spring Boot - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Mastery in Spring Boot
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Spring Boot controller method is annotated with @Async?
Consider a Spring Boot REST controller method annotated with @Async. What is the behavior when this method is called?
Spring Boot
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.CompletableFuture;

@RestController
public class AsyncController {

    @Async
    @GetMapping("/async")
    public CompletableFuture<String> asyncMethod() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return "Done";
        });
    }
}
AThe method throws an exception because @Async cannot be used on controller methods.
BThe method blocks the caller thread until the task completes and then returns the result.
CThe method runs in a separate thread and immediately returns a CompletableFuture to the caller.
DThe method runs synchronously but logs the execution asynchronously.
Attempts:
2 left
💡 Hint
Think about how @Async changes the thread behavior of the method.
state_output
intermediate
2:00remaining
What is the output when calling an async method without waiting for the result?
Given a Spring Boot service method annotated with @Async that returns a CompletableFuture, what will be printed if the caller does not wait for the CompletableFuture to complete?
Spring Boot
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;

@Service
public class AsyncService {

    @Async
    public CompletableFuture<String> fetchData() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return CompletableFuture.completedFuture("Data Ready");
    }
}

// Caller code (Spring-managed bean):
CompletableFuture<String> future = service.fetchData();
System.out.println("Result: " + future.getNow("Not Ready"));
AResult: Not Ready
BResult: Data Ready
CCompilation error because getNow cannot be used here
DRuntime exception due to calling getNow on incomplete future
Attempts:
2 left
💡 Hint
Check what getNow returns if the CompletableFuture is not done yet.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly enables async processing in Spring Boot?
To use @Async in a Spring Boot application, which of the following code snippets correctly enables async support?
A
@EnableTransactionManagement
@SpringBootApplication
public class App {}
B
@EnableAsync
@SpringBootApplication
public class App {}
C
@EnableScheduling
@SpringBootApplication
public class App {}
D
@Async
@SpringBootApplication
public class App {}
Attempts:
2 left
💡 Hint
Look for the annotation that activates async processing in Spring.
🔧 Debug
advanced
2:00remaining
Why does this async method not run asynchronously?
Given the following Spring Boot service, why does the async method run synchronously instead of asynchronously?
Spring Boot
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class MyService {

    @Async
    public void asyncTask() {
        System.out.println("Running async task");
    }

    public void callAsync() {
        asyncTask();
    }
}
ABecause asyncTask() is called from within the same class, Spring's proxy does not intercept the call to run it asynchronously.
BBecause the @Async annotation is missing the executor parameter.
CBecause the method asyncTask() must return a Future or CompletableFuture to be async.
DBecause the @Service annotation disables async processing.
Attempts:
2 left
💡 Hint
Think about how Spring proxies work for async methods.
🧠 Conceptual
expert
2:00remaining
Why is async processing important in web applications?
Which of the following best explains why asynchronous processing matters in web applications built with Spring Boot?
AIt automatically caches all responses to speed up future requests.
BIt guarantees that all tasks run in parallel, eliminating any waiting time completely.
CIt simplifies code by removing the need for error handling in background tasks.
DIt allows the server to handle more requests by freeing up threads while waiting for long tasks, improving scalability and user experience.
Attempts:
2 left
💡 Hint
Think about how threads and waiting affect server performance.