栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

高效可伸缩的结果缓存

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

高效可伸缩的结果缓存

package com.yw.cache;

import javax.annotation.concurrent.GuardedBy;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;


public class CacheMain implements Computable {
    
    @Override
    public BigInteger compute(String arg) throws InterruptedException {
        // 在进过长时间计算后
        return new BigInteger(arg);
    }
}

interface Computable {
    V compute(A arg) throws InterruptedException;
}

class MemoizerOne implements Computable {
    @GuardedBy("this")
    private final Map cache = new HashMap<>();
    private final Computable c;
    private final ReentrantLock lock = new ReentrantLock();

    MemoizerOne(Computable computable) {
        this.c = computable;
    }

    @Override
    public V compute(A arg) throws InterruptedException {
        final ReentrantLock l = this.lock;
        l.lock();
        try {
            V result = cache.get(arg);
            if (result == null) {
                result = c.compute(arg);
                cache.put(arg, result);
            }
            return result;
        } finally {
            l.unlock();
        }
    }
}

class MemoizerTwo implements Computable {
    @GuardedBy("this")
    private final Map cache = new ConcurrentHashMap<>();
    private final Computable c;

    MemoizerTwo(Computable computable) {
        this.c = computable;
    }

    @Override
    public V compute(A arg) throws InterruptedException {
        V result = cache.get(arg);
        if (result == null) {
            result = c.compute(arg);
            cache.put(arg, result);
        }
        return result;
    }
}

class MemoizerThree implements Computable {
    @GuardedBy("this")
    private final Map> cache = new ConcurrentHashMap<>();
    private final Computable c;

    MemoizerThree(Computable computable) {
        this.c = computable;
    }

    
    @Override
    public V compute(A arg) throws InterruptedException {
        Future result = cache.get(arg);
        if (result == null) {
            Callable callable = new Callable() {
                @Override
                public V call() throws Exception {
                    return c.compute(arg);
                }
            };
            FutureTask task = new FutureTask(callable);
            result = task;
            cache.put(arg, result);
            task.run(); // 在这里调用c.compute(arg)
        }
        try {
            return result.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
}

class MemoizerFour implements Computable {
    @GuardedBy("this")
    private final Map> cache = new ConcurrentHashMap<>();
    private final Computable c;

    MemoizerFour(Computable computable) {
        this.c = computable;
    }

    
    @Override
    public V compute(A arg) throws InterruptedException {
        for (; ; ) { // 为什么不用while(true)因为for会被虚拟机直接优化为死循环,而while(true)还需要运行判断
            Future result = cache.get(arg);
            if (result == null) {
                Callable callable = new Callable() {
                    @Override
                    public V call() throws Exception {
                        return c.compute(arg);
                    }
                };
                FutureTask task = new FutureTask(callable);
                result = cache.putIfAbsent(arg, result);
                if (result == null) {
                    result = task;
                    task.run();// 在这里调用c.compute(arg)}
                }
                try {
                    return result.get();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769336.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号