Callable和Supplier的区别

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 Callables, 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

posted @   xiaoxi666  阅读(1223)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
TOP
点击右上角即可分享
微信分享提示