# Thread safety
- When multiple threads accessing same variable or memory space at same time, sometimes one threads reads the variable and another thread write to variable at same time. Due to which program becomes error prone, which does not give expected result. To ensure safe execution of program during multithreading we follow certain mechanisms which are commonly known as thread safety.
## Thread safety removes the following conditions in the code:
1. Race Condition - two threads accessing variable at same time, one thread reads, another writes
2. Deadlocks - occurs when one thread waiting for another thread to release a resource.
### Question: Is the C# Static Constructor Thread -Safe?
Static constructors are guaranteed to be run only once per application domain, before any instances of a class are created or any static members are accessed. Using a Static Constructor actually is Thread-Safe.
## Parallel execution in threads
Parallel.Invoke method of .NET Framework 4.0, we can easily make parallel call to two methods.
Parallel.Invoke(
() => PrintStudentdetails(),
() => PrintEmployeeDetails()
);
## 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.
Tuesday, 8 March 2022
Threads
Subscribe to:
Post Comments (Atom)
Search This Blog
Creating your first "Alexa" Skill
Index What is Alexa What is Alexa Skill? Why is it required when Alexa already equipped with voice assistant? Dev...
About Me
Menu
-
Index What is Alexa What is Alexa Skill? Why is it required when Alexa already equipped with voice assistant? Dev...
-
Adding Azure AD B2C to React Native App Register your app in Azure active directory 1. Go to azure ad b2c, app registratio...
-
# Project file 1. .net Core project file no longer contains file or folder reference - all files and folder present within the root fol...
No comments:
Post a Comment