⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memoizer1.java

📁 java concurrency in practice 源码. JAVA并发设计
💻 JAVA
字号:
package net.jcip.examples;import java.math.BigInteger;import java.util.*;import net.jcip.annotations.*;/** * Memoizer1 * * Initial cache attempt using HashMap and synchronization * * @author Brian Goetz and Tim Peierls */public class Memoizer1 <A, V> implements Computable<A, V> {    @GuardedBy("this") private final Map<A, V> cache = new HashMap<A, V>();    private final Computable<A, V> c;    public Memoizer1(Computable<A, V> c) {        this.c = c;    }    public synchronized V compute(A arg) throws InterruptedException {        V result = cache.get(arg);        if (result == null) {            result = c.compute(arg);            cache.put(arg, result);        }        return result;    }}interface Computable <A, V> {    V compute(A arg) throws InterruptedException;}class ExpensiveFunction        implements Computable<String, BigInteger> {    public BigInteger compute(String arg) {        // after deep thought...        return new BigInteger(arg);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -