Lazy vs Eager loading in Singleton

Suggested Videos
Part 2 - Singleton Design Pattern - Text - Slides
Part 3 - Why is singleton class sealed - Text - Slides
Part 4 - Thread Safety in Singleton - Text - Slides

In this tutorial we will discuss the difference between Lazy Initialization and Eager Initialization

Lazy Initialization : The lazy initialization of an object improves the performance and avoids unnecessary computation till the point the object is accessed. Further, it reduces the memory footprint during the startup of the program. Reducing the memory print will help faster loading of the application.

(追記) (追記ここまで)

Non-Lazy or Eager Loading : Eager loading is nothing but to initialize the required object before it’s being accessed. Which means, we instantiate the object and keep it ready and use it when we need it. This type of initialization is used in lower memory footprints. Also, in eager loading, the common language runtime takes care of the variable initialization and its thread safety. Hence, we don’t need to write any explicit coding for thread safety.

(追記) (追記ここまで)

Singleton with Lazy keyword (.NET 4.0) : Lazy keyword provides support for lazy initialization. In order to make a property as lazy, we need to pass the type of object to the lazy keyword which is being lazily initialized.

By default, Lazy<T> objects are thread-safe. In multi-threaded scenarios, the first thread which tries to access the Value property of the lazy object will take care of thread safety when multiple threads are trying to access the Get Instance at the same time.

Therefore, it does not matter which thread initializes the object or if there are any thread race conditions that are trying to access this property.


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SingletonDemo
{
classProgram
{
staticvoid Main(string[] args)
{
Parallel.Invoke(
() => PrintStudentDetails(),
() => PrintEmployeeDetails()
);
Console.ReadLine();
}

privatestaticvoid PrintEmployeeDetails()
{
Singleton fromEmployee = Singleton.GetInstance;
fromEmployee.PrintDetails("From Employee");
}

privatestaticvoid PrintStudentDetails()
{
Singleton fromStudent = Singleton.GetInstance;
fromStudent.PrintDetails("From Student");
}
}
}

Singleton.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SingletonDemo
{
publicsealedclassSingleton
{
privatestaticint counter = 0;
private Singleton()
{
counter++;
Console.WriteLine("Counter Value " + counter.ToString());
}
privatestaticreadonlyLazy<Singleton> instance =
newLazy<Singleton>(()=>newSingleton());
publicstaticSingleton GetInstance
{
get
{
return instance.Value;
}
}
publicvoid PrintDetails(string message)
{
Console.WriteLine(message);
}
}
}

Design Patterns tutorial for beginners

1 comment:

It would be great if you can help share these free resources

[フレーム]

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /