A Callable
is "A task that returns a result, while a Supplier
is "a supplier of results". In other words a Callable
is a way to reference a yet-unrun unit of work, while a Supplier
is a way to reference a yet-unknown value.
It's possible that a Callable
could do very little work and simply return a value. It's also possible a Supplier
could do quite a lot of work (e.g. construct a large data structure). But generally speaking what you care about with either is their principle purpose. For example an ExecutorService
works with Callable
s, because it's primary purpose is to execute units of work. A lazy-loaded data store would use a Supplier
, because it cares about being supplied a value, without much concern about how much work that might take.
Another way of phrasing the distinction is that a Callable
may have side-effects (e.g. writing to a file), while a Supplier
should generally be side-effect free. The documentation doesn't explicitly mention this (since it's not a requirement), but I'd suggest thinking in those terms. If the work is idempotent use a Supplier
, if not use a Callable
.
摘自https://softwareengineering.stackexchange.com/questions/254311/what-is-the-difference-between-callablet-and-java-8s-suppliert