AtomicLongMap的使用

AtomicLongMap是Google Guava项目的一个类,它是线程安全、支持并发访问的。

 

Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等

 

public class GuavaTest {
    //来自于Google的Guava项目
    AtomicLongMap<String> map = AtomicLongMap.create(); //线程安全,支持并发
    
    Map<String, Integer> map2 = new HashMap<String, Integer>(); //线程不安全
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); //为map2增加并发锁
    
    ConcurrentHashMap<String, Integer> map3 = new ConcurrentHashMap<String, Integer>(); //线程安全,但也要注意使用方式
    
    private int taskCount = 100;
    CountDownLatch latch = new CountDownLatch(taskCount); //新建倒计时计数器,设置state为taskCount变量值
    
	public static void main(String[] args) {
	    GuavaTest t = new GuavaTest();
        t.test();
	}
	
	private void test(){
	    //启动线程
	    for(int i=1; i<=taskCount; i++){
	        Thread t = new Thread(new MyTask("key", 100));
	        t.start();
	    }
	    
	    try {
	        //等待直到state值为0,再继续往下执行
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
	    
	    System.out.println("##### AtomicLongMap #####");
	    for(String key : map.asMap().keySet()){
	        System.out.println(key + ": " + map.get(key));
	    }
	    
        System.out.println("##### HashMap #####");
        for(String key : map2.keySet()){
            System.out.println(key + ": " + map2.get(key));
        }
        
        System.out.println("##### ConcurrentHashMap #####");
        for(String key : map3.keySet()){
            System.out.println(key + ": " + map3.get(key));
        }
	}
	
	class MyTask implements Runnable{
	    private String key;
        private int count = 0;
        
        public MyTask(String key, int count){
            this.key = key;
            this.count = count;
        }
        
        @Override
        public void run() {
            try {
                for(int i=0; i<count; i++){
                    map.incrementAndGet(key); //key值自增1后,返回该key的值
                    
                    //对map2添加写锁,可以解决线程并发问题
                    lock.writeLock().lock();
                    try{
                        if(map2.containsKey(key)){
                            map2.put(key, map2.get(key)+1);
                        }else{
                            map2.put(key, 1);
                        }
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }finally{
                        lock.writeLock().unlock();
                    }
                    
                    //虽然ConcurrentHashMap是线程安全的,但是以下语句块不是整体同步,导致ConcurrentHashMap的使用存在并发问题
                    if(map3.containsKey(key)){
                        map3.put(key, map3.get(key)+1);
                    }else{
                        map3.put(key, 1);
                    }
                    
                    //TimeUnit.MILLISECONDS.sleep(50); //线程休眠50毫秒
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                latch.countDown(); //state值减1
            }
        }
    }
	
}

 

执行结果如下:

##### AtomicLongMap #####

key: 10000

##### HashMap #####

key: 10000

##### ConcurrentHashMap #####

key: 9311

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值