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既保证线程安全性,也大大增强了并发时的效率(跟并发锁方式相比).尤其当一个 ...
随机推荐
- Git Permission denied (publickey).
有可能, jenkins slave service or jenkins service的logon 账户没有设置好
- 从头开始一步一步实现EF6+Autofac+MVC5+Bootstarp极简前后台ajax表格展示及分页(二)前端修改、添加表格行点击弹出模态框
在前一篇中,由于不懂jquery,前端做的太差了,今天做稍做修改,增加一个跳转到指定页面功能,表格行点击样式变化.并且在表格中加入bootstarp的按钮组,按钮点击后弹出模态框,须修改common, ...
- [6] 智能指针boost::weak_ptr
[1]boost::weak_ptr简介 boost::weak_ptr属于boost库,定义在namespace boost中,包含头文件 #include<boost/weak_ptr.hp ...
- Egret官方案例学习笔记
1.资源记载方式 (1)Egret引擎是2.0.5. (2)resource/resource.json文件是: { "resources": [ { "name&quo ...
- wex5 教程之 图文讲解 Cloudx5一键部署
视频教程地址:http://v.youku.com/v_show/id_XMTc3OTExNTUwNA==.html 效果预览: 一键部署cloudx5三要领 1.数据源命名为x5 2.数据库命名为x ...
- js日期选择控件
// 日期选择 // By Ziyue(http://www.web-v.com/) // 使用方法: // <script type="text/javascript" s ...
- Android网络框架源码分析一---Volley
转载自 http://www.jianshu.com/p/9e17727f31a1?utm_campaign=maleskine&utm_content=note&utm_medium ...
- a标签样式
.a{ cursor:pointer; color: blue; text-decoration:none; }
- Unity3D 材质球设置参数无效果的解决方法
要设置shader里有下划线的属性名,而不是后面字符串的属性名. Color tmp = ,,,alpha); mat.SetColor("_Color", tmp);
- selenium—JS点击方法
package com.allin.pc;import java.util.NoSuchElementException;import org.openqa.selenium.By;import or ...