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");
        }
    }
  • 相关阅读:
    Sql2008 全文索引 简明教程
    sql server 全文检索 使用
    数据库分词查询的优缺点以及英文和中文各自的分词方法(一)
    win10中打开SQL Server配置管理器方法
    Asp.net 中高亮显示搜索关键字简单方法
    EntityFramework优缺点
    LoadXml载入Xhtml文件速度很慢
    c#无限循环线程如何正确退出
    线程的等待方法:join
    C#如何优雅的结束一个线程
  • 原文地址:https://www.cnblogs.com/frankyou/p/9394871.html
Copyright © 2011-2022 走看看