转载:http://www.cnblogs.com/jun-ma/p/4850591.html

Guava Suppliers的主要功能是创建包裹的单例对象,通过get方法可以获取对象的值。每次获取的对象都为同一个对象,但你和单例模式有所区别,Suppliers具备更加迷人的色彩。

Lazy初始化,Supplier wrapped的对象只在第一次get时候会被初始化

public void should_init_the_supplier_wrapped_object_when_get_object() throws Exception {

  Supplier<Integer> memoize = Suppliers.memoize(new Supplier<Integer>() {

    public Integer get() {

      System.out.println("init supplier wrapped object");

      return 1;

     }

    });

  System.out.println("main thread block");

  Thread.sleep(2000);

  System.out.println(memoize.get());

}

Supplier wrapped对象只初始化一次

public void should_init_the_supplier_wrapped_object_for_only_one_time() throws Exception {

   Supplier<Integer> memoize = Suppliers.memoize(new Supplier<Integer>() {

    public Integer get() {

      System.out.println("init supplier wrapped object");

      return 1;

    }

   });

  System.out.println(memoize.get());

  System.out.println(memoize.get());

}

可以使用memoizeWithExpiration函数创建过期设置的Supplier对象,时间过期,get对象会重新初始化对象

public void should_re_init_the_supplier_wrapped_object_when_set_the_expire_time() throws Exception {

  Supplier<Integer> memoize = Suppliers.memoizeWithExpiration(new Supplier<Integer>() {

    public Integer get() { System.out.println("init supplier wrapped object");

    return 1;

     }

  }, 5, TimeUnit.SECONDS);

  System.out.println(memoize.get());

  Thread.sleep(6000);

  System.out.println(memoize.get());

}

总结:

Suppliers的特性可以用来对程序中只需要初始化一次的资源进行管理,比如数据库管理对象,当然,用户也可以根据需求选择是否需要定时更新对象,总而言之,

Suppliers给我们编程带来了更多的选择。

guava学习--Supplier Suppliers的更多相关文章

  1. Guava学习笔记目录

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...

  2. guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁

    guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...

  3. guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...

  4. Guava学习

    Guava学习笔记目录 Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concu ...

  5. [置顶] Guava学习之ArrayListMultimap

    ArrayListMultimap类的继承关系如下图所示: Guava ArrayListMultimap List Multimap 是一个接口,继承自 Multimap 接口.ListMultim ...

  6. [置顶] Guava学习之Splitter

    Splitter:在Guava官方的解释为:Extracts non-overlapping substrings from an input string, typically by recogni ...

  7. [置顶] Guava学习之Iterators

    Iterators类提供了返回Iterator类型的对象或者对Iterator类型对象操作的方法.除了特别的说明,Iterators类中所有的方法都在Iterables类中有相应的基于Iterable ...

  8. [置顶] Guava学习之Lists

    Lists类主要提供了对List类的子类构造以及操作的静态方法.在Lists类中支持构造ArrayList.LinkedList以及newCopyOnWriteArrayList对象的方法.其中提供了 ...

  9. [置顶] Guava学习之Immutable集合

    Immutable中文意思就是不可变.那为什么需要构建一个不可变的对象?原因有以下几点: 在并发程序中,使用Immutable既保证线程安全性,也大大增强了并发时的效率(跟并发锁方式相比).尤其当一个 ...

随机推荐

  1. linux command screen

    喜欢这句话 “我们永远相信,分享是一种美德 | We Believe, Great People Share Knowledge...” 但是这种美德不是为什么都要问的人准备的 ——————————— ...

  2. mongodb备份与恢复

    一.备份:mongodump -d mailaccess -c Mail -q ‘{user:”zhaoxy1@szdep.com”}’ -o /data/dump备份mailaccess datab ...

  3. Angularjs 中文版API v1.3.9 阅读

    http://www.angularjsapi.cn/#/bootstrap 2016.7.4 ng.function: 1.angular.bind(self,fn,args ); 2.angula ...

  4. SpringMVC访问静态资源的三种方式(转)

    本文转自:http://www.iigrowing.cn/springmvc_fang_wen_jing_tai_zi_yuan_de_san_zhong_fang_shi.html 如何你的Disp ...

  5. apache+php+mysql的配置(转载)

    windows: 按http://jingyan.baidu.com/article/fcb5aff797ec41edaa4a71c4.html的安装 按http://www.jb51.net/art ...

  6. maven常见错误

    摘要: 1.Java-maven异常-cannot be cast to javax.servlet.Filter 报错  tomcat 启动后先将tomcat/lib目录下的jar包全部读入内存,如 ...

  7. 如何在Linux中查看所有正在运行的进程

    如何在Linux中查看所有正在运行的进程 FROM:http://os.51cto.com/art/201101/244090.htm 进程是一个其中运行着一个或多个线程的地址空间和这些线程所需要的系 ...

  8. [转]Setup-Subversion-1.6.5+TortoiseSVN-v1.6.5

    转载地址:http://xietingfengsxm.blog.163.com/blog/static/994118512010729111624385/ 开发环境: myEclipse6.5+ecl ...

  9. noi 2718 移动路线

    题目链接: http://noi.openjudge.cn/ch0206/2718/ 右上角的方案数 f(m,n) = f(m-1,n) + f(m,n-1); http://paste.ubuntu ...

  10. php数组插入数据库这个功能该怎么实现

    原文网址:https://segmentfault.com/q/1010000004048449 比如一个数组$a=array("a"=>"1",&quo ...