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
.
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有