guava学习--Supplier Suppliers
转载: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的更多相关文章
- Guava学习笔记目录
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...
- guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁
guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...
- Guava学习
Guava学习笔记目录 Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concu ...
- [置顶] Guava学习之ArrayListMultimap
ArrayListMultimap类的继承关系如下图所示: Guava ArrayListMultimap List Multimap 是一个接口,继承自 Multimap 接口.ListMultim ...
- [置顶] Guava学习之Splitter
Splitter:在Guava官方的解释为:Extracts non-overlapping substrings from an input string, typically by recogni ...
- [置顶] Guava学习之Iterators
Iterators类提供了返回Iterator类型的对象或者对Iterator类型对象操作的方法.除了特别的说明,Iterators类中所有的方法都在Iterables类中有相应的基于Iterable ...
- [置顶] Guava学习之Lists
Lists类主要提供了对List类的子类构造以及操作的静态方法.在Lists类中支持构造ArrayList.LinkedList以及newCopyOnWriteArrayList对象的方法.其中提供了 ...
- [置顶] Guava学习之Immutable集合
Immutable中文意思就是不可变.那为什么需要构建一个不可变的对象?原因有以下几点: 在并发程序中,使用Immutable既保证线程安全性,也大大增强了并发时的效率(跟并发锁方式相比).尤其当一个 ...
随机推荐
- Groovy学习笔记(二)
在上一篇文章中我们主要学习了如何搭建Groovy开发环境,为我们的Groovy之旅做好了准备工作,不知道你是否准备好了?接下来我们就一起看看Groovy与我们熟悉的Java有什么异同. Groovy是 ...
- POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)
Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...
- Android系统下,用adb实现自动获取应用性能数据
[自动化测试模式] 支持以adb shell命令的形式启动和运行.需要注意的是,office系列软件可能会更改命令中的字符,导致命令不可用!请手工输入命令,或从附带的command.txt文本中复制. ...
- Ajax提交整个表单
//view页面 <script> $(function () { $("#btnAdd").click(function () { var pars = $(&quo ...
- C++嵌套多个命名空间举例
首先在结构上是能经得起推敲的,举个例子: test.h #pragma region 嵌套多个命名空间举例 namespace Group { namespace C ...
- 移动前端中viewport(视口) 转
移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dpi ( device-independent pixel 设备逻辑像素 )和 CSS 像素之间 ...
- spring环境搭建需要的插件-------Spring Tool Suite™ Downloads
下载地址http://spring.io/tools/sts/all 上面的是集成了eclipse的,所以文件比较大,下面的是单独的插件,下载之后打开eclipse,help->installN ...
- 新知识:Java 利用itext填写pdf模板并导出(昨天奋战到深夜四点,知道今天两点终于弄懂)
废话少说,不懂itext干啥用的直接去百度吧. ***************制作模板******************* 1.先用word做出界面 2.再转换成pdf格式 3.用Adobe Acr ...
- gfortran、g77等编译器中使用多个文件
gfortran aaaa.f90 bbbb.f90 -o cccc (生成cccc可执行文件,cccc名称可自由设定) 又可以分成两步,因为gfortran先把程序文件编译成*.o文件,再把*.o文 ...
- c#下载网页源码的两种方法
1.WebClient: System.Net.WebClient wc = new System.Net.WebClient(); Byte[] pageData = wc.DownloadData ...