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

Callable和Supplier的区别的更多相关文章

  1. 在线程池使用Callable和Runnable的区别以及如何关闭线程

    一.区别总结: Callable定义的方法是call,而Runnable定义的方法是run. Callable的call方法可以有返回值,而Runnable的run方法不能有返回值,这是核心区别. C ...

  2. Callable 和 Runnable 的区别

    Callable 和 Runnable 的使用方法大同小异, 区别在于: 1.Callable 使用 call() 方法, Runnable 使用 run() 方法 2.call() 可以返回值, 而 ...

  3. callable和runnable的区别

    Runnable接口源码 @FunctionalInterface public interface Runnable { /** * When an object implementing inte ...

  4. 浅谈线程runnable和callable的使用及区别

    线程使用比较广泛,但实际上一般项目很少用上线程,线程常用于优化复杂的程序执行流程,把一些与业务关系关系不大但是必须要执行的流程使用线程的方式让子线程去执行,主流程只返回跟业务有关的信息 runnabl ...

  5. Callable和Future的区别

    Callable 在Java中,创建线程一般有两种方式,一种是继承Thread类,一种是实现Runnable接口.然而,这两种方式的缺点是在线程任务执行结束后,无法获取执行结果.我们一般只能采用共享变 ...

  6. java 8 supplier object区别

    起初用supplier的时候,发现用法和object一样呀,只是一个用于生产object工厂方法而已,为什么要新引起这样一个对象呢. 后面查到这篇英语文档,才发现,相比object有如下三个用法: ( ...

  7. Callable与Future、FutureTask的学习 & ExecutorServer 与 CompletionService 学习 & Java异常处理-重要

    Callable是Java里面与Runnable经常放在一起说的接口. Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务 ...

  8. Callable与Future的简单介绍

    Callable与Future的介绍 Callable与 Future 两功能是Java在后续版本中为了适应多并法才加入的,Callable是类似于Runnable的接口,实现Callable接口的类 ...

  9. Runnable、Callable、Future和FutureTask用法

    http://www.cnblogs.com/dolphin0520/p/3949310.html java 1.5以前创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable ...

随机推荐

  1. redis——redis的一些核心把握

    redis单线程,为什么比较快 单线程指的是网络请求模块使用了一个线程(所以不需考虑并发安全性),即一个线程处理所有网络请求,其他模块仍用了多个线程.redis能够快速执行的原因有三点: (1) 绝大 ...

  2. js获取div基础元素

    1.js获取div元素 clientHeight 获取对象的高度,不计算任何边距.边框.滚动条,但包括该对象的补白. clientLeft 获取 offsetLeft 属性和客户区域的实际左边之间的距 ...

  3. bind9+dlz+mysql连接断开问题

    前言 关于bind-dlz介绍:http://bind-dlz.sourceforge.net/ DLZ(Dynamically Loadable Zones)与传统的BIND9不同,BIND的不足之 ...

  4. systemctl automatically restart service

    systemctl automatically restart service  Pothi Kalimuthu  Linux  October 9, 2016 0 Minutes Reference ...

  5. Linux之ln文件创建链接

    ln命令用来为文件创建链接,链接类型分为硬链接和软链接(符号链接)两种 1)软连接和Windows系统中的快捷方式有点类似 2)硬链接,相当于多了一个文件名指向同一块内存空间,目录无法创建硬链接,不可 ...

  6. 域知识深入学习一:Active Directory 域服务

      AD DS用来组织,管理,控制网络资源 1.1 Active Directory 域服务概述 AD内的directorydatabase(目录数据库)用来存储用户账户,计算机账户,打印机与共享文件 ...

  7. tensorflow.python.framework.errors_impl.PermissionDeniedError: /data; Permission denied

    在linux系统中,tensorflow跑mnist数据集出现错误,本应该自动下载的数据集 将mnist自动下载的路径,由/data/mnist之前的/删掉即可.改为data/mnist.

  8. springboot2.0入门(三)----定义编程风格+jackjson使用+postMan测试

    一.RESTFul风格API 1.优点: )看Url就知道要什么资源 )看http method就知道针对资源干什么 )看http status code就知道结果如何 HTTP方法体现对资源的操作: ...

  9. MULTIPOLYGON、POLYGON 封装为echart geoJson 数据封装

    一.环境.问题简述: 1.采用的事前后端分离,后端需要封装将点位获取的点位信息封装为geoJson数据类型,供前端利用echart 绘制地图: 2.思路:简单理解geoJson,将对应坐标点位封装为g ...

  10. JS !function 稀奇古怪的写法

    !function(){alert("Execute after ()fun!")}(alert("Execute Firstly!")) 注意上面的执行顺序