zoukankan      html  css  js  c++  java
  • 一个高速的结果缓存

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

    实现一个计算接口

    package com._resultCache;
    
    import java.math.BigInteger;
    /**
     *  一个很花费时间的计算
     * */
    public class ExpensiveFunction implements Computable<String,BigInteger> {
        @Override
        public BigInteger compute(String arg) throws InterruptedException {
            System.out.println("正在计算" + arg);
            Thread.sleep(2000);
            return new BigInteger(arg);
        }
    
    
    }

    一个缓存类

    1.  先检查是否存在结果缓存

    2.  如果没有就准备执行计算

    3.  将准备计算的任务加入到缓存,其他相同任务到来时候就会被阻塞,等待第一个任务的结束而不需要重新计算

    4.  新的任务开始计算

    5.  获取结果返回

    package com._resultCache;
    
    import java.util.Map;
    import java.util.concurrent.*;
    
    public class Memoizer<A,V> {
    
        //存储一个任务的缓存
        private final Map<A,Future<V>> cache = new ConcurrentHashMap<A,Future<V>>();
    
        private final Computable<A,V> c;
    
        public Memoizer(Computable<A, V> c) {
            this.c = c;
        }
    
        public V compute(final A arg){
            while(true){
                Future f = cache.get(arg);
                if(f == null){
                    Callable<V> eval = new Callable<V>() {
                        @Override
                        public V call() throws Exception {
                            return c.compute(arg);
                        }
                    };
                    FutureTask<V> ft = new FutureTask<V>(eval);
                    f = cache.putIfAbsent(arg,ft);
                    if(f == null){
                        f = ft;
                        ft.run();
                    }
                }
                try {
                    return (V) f.get();
                } catch (CancellationException e) {
                    cache.remove(arg,f);
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    代码的演示Demo

    package com._resultCache;
    
    import java.math.BigInteger;
    
    public class Demo {
        public static void main(String[] args) {
            ExpensiveFunction expensiveFunction = new ExpensiveFunction();
            Memoizer<String,BigInteger> memoizer = new Memoizer<String,BigInteger>(expensiveFunction);
    
            System.out.println("计算3234开始...");
            System.out.println("3234结果" + memoizer.compute("3234"));
            System.out.println("计算4456开始...");
            System.out.println("4456结果" + memoizer.compute("4456"));
    
            System.out.println("计算123456开始...");
    
            for(int i = 0 ;i  < 10 ;i ++){
                System.out.println("123456结果 " + memoizer.compute("123456"));
            }
            System.out.println("计算4456开始...");
            System.out.println("4456结果" + memoizer.compute("4456"));
    
        }
    
    }

    运行的结果

    计算3234开始...
    正在计算3234
    3234结果3234
    计算4456开始...
    正在计算4456
    4456结果4456
    计算123456开始...
    正在计算123456
    123456结果 123456
    123456结果 123456
    123456结果 123456
    123456结果 123456
    123456结果 123456
    123456结果 123456
    123456结果 123456
    123456结果 123456
    123456结果 123456
    123456结果 123456
    计算4456开始...
    4456结果4456
  • 相关阅读:
    ionic4 无限滚动加载组件 ion-infinite-scroll-content 的loadingSpinner 属性
    ionic cordova build android 报错 解决
    ionic4 标题栏 ion-toolbar 默认 底部会显示一个底边框 解决
    ionic3 在ios12.2 12.3 12.4上页面无法滚动
    Android 打生产包(release)生成密钥(证书)及签名
    在Vue.js应用程序中使用Ionic 4组件
    ionic4 ionic-native列表
    清除 多行li 行间距
    Connection Pool
    Logging
  • 原文地址:https://www.cnblogs.com/da-peng/p/9810711.html
Copyright © 2011-2022 走看看