https://stackoverflow.com/questions/52215917/callable-vs-supplier-interface-in-java
What is diffrent between Callable and Supplier interface?
In documentation
Callable
Package: java.util.concurrent
A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call.
The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread.
A Runnable, however, does not return a result and cannot throw a checked exception.
The Executors class contains utility methods to convert from other common forms to Callable classes.
Supplier
Package: java.util.function
Represents a supplier of results.
There is no requirement that a new or distinct result be returned each time the supplier is invoked.
This is a functional interface whose functional method is get().
Results
This means that the caller of Callable.call expects an exception to be thrown and will handle the exception accordingly. This is useful for tasks like reading and writing to files, where many kinds of IOExceptions can be thrown. Callable is also designed to be run on another thread.
Supplier on the other hand, is very general. It just "supplies a value" and that's it.
So Callable is more specialised than Supplier. If you are not dealing with another thread or your task is very unlikely to throw an exception, Supplier is recommended.
cf) Basic Functional Interface Ins and Outs
0 in (arguments) | 1 in (argument) | |
0 out (returned) | Runnable | Consumer |
1 out (returned) | Supplier | Function |
'개발' 카테고리의 다른 글
Java21 - Virtual Thread (0) | 2023.08.17 |
---|---|
How Do I Wrap a Synchronous, Blocking Call (0) | 2023.07.04 |
Thread Pool (0) | 2023.07.03 |
Retry (0) | 2023.07.03 |
[Redis] Replication / Cluster / Sentinel (0) | 2023.06.18 |