<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.     }  

    主站蜘蛛池模板: 羞羞视频在线观看免费| 亚洲欧洲日韩极速播放| 精品人妻系列无码人妻免费视频| 日韩成人免费视频播放| 亚洲色成人四虎在线观看| 午夜成年女人毛片免费观看| 亚洲一区二区三区在线观看网站| 久久WWW色情成人免费观看| 亚洲久热无码av中文字幕| 国内大片在线免费看| 羞羞视频免费观看| 久久久久一级精品亚洲国产成人综合AV区 | 国产成人在线免费观看| 国产亚洲精品2021自在线| 亚洲阿v天堂在线2017免费| 一边摸一边桶一边脱免费视频| 亚洲国产精品无码久久久久久曰| 99re8这里有精品热视频免费| 亚洲av永久无码精品国产精品 | A毛片毛片看免费| 亚洲今日精彩视频| 色婷婷7777免费视频在线观看| 亚洲综合激情五月色一区| 免费看国产一级片| 免费一区二区无码东京热| 亚洲无成人网77777| 免费观看午夜在线欧差毛片 | 久久99精品免费一区二区| 亚洲AV成人片色在线观看| 黄页网站在线观看免费高清| 亚洲AV日韩AV永久无码色欲| 久久亚洲AV永久无码精品| 精品无码无人网站免费视频| 亚洲精品无码中文久久字幕| 黑人大战亚洲人精品一区| 1000部禁片黄的免费看| 亚洲大码熟女在线观看| 久久精品国产亚洲沈樵| 免费看无码自慰一区二区| a级毛片免费观看视频| 亚洲人成色4444在线观看|