序
本文主要介紹如何利用Guava的Suppliers.memoize實(shí)現(xiàn)單例。
實(shí)例/** * 利用Suppliers.memoize實(shí)現(xiàn)單例 * Created by xixicat on 15/12/25. */ public class SuppilerSingletonTest { class HeavyObject{ public HeavyObject() { System.out.println("being created"); } } class ObjectSuppiler implements SupplierSuppliers.memoize方法{ @Override public HeavyObject get() { return new HeavyObject(); } } /** * 每次都new一次 */ @Test public void testNotSingleton(){ Supplier notCached = new ObjectSuppiler(); for(int i=0;i<5;i++){ notCached.get(); } } /** * 單例 */ @Test public void testSingleton(){ Supplier notCached = new ObjectSuppiler(); Supplier cachedSupplier = Suppliers.memoize(notCached); for(int i=0;i<5;i++){ cachedSupplier.get(); } } }
/** * Returns a supplier which caches the instance retrieved during the first * call to {@code get()} and returns that value on subsequent calls to * {@code get()}. See: * memoization * *參考The returned supplier is thread-safe. The supplier"s serialized form * does not contain the cached value, which will be recalculated when {@code * get()} is called on the reserialized instance. * *
If {@code delegate} is an instance created by an earlier call to {@code * memoize}, it is returned directly. */ public static
Supplier memoize(Supplier delegate) { return (delegate instanceof MemoizingSupplier) ? delegate : new MemoizingSupplier (Preconditions.checkNotNull(delegate)); } @VisibleForTesting static class MemoizingSupplier implements Supplier , Serializable { final Supplier delegate; transient volatile boolean initialized; // "value" does not need to be volatile; visibility piggy-backs // on volatile read of "initialized". transient T value; MemoizingSupplier(Supplier delegate) { this.delegate = delegate; } @Override public T get() { // A 2-field variant of Double Checked Locking. if (!initialized) { synchronized (this) { if (!initialized) { T t = delegate.get(); value = t; initialized = true; return t; } } } return value; } @Override public String toString() { return "Suppliers.memoize(" + delegate + ")"; } private static final long serialVersionUID = 0; }
Guava Explained
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/65566.html
摘要:子類通過實(shí)現(xiàn)方法或重寫其他父類的方法,從而提供了各種不同的具體操作,如判斷是否為某一個(gè)字符,判斷是否為數(shù)字字符,判斷是否為字符等。 showImg(https://segmentfault.com/img/bVbe1IW?w=300&h=300); 本文源地址:http://www.fullstackyang.com/...,轉(zhuǎn)發(fā)請注明該地址或segmentfault地址,謝謝! 最近...
摘要:創(chuàng)建一個(gè)指定引用的,若引用為則表示引用缺失,并返回一個(gè)。返回包含的實(shí)例,該實(shí)例必須存在,如果不存在將會(huì)拋出異常。說明有三個(gè)方法沒有作解釋,主要是擔(dān)心相關(guān)知識(shí)不理解,容易做出錯(cuò)誤的翻譯,望請諒解 原文地址譯者 Guava Optional Class Optional 是一個(gè)不可變對象,用來包含一個(gè)非null對象。Optional使用absent來表達(dá)null值。該類提供了很多實(shí)用的方法...
摘要:緩存本次主要討論緩存。清除數(shù)據(jù)時(shí)的回調(diào)通知。具體不在本次的討論范圍。應(yīng)該是以下原因新起線程需要資源消耗。維護(hù)過期數(shù)據(jù)還要獲取額外的鎖,增加了消耗。 showImg(https://segmentfault.com/img/remote/1460000015272232); 前言 Google 出的 Guava 是 Java 核心增強(qiáng)的庫,應(yīng)用非常廣泛。 我平時(shí)用的也挺頻繁,這次就借助日...
摘要:緩存本次主要討論緩存。清除數(shù)據(jù)時(shí)的回調(diào)通知。具體不在本次的討論范圍。應(yīng)該是以下原因新起線程需要資源消耗。維護(hù)過期數(shù)據(jù)還要獲取額外的鎖,增加了消耗。 showImg(https://segmentfault.com/img/remote/1460000015272232); 前言 Google 出的 Guava 是 Java 核心增強(qiáng)的庫,應(yīng)用非常廣泛。 我平時(shí)用的也挺頻繁,這次就借助日...
閱讀 1225·2021-11-11 16:54
閱讀 878·2021-10-19 11:44
閱讀 1337·2021-09-22 15:18
閱讀 2445·2019-08-29 16:26
閱讀 2946·2019-08-29 13:57
閱讀 3095·2019-08-26 13:32
閱讀 1081·2019-08-26 11:58
閱讀 2328·2019-08-26 10:37