java并发编程实战程序5-19

验证Java并发编程实战程序清单5-19中memorizer.compute对于相同参数的请求只有一个能进Map


import org.junit.Test;

import java.util.concurrent.*;

public class LearnFutureTask {
    @Test
    public void test() throws InterruptedException {
        Memorizer<String, Integer> memorizer = new Memorizer<>(new Util());
        for (int i = 0; i < 100; i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Integer len = memorizer.compute("abcd");
//                        System.out.println(len);
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        Thread.sleep(10000);
    }
}

interface Computable<A,V>{
    V compute(A arg) throws ExecutionException;
}

class Util implements Computable<String,Integer>{
    @Override
    public Integer compute(String arg) throws ExecutionException {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return arg.length();
    }
}
class MyHaspMap<K,V> extends ConcurrentHashMap<K,V>{
    @Override
    public V putIfAbsent(K key, V value) {
        V v = super.putIfAbsent(key, value);
        System.out.println("entered");//记录调用putIfAbsent次数
        return v;
    }
}
class Memorizer<A,V> implements Computable<A,V>{
    private final ConcurrentHashMap<A, Future<V>>cache = new MyHaspMap<A,Future<V>>();
    private Computable<A,V> util;
    public Memorizer(Computable<A,V>util){
        this.util = util;
    }
    @Override
    public V compute(A arg) throws ExecutionException {
        while (true){
            Future<V> vFuture = cache.get(arg);
            if (vFuture == null){
                Callable<V> callable = new Callable<V>() {
                    @Override
                    public V call() throws Exception {
                        return util.compute(arg);
                    }
                };
                FutureTask<V> vFutureTask = new FutureTask<>(callable);
                vFuture = cache.putIfAbsent(arg, vFutureTask);
                if (vFuture == null){
                    vFuture = vFutureTask;
                    vFutureTask.run();
                }else {
                    System.out.println("put failed");//记录put失败次数
                    //调用次数减去失败次数即真正执行putIfAbsent的次数
                }
            }
            try {
                return vFuture.get();
            } catch (InterruptedException e) {
                cache.remove(arg);
            } catch (ExecutionException e) {
                throw e;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值