Bird
Raised Fist0
Spring Bootframework~20 mins

Why async processing matters in Spring Boot - Challenge Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. Why is async processing important in Spring Boot applications?
easy
A. It makes the application use more memory by duplicating tasks.
B. It forces all tasks to run one after another in sequence.
C. It allows tasks to run in the background without blocking the main thread.
D. It disables error handling for faster execution.

Solution

  1. Step 1: Understand async processing purpose

    Async processing lets tasks run separately from the main thread, so the app stays responsive.
  2. Step 2: Identify correct behavior in Spring Boot

    Spring Boot uses @Async to run methods in the background, improving speed and user experience.
  3. Final Answer:

    It allows tasks to run in the background without blocking the main thread. -> Option C
  4. Quick Check:

    Async means background tasks = B [OK]
Hint: Async means background work without blocking main thread [OK]
Common Mistakes:
  • Thinking async duplicates tasks unnecessarily
  • Believing async forces sequential execution
  • Assuming async disables error handling
2. Which annotation is used in Spring Boot to enable async processing on a method?
easy
A. @BackgroundTask
B. @EnableAsync
C. @AsyncMethod
D. @Async

Solution

  1. Step 1: Recall Spring Boot async annotations

    @Async is the annotation placed on methods to run them asynchronously.
  2. Step 2: Differentiate from other annotations

    @EnableAsync enables async support globally but is not placed on methods; @AsyncMethod and @BackgroundTask do not exist in Spring Boot.
  3. Final Answer:

    @Async -> Option D
  4. Quick Check:

    Method-level async annotation = A [OK]
Hint: Use @Async on methods to run them asynchronously [OK]
Common Mistakes:
  • Confusing @EnableAsync with @Async
  • Using non-existent annotations like @AsyncMethod
  • Applying @Async on classes instead of methods
3. What will be the output behavior of this Spring Boot method annotated with @Async?
@Async
public CompletableFuture<String> fetchData() {
    // simulate delay
    Thread.sleep(1000);
    return CompletableFuture.completedFuture("Done");
}
medium
A. The method immediately returns a CompletableFuture, running the delay in the background.
B. The method returns null because async methods cannot return values.
C. The method throws a compile-time error due to Thread.sleep in async method.
D. The method blocks the caller for 1 second before returning "Done".

Solution

  1. Step 1: Understand @Async with CompletableFuture

    @Async runs the method in a separate thread, so the caller gets a CompletableFuture immediately.
  2. Step 2: Analyze Thread.sleep effect

    The delay happens inside the async thread, not blocking the caller thread.
  3. Final Answer:

    The method immediately returns a CompletableFuture, running the delay in the background. -> Option A
  4. Quick Check:

    @Async returns immediately with CompletableFuture = D [OK]
Hint: Async method returns CompletableFuture immediately, delay runs in background [OK]
Common Mistakes:
  • Thinking Thread.sleep blocks the caller thread
  • Assuming async methods cannot return values
  • Believing async causes compile errors with sleep
4. Identify the error in this Spring Boot async method:
@Async
public void process() {
    CompletableFuture<String> result = CompletableFuture.supplyAsync(() -> {
        throw new RuntimeException("Fail");
    });
    System.out.println(result.get());
}
medium
A. Calling result.get() blocks the async thread, defeating async benefits.
B. Throwing RuntimeException inside supplyAsync is not allowed.
C. The method must return CompletableFuture<String>, not void.
D. Missing @EnableAsync annotation on the class.

Solution

  1. Step 1: Analyze use of result.get()

    Calling get() waits for the CompletableFuture to finish, blocking the async thread.
  2. Step 2: Understand async best practices

    Blocking defeats async purpose; exceptions inside supplyAsync are allowed and handled asynchronously.
  3. Final Answer:

    Calling result.get() blocks the async thread, defeating async benefits. -> Option A
  4. Quick Check:

    Blocking call inside async method = C [OK]
Hint: Avoid blocking calls like get() inside async methods [OK]
Common Mistakes:
  • Thinking exceptions inside supplyAsync are disallowed
  • Believing async methods must always return CompletableFuture
  • Forgetting to enable async globally (not always required)
5. You want to improve a Spring Boot app's responsiveness by running multiple independent tasks in parallel. Which approach best uses async processing to achieve this?
hard
A. Run all tasks sequentially in the main thread to avoid thread overhead.
B. Annotate each task method with @Async and return CompletableFuture, then combine results with CompletableFuture.allOf().
C. Use @Async but return void from all methods to simplify code.
D. Create new threads manually inside each method without @Async.

Solution

  1. Step 1: Identify best async pattern for parallel tasks

    Using @Async with CompletableFuture allows tasks to run in parallel and return results.
  2. Step 2: Combine results properly

    CompletableFuture.allOf() waits for all async tasks to finish, enabling coordinated processing.
  3. Final Answer:

    Annotate each task method with @Async and return CompletableFuture, then combine results with CompletableFuture.allOf(). -> Option B
  4. Quick Check:

    Parallel async tasks + combine results = A [OK]
Hint: Use @Async + CompletableFuture.allOf() for parallel task coordination [OK]
Common Mistakes:
  • Running tasks sequentially loses async benefits
  • Returning void loses ability to track task completion
  • Manually creating threads ignores Spring Boot async support
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard