Spring / Spring Webflux Interview questions
Difference between Mono.fromCallable and Mono.fromRunnable.
In Project Reactor, the primary difference is that Mono.fromCallable creates a Mono that can return a value (or throw an exception), while Mono.fromRunnable creates a Mono that only signals completion without emitting any value (a Mono
| Feature | Mono.fromCallable | Mono.fromRunnable |
|---|---|---|
| Return Value | Emits at most one value (T). | Emits no value (Mono<Void>), only a completion signal. |
| Input Type | Takes a java.util.concurrent.Callable (has a call() method). |
Takes a java.lang.Runnable (has a run() method). |
| Exception Handling | Can throw checked exceptions, which are captured and propagated as onError. |
Cannot throw checked exceptions (only RuntimeExceptions are propagated). |
| Use Case | Wrapping blocking operations that return data (e.g., DB calls). | "Fire and forget" tasks like logging or status updates with no result needed. |
| Laziness | Lazy: Executed only upon subscription. | Lazy: Executed only upon subscription. |
In essence, fromCallable is for tasks that compute a result, and fromRunnable is for tasks that perform an action.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
