Link Search Menu Expand Document

Fundamentals of Lazy Initialization in C# - Part-2


In this video, we’ll be going to explore lazy initialization in more detail. We’ll learn how lazy objects behave in a multi-threaded environment and then we’ll learn how we can change thread behavior by passing thread-safety mode in a lazy constructor.

Welcome back to the second part of this session where we are learning about the lazy class of #C. Now let’s talk about multi-threaded involvement. Do you think objects created with lazy classes are thread-safe? What if multiple threads are raising it to initialize it? Well, the answer is that objects created with lazy class are thread-safe. Let’s see it practically. Now here, I have defined one static method ‘Get Object Value’ [static void GetObjectValue(object LazyPatientDetail)] which accepts a lazy object as an input parameter. Then we create the actual object by accessing the value property of the lazy object and writing out the object information in the console window [console.WriteLine(“PatientDetail.PatientId=”+ patientDetail.PatientId);].

Programming Essentials Initialization

I have initialized the lazy object. There are three separate threads that will execute the ‘Get Object Value’ method and try to create an object by accessing the value property. We should get the message in the console three times. Run this application and see the behavior. As you can see here, that constructor is called one time, and this method is called three times, and prints information.

We can change this thread behavior by parsing the other lazy thread-safety mode, which has three options. The first one is Execution and Publication. The meaning of this option is only one concurrent thread will attempt to create the underlying value and on successful creation, all waiting threads will receive the same value which we have already seen earlier.

Programming Essentials C#

Another option is publication only. This means when multiple concurrent threads will attempt to create the underlying value, it allows all the threads to run the initialization method. Let’s pass this parameter and observe the difference. Here you can see that with this option, the constructor of the class is called three times. That means using this option, it allows all the threads to run the initialization method.

Now, what if I select the none option? As its name suggests, this is not thread-safe. When I select this option and run the app, now I’m getting the exception. This means that none option will allow only one thread to initialize this lazy object. Now let’s move on and see how a lazy class can catch the exception that is thrown from the initialization.

For more videos and articles visit:

👉 GET YOUR FREE API KEY for PDF.co

https://app.pdf.co/signup?utm_source=youtube

✅ ON-PREMISE SDK FREE TRIAL:

https://bytescout.com/download/web-installer?utm_source=youtube

🔎 SDK DOCUMENTATION:

https://bytescout.com/documentation/

🔑 WEB API DOCUMENTATION :

https://pdf.co/rest-web-api