- Prologue to C# Thread Synchronization
- Sentence structure of C# Thread Synchronization
- Elements of C# Thread Synchronization
- Guides to Implement C# Thread Synchronization
- C# Lock
- Strategies to oversee Synchronization
- Conclusion
- Thread thread_name = new Thread(method_name);
- thread_name.Start();
- thread_name.
- Join();
- or
- Thread thread_name = new Thread(method_name);
- thread_name.Start();
- method_name()
- {
- lock(this)
- {
- //thread_name thread is executed
- }
- }
- Any asset can be gotten to by the string for the expected measure of time in a multithreading program yet assuming that few strings attempt to get to similar asset, the sharing of assets by a few strings immediately or non concurrently turns into a basic errand and the framework might stop the execution.
- To beat this issue, synchronization of strings is vital. By synchronization of the string, just that specific string can approach the asset for a specific measure of time with practically no break from different strings.
- Synchronization of strings should be possible utilizing join watchword and lock catchphrase.
- Whenever join watchword is utilized on a string, the string is permitted to finish its execution without the interference of some other strings.
- At the point when the lock catchphrase is utilized, the asset on which the string is executing is locked for the time until the string finishes execution.
- utilizing System;
- utilizing System.Threading;
- //a namespace called program is made
- namespace program
- {
- //a class called check is characterized
- class check
- {
- //fundamental strategy is called
- static void Main(string[] args)
- {
- //an occasion of the string class is made which works on a technique
- String firstthread = new Thread(secondfunction);
- //start strategy is utilized to start the execution of the string
- firstthread.Start();
- //join strategy shuts down any remaining strings while the current string is executing
- firstthread.Join();
- String secondthread = new Thread(firstfunction);
- secondthread.Start();
- secondthread.Join();
- }
- private static void firstfunction(object obj)
- {
- for(inti=1;i
- }
- private static void secondfunction(object obj)
- {
- for(inti=1;i<3;i++)
- {
- Console.WriteLine
- }
- }
- }
- }
- using System;
- using System.Threading;
- //a class called create is created
- class create
- {
- public void func()
- {
- //lock is called on this method
- lock (this)
- {
- for (inti = 1; i<= 2; i++)
- {
- Console.WriteLine(“The thread is executing”);
- }
- }
- }
- }
- class check
- {
- public static void Main(string[] args)
- {
- //an instance of the create class is created
- create c = new create();
- //an instance of the thread class is created which operates on the method in another class
- Thread firstthread = new Thread(c.func);
- firstthread.Start();
- Thread secondthread = new Thread(c.func);
- secondthread.Start();
- }
- }
- That way the strings are executed simultaneously. The result of the program is displayed in the depiction above.
- Synchronization is a strategy that permits just one string to get to the asset for the specific time.
- No other string can hinder until the doled out string gets done with its job.
- In multithreading program, strings are permitted to get to any asset for the expected execution time.
- Strings share assets and executes non concurrently. Getting to shared assets (information) is basic errand that occasionally may end the framework. We manage it by making strings synchronized.
- It is for the most part utilized in the event of exchanges like store, pull out and so forth
- Consistency Maintain
- No Thread Interference
- We can utilize C# lock catchphrase to execute program simultaneously. It is utilized to get lock for the current string, execute the undertaking and afterward discharge the lock.
- It guarantees that other string doesn’t interfere with the execution until the execution finish.
- Here, we are making two models that executes nonconcurrently and simultaneously.
- utilizing System;
- utilizing System.Threading;
- namespace JoinDemo
- {
- class Example
- {
- static void Main(string[] args)
- {
- Thread t1 = new Thread(Func1);
- t1.Start();
- Thread t2 = new Thread(Func2);
- t2.Start();
- t1.Join();
- t2.Join();
- }
- private static void Func2(object obj)
- {
- Console.WriteLine(“Thread1 is executed”);
- }
- private static void Func1(object obj)
- {
- Console.WriteLine(“Thread2 is executed”);
- }
- }
- Output :
- Thread2 is executed
- Thread1 is executed
- utilizing System;
- utilizing System.Threading;
- namespace LockDemo
- {
- class LockDisplay
- {
- public void DisplayNum()
- {
- lock (this)
- {
- for (int i = 1; i <= 5; i++)
- {
- Thread.Sleep(100);
- Console.WriteLine(“i = {0}”, i);
- }
- }
- }
- }
- class Example
- {
- public static void Main(string[] args)
- {
- LockDisplay obj = new LockDisplay();
- Console.WriteLine(“Threading using Lock”);
- Thread t1 = new Thread(new ThreadStart(obj.DisplayNum));
- Thread t2 = new Thread(new ThreadStart(obj.DisplayNum));
- t1.Start();
- t2.Start();
- }
- }
- }
- Output :
- I = 1
- I = 2
- I = 3
- I = 4
- I = 5
- I = 1
- I = 2
- I = 3
- I = 4
- I = 5
Prologue to C# Thread Synchronization :-
The strategy of the assets being accessible for just each string in turn without the interference of some other string until the errand relegated to the current string is done is called synchronization in C#.
As a matter of fact, any asset can be gotten to by the string for the expected measure of time in a multithreading program and the assets are shared and executed nonconcurrently by the strings which is a basic assignment and may bring about halting the framework and thus strings should be executed simultaneously and by synchronization of strings, we can keep up with consistency of the strings and ensure no different strings meddle during the execution of one string.
Sentence structure of C# Thread Synchronization :-
The following is the sentence structure of C#Thread Synchronization is as per the following:
Where thread_name is the name of the string and method_name is the name of the strategy got to by this string alone beginning from the time thread_name.Start() is called, and thread_name.Join() holds up till the fruition of this string by halting the execution of any remaining strings.
Lock watchword inside the technique, method_name locks the string execution so no different strings can get to the strategy until the consummation of the current string.
Elements of C# Thread Synchronization :-
Guides to Implement C# Thread Synchronization :-
The following are the instances of C# Thread Synchronization:
Model #1 :
C# program to show synchronization of strings utilizing join catch phrase.
Output:
Clarification: In the above program, a namespace called program is made. Then, at that point, a class called check is characterized inside which the primary strategy is called.
Then, at that point, an occasion of a string is made to work on a technique, which is started utilizing Start() strategy and join() strategy is utilized on a similar string to ensure its execution isn’t hindered by different strings. Subsequently the result is shown in succession simultaneously. The result of the program is displayed in the depiction above.
Model #2
:C# program to show synchronization of strings utilizing lock catchphrase.
Output:
Clarification: In the above program, a class called to make is made with which the strategy is characterized for which we have utilized a lock catchphrase meaning the string working on this technique locks the technique for itself until it finishes execution without permitting different strings to get to the technique.
Benefit of Thread Synchronization:
C# Lock :-
Synchronization in C# :
Synchronization in C# is a system that ensures just one cycle or string gets to the basic part of the program. The wide range of various strings need to delay until the basic segment is free before they can enter it.
A portion of the benefits of string synchronization is given as follows:
1. Synchronization can be utilized to accomplish common rejection for example just one interaction or string gets to the basic part of the program.
2. No string or interaction has a boundless holding up time utilizing synchronization.
3. Synchronization is an intrinsically fair interaction.
4. All the solicitations are allowed in a particular request utilizing synchronization.
5. Synchronization can be utilized for asset designation also. It ensures that just one cycle or string can utilize an asset at a time.
Strategies to oversee Synchronization :-
Synchronization can be taken care of utilizing different strategies. These techniques are separated into 4 classes overall. These are as per the following:
1. Blocking Methods
2. Locking Constructs
3. No hindering synchronization
4. Signaling
Programs that exhibit a couple of the above strategies are given as follows:
Join :
In string synchronization, join is an obstructing instrument that stops the calling string. This is done work the string whose join strategy was called has finished its execution. A program that exhibits the join strategy is given as follows:
Lock :
Lock is a synchronization technique that is utilized to secure in the current string so no other string can intrude on the execution of the locked string. After the string execution is finished, it is opened.
A program that exhibits the lock strategy is given as follows:
Conclusion :-
In this instructional exercise, we comprehend the idea of ThreadSynchronization in C# through definition, linguistic structure, and working of Thread synchronization through programming models and their results.