System.Lazy <T> delay loading

Original: System.Lazy <T> delay loading

In many cases, some objects need to be loaded according to a logic or dynamically loaded during use. In some cases, if not lazy loading, it may affect the efficiency even throw Timeout Exception. Such as network operations, database operations, file IO operations

Directly on the code, to help us understand how to achieve the object of lazy loading by Lazy class.

Copy the code
   class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            the foreach ( var Item in Product.GetProductListLazy ()) 
            { 
                // following operations performs class constructor Product 
                Product P = item.Value;
                 // constructor executes complete 
                int ID = item.Value.Id; 
                Console.WriteLine (ID); 
            } 
            the Console.ReadKey (); 
        } 
    } 

    public  class Product 
    { 
        public  int Id {set; get; }
        public string Title { set; get; }

        public Product()
        {
            Thread.Sleep(5000);
        }
        public static IList<Lazy<Product>> GetProductListLazy()
        {
            try
            {
                Console.WriteLine("非LazyLoad加载");
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff"));
                Lazy<Product> product1 = new Lazy<Product>(() => new Product() { Id = 1, Title = "产品 1" });
                Lazy<Product> product2 = new Lazy<Product>(() => new Product() { Id = 2, Title = "产品 2" });
                Lazy<Product> product3 = new Lazy<Product>(() => new Product() { Id = 3, Title = "产品 3" });
                return new List<Lazy<Product>> { product1, product2, product3 };
            }
            finally {
                Console.WriteLine(DateTime.Now.ToString("HH:mm.ss.fff"));            
            }
        }

        public static IList<Product> GetProductList()
        {
            try
            {
                Console.WriteLine("非LazyLoad加载");
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff"));
                Product product1 = new Product() { Id = 1, Title = "产品 1" };
                Product product2 = new Product() { Id = 1, Title = "产品 2" };
                Product product3 = new Product() { Id = 1, Title = "产品 3" };
                return new List<Product> { product1, product2, product3 };
            }
            finally {
                Console.WriteLine(DateTime.Now.ToString("HH:mm.ss.fff"));
            }
        }
    }
Copy the code

In another case, we know that multiple threads of a single piece design pattern data synchronization needs to be done:

Copy the code
    public class Singleton
    {
        private static Singleton _Instance = new Singleton();

        public static Singleton GetInstance()
        {
            return _Instance;
        }
    }
Copy the code

This will ensure that the case of multi-threaded, only one instance, because when the program is loaded, to initialize the object, but also because of this, lost the advantage of a one-piece, demand loading.

So, we can achieve:

Copy the code
    public class SingletonLazy
    {
        private static Lazy<SingletonLazy> _Instance = new Lazy<SingletonLazy>();

        public static SingletonLazy GetInstance()
        {
            return _Instance.Value;
        }
    }
Copy the code

In this case, an instance can be guaranteed while achieving a delay loading, loaded on demand.

Above introduction is over, we welcome the exchange and correction

Attached:

Reference article: http://www.codeproject.com/Articles/612801/Lazy-Object-Initialization-with-System-Lazy-of-Tc

Demo Download: http://files.cnblogs.com/wpfworld/LazyLoadDemo.rar

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12082282.html