zoukankan      html  css  js  c++  java
  • Guava的Supplier实现单例

    1、函数式编程;

    2、第一次get时才会初始化;

    3、可以实现单例或缓存。

    package suppliers;
    
    import com.google.common.base.Supplier;
    import com.google.common.base.Suppliers;
    import com.google.common.collect.Iterables;
    
    import java.util.ServiceLoader;
    
    /**
     * @author xfyou
     * @date 2018/7/30
     */
    public class Test {
    
        public static void main(String[] args) {
            try {
                //shouldInitTheSupplierWrappedObjectWhenGetObject();
                shouldInitTheSupplierWrappedObjectForOnlyOneTime();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static void shouldInitTheSupplierWrappedObjectWhenGetObject() throws Exception {
            Supplier<Integer> memoize = Suppliers.memoize(new Supplier<Integer>() {
                @Override
                public Integer get() {
                    System.out.println("init supplier wrapped object");
                    return 1;
                }
            });
            System.out.println("main thread block");
            Thread.sleep(2000);
            System.out.println(memoize.get());
            System.out.println(memoize.get());
        }
    
        private static void shouldInitTheSupplierWrappedObjectForOnlyOneTime() {
            Supplier<Admin> memoize = Suppliers.memoize(new Supplier<Admin>() {
                @Override
                public Admin get() {
                    System.out.println("init supplier wrapped object");
                    ServiceLoader<Admin> loader = ServiceLoader.load(Admin.class);
                    return Iterables.getFirst(loader, null);
                }
            });
            System.out.println(memoize.get());
            System.out.println(memoize.get());
            System.out.println(memoize.get());
        }
    }
    package suppliers;
    
    /**
     * @author xfyou
     * @date 2018/7/31
     */
    public interface User {
        /**
         * @param firstName
         * @param lastName
         */
        void addUser(String firstName, String lastName);
    }
    package suppliers;
    
    import lombok.ToString;
    
    import java.util.concurrent.atomic.AtomicReference;
    
    /**
     * @author xfyou
     * @date 2018/7/31
     */
    @ToString
    public class Admin implements User {
        private static final AtomicReference<Admin> INSTANCE = new AtomicReference<>();
    
        public Admin() {
            if (!INSTANCE.compareAndSet(null, this)) {
                throw new IllegalStateException("This class only can be initialized once");
            } else {
                System.out.println("Construct");
            }
        }
    
        @Override
        public void addUser(String firstName, String lastName) {
            System.out.println("Add new user");
        }
    }
  • 相关阅读:
    mybatis 绑定 statement 失败
    JDBC链接Mysql失败
    Mysql 链接数据库时区错误
    mybatis 延迟加载
    C++ 虚函数表解析
    运行错误:error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or
    QComboBox的activated与currentIndexChanged的区别
    QT 文件对话框(QFileDialog)
    VS2010 ERROR:c1xx fatal error c1083
    django 在字符串[str(list)]中精确查找
  • 原文地址:https://www.cnblogs.com/frankyou/p/9394871.html
Copyright © 2011-2022 走看看