コード例 #1
0
 /**
  * Get heavy object.
  */
 public Heavy GetHeavy()
 {
     if (_heavy == null)
     {
         _heavy = new Heavy();
     }
     return(_heavy);
 }
コード例 #2
0
        /**
         * Get heavy object.
         */
        public Heavy GetHeavy()
        {
            if (_heavy == null)
            {
                lock (SyncObject)
                {
                    if (_heavy == null)
                    {
                        _heavy = new Heavy();
                    }
                }

                return(_heavy);
            }

            return(_heavy);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            // Simple lazy loader - not thread safe
            HolderNaive holderNaive = new HolderNaive();
            Heavy       heavy       = holderNaive.GetHeavy();

            Console.WriteLine("heavy={0}", heavy);

            // Thread safe lazy loader, but with heavy synchronization on each access
            HolderThreadSafe holderThreadSafe = new HolderThreadSafe();
            Heavy            another          = holderThreadSafe.GetHeavy();

            Console.WriteLine("another={0}", another);

            // The most efficient lazy loader utilizing Java 8 features
            HolderLazy java8Holder = new HolderLazy();
            Heavy      next        = java8Holder.GetHeavy();

            Console.WriteLine("next={0}", next);
            Console.WriteLine("Hello World!");
        }