`
san_yun
  • 浏览: 2597547 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Google Collections(Guava)中强大的Concurrent MapMaker

 
阅读更多

JDK 1.5引入的ConcurrentHashMap由于其精巧的设计,更高的并发性能,捕获了大家的心,在并发场景中出场率极高,但随着深入的使用,很快的 就发现了其中的不足。例如在以Map作为Cache的典型场景中,我们都需要有元素过期的处理,WeakHashMap是这方面的高手,但其在并发方面有 点菜(非线程安全),当我们想让这两位大将同时上场的时候,就只能抓耳搔腮了。

 

Google Collections中的MapMaker融合了Weak Reference,线程安全,高并发性能,异步超时清理,自定义构建元素等强大功能于一身,除了可以设置超时功能外,还可以绑定key的未取到值的function,当通过get(key)取缓存为空的时候,可以通过这个绑定的function来将返回值put到缓存中,留着下一次get。

       听完这些介绍,心动不已,找个时间,简单的test了下MapMaker,果然感觉很强大。

 

       GoogleCollection的下载地址:http://code.google.com/p/google-collections/       

 

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Function;
import com.google.common.collect.MapMaker;

/**
 * @author guoliang created GoogleColTestMain.java
 * @since 2010-4-28 下午05:48:55
 */
public class GoogleColTestMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
    	/**
    	 * softKeys
    	 * weakValues
    	 * 可以设置key跟value的strong,soft,weak属性。不错不错。
    	 * expiration(3, TimeUnit.SECONDS)设置超时时间为3秒
    	 * 
    	 */
        ConcurrentMap<String, String> testMap = new MapMaker().concurrencyLevel(32).softKeys().weakValues().expiration(
                3, TimeUnit.SECONDS).makeComputingMap(new Function<String, String>() {
            /**
             * 这里就是绑定的根据key没找到value的时候触发的function,
             * 可以将这里的返回值放到对应的key的value中!
             * @param arg0
             * @return
             */
            @Override
            public String apply(String arg0) {
                return "create:" + arg0;
            }

        });

        testMap.put("a", "testa");
        testMap.put("b", "testb");

        System.out.println(testMap.get("a"));
        System.out.println(testMap.get("b"));
        System.out.println(testMap.get("c"));

        /**
         * 这里sleep4秒钟过后,
         * 缓存都失效,再get就会根据绑定的function去获得value放在map中了。
         */
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        /**
         * 看看这里的再输出,是不是就是新的值了!~
         */

        System.out.println(testMap.get("a"));
        System.out.println(testMap.get("b"));
        System.out.println(testMap.get("c"));
    }

}

 

 

参考:http://norther.iteye.com/blog/670414

http://guoliangqi.iteye.com/blog/657534

分享到:
评论

相关推荐

    Google-Guava-Collections-使用介绍

    Google_Guava_Collections_使用介绍.pdf )

    Google_Guava_Collections_使用介绍.pdf )

    guava-collections-r03.jar

    guava类似Apache Commons工具集包含了若干被Google的 Java项目广泛依赖 的核心库

    Google的Guava工具包

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...

    Guava官方教程-中文

    Google Guava是一个比较有趣的框架,它提供了很多有趣的的功能, google Guava 给开发者提供了如下常用功能: 集合(collections) 缓存(caching) 原生的类型支持(primitives support) 并发类库(concurrency ...

    Guava及扩展组件资源大全:含最新及历史各版本Jar、源代码

    扩展组件包括guava-annotations、guava-base、guava-bootstrap、guava-collections、guava-concurrent、guava-gwt、guava-io、guava-parent、guava-primitives、listenablefuture、guava-testlib等。

    Google中的Guava源码

    Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, ...

    com.google.common guava 18.0 JAR包

    Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] ...

    google Guava集合工具类(超实用)

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...

    google-collections-1.0-rc2.jar

    google-collections-1.0-rc2.jar 的jar包,放心使用。

    google-guava

    Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] ...

    Getting Started with Google Guava

    Write more robust code that is easier to read and maintain, Learn how to use Preconditions to prevent and find errors faster, Shows how Guava Collections can make working with Java Collections a ...

    Google开源Guava包让使用Java语言变得更舒适

    Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] ...

    Guava 16.0 API (CHM格式)

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...

    Google Collections 源代码和API文档

    Google Collections是Google公司开发的常用工具库,包括对字符串,文件操作,数据结构和并发程序开发的支持。它比Apache Commons Collections提供了更多更强大的函数,使得程序编写更加简洁,不易产生错误。 这个...

    google-collections-1.0.rar

    google公共工具类;google collections是google的工程师利用传说中的“20%时间”开发的集合库,它是对java.util的扩展,提供了很多实用的类来简化代码。google collections使用了范型,所以要求jdk1.5以上。

    Android代码-guava

    Guava: Google Core Libraries for Java Guava is a set of core libraries that includes new collection types (such as multimap and multiset), immutable collections, a graph library, functional types, ...

    google-collections

    oogle collections是google的工程师利用传说中的“20%时间”开发的集合库,它是对java.util的扩展,提供了很多实用的类来简化代码。google collections使用了范型,所以要求jdk1.5以上。它的作者没有像apache ...

    commons-collections4-4.1-API文档-中文版.zip

    赠送jar包:commons-collections4-4.1.jar; 赠送原API文档:commons-collections4-4.1-javadoc.jar; 赠送源代码:commons-collections4-4.1-sources.jar; 赠送Maven依赖信息文件:commons-collections4-4.1.pom;...

Global site tag (gtag.js) - Google Analytics