Callable和Supplier的区别
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 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.
Callable和Supplier的区别的更多相关文章
- 在线程池使用Callable和Runnable的区别以及如何关闭线程
一.区别总结: Callable定义的方法是call,而Runnable定义的方法是run. Callable的call方法可以有返回值,而Runnable的run方法不能有返回值,这是核心区别. C ...
- Callable 和 Runnable 的区别
Callable 和 Runnable 的使用方法大同小异, 区别在于: 1.Callable 使用 call() 方法, Runnable 使用 run() 方法 2.call() 可以返回值, 而 ...
- callable和runnable的区别
Runnable接口源码 @FunctionalInterface public interface Runnable { /** * When an object implementing inte ...
- 浅谈线程runnable和callable的使用及区别
线程使用比较广泛,但实际上一般项目很少用上线程,线程常用于优化复杂的程序执行流程,把一些与业务关系关系不大但是必须要执行的流程使用线程的方式让子线程去执行,主流程只返回跟业务有关的信息 runnabl ...
- Callable和Future的区别
Callable 在Java中,创建线程一般有两种方式,一种是继承Thread类,一种是实现Runnable接口.然而,这两种方式的缺点是在线程任务执行结束后,无法获取执行结果.我们一般只能采用共享变 ...
- java 8 supplier object区别
起初用supplier的时候,发现用法和object一样呀,只是一个用于生产object工厂方法而已,为什么要新引起这样一个对象呢. 后面查到这篇英语文档,才发现,相比object有如下三个用法: ( ...
- Callable与Future、FutureTask的学习 & ExecutorServer 与 CompletionService 学习 & Java异常处理-重要
Callable是Java里面与Runnable经常放在一起说的接口. Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务 ...
- Callable与Future的简单介绍
Callable与Future的介绍 Callable与 Future 两功能是Java在后续版本中为了适应多并法才加入的,Callable是类似于Runnable的接口,实现Callable接口的类 ...
- Runnable、Callable、Future和FutureTask用法
http://www.cnblogs.com/dolphin0520/p/3949310.html java 1.5以前创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable ...
随机推荐
- namespace" 或The content of element type "mapper" must match "EMPTY"
必须为元素类型 "mapper" 声明属性 "namespace" 或The content of element type "mapper" ...
- 如何决定使用 HashMap 还是 TreeMap?(未完成)
如何决定使用 HashMap 还是 TreeMap?(未完成)
- 牛客练习赛48 E 小w的矩阵前k大元素
E 思路: 优先队列,将迭代器变量作为结构体的变量. 迭代器走的时候只能像一个方向走,另外一个方向只有最开始才走.如下图所示: 如果两个方向同时走,同一个值会被遍历多次,像上图那样就能保证每个位置都走 ...
- systemctl automatically restart service
systemctl automatically restart service Pothi Kalimuthu Linux October 9, 2016 0 Minutes Reference ...
- Orchard Core 使用工作流处理页面提交
上一篇文章中:Orchard Core 使用工作流处理审批和创建内容项 我们介绍了如何使用工作流处理审批,通过此文章我们了解到工作流的简单使用.但提交数据来自于Postman 本次文章我将演示如何从页 ...
- css浮动(float)及如何清除浮动
前言: CSS浮动是现在网页布局中使用最频繁的效果之一,而浮动可以帮我们解决很多问题,那么就让我们一起来看一看如何使用浮动. 一.css浮动(float)(1)html文档流 自窗体自上而下分成一行一 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) A. Altruistic Amphibians (DP)
题目链接:https://codeforc.es/gym/101933/problem/A 题意:有 n 只青蛙在一个坑里面,要求可以跳出坑的青蛙的最大数量.每个青蛙有 3 种属性:l 为青蛙一次可以 ...
- C# HttpClient使用 网络(一)
一.异步调用web服务 GetAsync() static void Main(string[] args) { Console.WriteLine("In main before cal ...
- Luogu P4781【模板】拉格朗日插值
洛谷传送门 板题-注意一下求多个数的乘积的逆元不要一个个快速幂求逆元,那样很慢,时间复杂度就是O(n2log)O(n^2log)O(n2log).直接先乘起来最后求一次逆元就行了.时间复杂度为O(nl ...
- Elasticsearch7.1中文文档-第一章-入门
安装openjdk wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-secur ...