<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 9, comments - 4, trackbacks - 0, articles - 21
    有一個很耗時的運算需要緩存其計算結果,采用備忘錄模式實現,如:
    Java代碼 
    1. public interface Computable<A, V> {  
    2.     V compute(A arg) throws InterruptedException;  
    3. }  
    4.   
    5. public class ExpensiveFunction  
    6.         implements Computable<String, BigInteger> {  
    7.     public BigInteger compute(String arg) {  
    8.         // 假設這里非常耗時...  
    9.         return new BigInteger(arg);  
    10.     }  
    11. }  


    備忘錄緩存方案一:
    Java代碼 
    1. public class Memoizer1<A, V> implements Computable<A, V> {  
    2.     @GuardedBy("this")  
    3.     private final Map<A, V> cache = new HashMap<A, V>();  
    4.     private final Computable<A, V> c;  
    5.   
    6.     public Memoizer1(Computable<A, V> c) {  
    7.         this.c = c;  
    8.     }  
    9.   
    10.     public synchronized V compute(A arg) throws InterruptedException {  
    11.         V result = cache.get(arg);  
    12.         if (result == null) {  
    13.             result = c.compute(arg);  
    14.             cache.put(arg, result);  
    15.         }  
    16.         return result;  
    17.     }  
    18. }  

    這里備忘錄的整個compute過程被同步,相當于我最原始的低效方案,


    備忘錄緩存方案二:
    Java代碼 
    1. public class Memoizer2<A, V> implements Computable<A, V> {  
    2.     private final Map<A, V> cache = new ConcurrentHashMap<A, V>();  
    3.     private final Computable<A, V> c;  
    4.   
    5.     public Memoizer2(Computable<A, V> c) { this.c = c; }  
    6.   
    7.     public V compute(A arg) throws InterruptedException {  
    8.         V result = cache.get(arg);  
    9.         if (result == null) {  
    10.             result = c.compute(arg);  
    11.             cache.put(arg, result);  
    12.         }  
    13.         return result;  
    14.     }  
    15. }  

    將線程安全性委給cache,注:這個方案的cache用了ConcurrentHashMap,它是線程安全的。

    備忘錄緩存方案三:
    Java代碼 
    1. public class Memoizer3<A, V> implements Computable<A, V> {  
    2.     private final Map<A, Future<V>> cache  
    3.             = new ConcurrentHashMap<A, Future<V>>();  
    4.     private final Computable<A, V> c;  
    5.   
    6.     public Memoizer3(Computable<A, V> c) { this.c = c; }  
    7.   
    8.     public V compute(final A arg) throws InterruptedException {  
    9.         Future<V> f = cache.get(arg);  
    10.         if (f == null) {  
    11.             Callable<V> eval = new Callable<V>() {  
    12.                 public V call() throws InterruptedException {  
    13.                     return c.compute(arg);  
    14.                 }  
    15.             };  
    16.             FutureTask<V> ft = new FutureTask<V>(eval);  
    17.             f = ft;  
    18.             cache.put(arg, ft);  
    19.             ft.run(); // call to c.compute happens here  
    20.         }  
    21.         try {  
    22.             return f.get();  
    23.         } catch (ExecutionException e) {  
    24.             throw launderThrowable(e.getCause());  
    25.         }  
    26.     }  
    27. }  

    使用Future(相當上一帖的Entry)封裝,因為這里資源獲取過程不固定(通用方案),所以使用了Callable進行回調資源獲取過程(求值),
    這個方案的 if (f == null) 不安全,特殊性可能會進行多次運算,下面的方案四解決這個問題。

    備忘錄緩存方案四(最終方案):
    Java代碼 
    1. public class Memoizer<A, V> implements Computable<A, V> {  
    2.     private final ConcurrentMap<A, Future<V>> cache  
    3.         = new ConcurrentHashMap<A, Future<V>>();  
    4.     private final Computable<A, V> c;  
    5.   
    6.     public Memoizer(Computable<A, V> c) { this.c = c; }  
    7.   
    8.     public V compute(final A arg) throws InterruptedException {  
    9.         while (true) {  
    10.             Future<V> f = cache.get(arg);  
    11.             if (f == null) {  
    12.                 Callable<V> eval = new Callable<V>() {  
    13.                     public V call() throws InterruptedException {  
    14.                         return c.compute(arg);  
    15.                     }  
    16.                 };  
    17.                 FutureTask<V> ft = new FutureTask<V>(eval);  
    18.                 f = cache.putIfAbsent(arg, ft);  
    19.                 if (f == null) { f = ft; ft.run(); }  
    20.             }  
    21.             try {  
    22.                 return f.get();  
    23.             } catch (CancellationException e) {  
    24.                 cache.remove(arg, f);  
    25.             } catch (ExecutionException e) {  
    26.                 throw launderThrowable(e.getCause());  
    27.             }  
    28.         }  
    29.     }  

    主站蜘蛛池模板: 大学生美女毛片免费视频| 久艹视频在线免费观看| 久久久久久久久免费看无码| 国产AV无码专区亚洲AVJULIA| WWW国产成人免费观看视频| 国产成人99久久亚洲综合精品 | 亚洲AV无码不卡在线播放| 中文字幕免费在线看线人动作大片 | 亚洲国产精品无码AAA片| 久久免费香蕉视频| 亚洲乱码国产乱码精品精| 精品成人免费自拍视频| 久久亚洲AV无码精品色午夜麻| 免费无码作爱视频| 亚洲男人天堂av| 亚洲视频在线免费看| 亚洲综合成人婷婷五月网址| 国产va精品免费观看| 亚洲色成人四虎在线观看| 暖暖在线日本免费中文| 免费福利在线观看| 国产亚洲美女精品久久久2020| 中文字幕版免费电影网站| 亚洲国产成人私人影院| 91精品免费在线观看| 亚洲人成网站999久久久综合| 国产又大又黑又粗免费视频| 性生大片视频免费观看一级| 黑人精品videos亚洲人| 在线永久免费的视频草莓| 亚洲一本到无码av中文字幕| 四虎永久免费观看| 亚洲免费观看视频| 97久久国产亚洲精品超碰热| 国产18禁黄网站免费观看| 野花香在线视频免费观看大全| 亚洲午夜精品在线| 亚洲精品国产精品国自产观看| 免费一区二区三区| 亚洲区日韩精品中文字幕| 日日噜噜噜噜夜夜爽亚洲精品|