SlideShare a Scribd company logo
Multithreading
and
concurrency
SHYMA’A QADOOM
Concurrency
• is the tendency for things
to happen at the same time
in a system. ... Figure 1:
Example of concurrency at
work: parallel activities that
do not interact have simple
concurrency issues. It is when
parallel activities interact or
share the same resources that
concurrency issues become
important.
• Concurrent
• Two queues and one
coffee machine.
• Parallel
• Two queues and two
coffee machines.
What are threads in
C#?
A thread is defined as the
execution path of a program. Each
thread defines a unique flow of
control. If your application involves
complicated and time-consuming
operations, then it is often helpful
to set different execution paths or
threads, with each thread
performing a particular job.
In the main thread in the “Main” method we create three other threads, methods that
display characters in the console and you can see that at the same time characters
from the “Main” method are displaying, i.e. our program is multithreaded.
You can also wait for the thread to finish working, then you need to add the Join()
method
You can see the differences that the threads are executing now in turn.
We can also put the thread to sleep for a while, let’s put the thread y to sleep:
Synchronization - lock
Keyword
Since we got inconsistent result, we need to find a way to
programmatically enforce synchronized access to the shared
resources. The System.Threading provides a number of
synchronization types as we'll see in the following sections.
In this section, we're going to use lock keyword for the synchronized
access to shared resources. The lock allows us to define a scope of
code that must be synchronized between threads so that incoming
threads cannot interrupt the current thread. To use the lock, we
need to specify a token that must be acquired by a thread to enter
inside the scope which has been locked. So, when we want to lock
down a private instance method, we can simply pass in an object
reference to the current type:
Synchronization - Mutex
A Mutex is like the lock, however, it can work
across multiple processes. But it is slower than
the lock.
Mutex allows us to call the WaitOne() method to
lock and ReleaseMutex() to unlock. Note that
a Mutex can be released only from the same
thread which obtained it. The primary use for a
cross-process Mutex is to ensure that only one
instance of a program can run at a time.
Synchronization -
Semaphore
A Semaphore restricts the number of simultaneous
users of the shared resources up to a maximum
number. It ensures not more than a specific
number of concurrent threads, which can access a
resource.
A Semaphore is like a football team, it has a
certain capacity Once it is full, no more player can
enter and other players wait outside in the form of
queue. For each player that leaves, another one
enters from the head of the queue.
• On a computer, the
operating system loads and
starts applications. Each
application or service runs as
a separate process on the
machine. The following image
illustrates that there are quite
a few processes running than
there are actual applications
running in the system. Many
of these processes are
background operating system
processes that are started
automatically when the OS
loads.
• Obtaining Current Thread
Information
• To illustrate the basic use
of the Thread type, suppose
you have a console
application in which the
Current Thread property
retrieves a Thread object
that represents the currently
executing thread.
Tasks
• The thread is a low-level tool designed to provide concurrency and
therefore has some limitations.
• It is easy to pass data to it, but it is not easy to get a return value from it.
• You can not tell a thread to execute another thread when it ends. Instead,
it is necessary to use the Join() method, which means blocking the thread at
that time.
• 3. Direct use of threads also worsens performance.
The Task class is the solution to all these problems.
Go to demos…
In this example, the WriteY() method will be executed immediately, then we
have to wait 3 seconds to execute the next threads, but the time must be given
in milliseconds.
While there is a problem with
threads, look at the example below:
At the same time, two threads are executed that start the same method,
this will display the word “Done” twice in the console.
How can you check the security of threads? The keyword “lock” is used
for this. So, we will secure our threads in the example below.
The solution used in the above example guarantees that one block can
be put on one thread at a time, which means that the word “Done” will
be displayed once.
Thank you..
Have a nice day!

More Related Content

Similar to Multithreading and concurrency.pptx

Multithreading
MultithreadingMultithreading
Multithreading
F K
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
Tuan Chau
 
Java
JavaJava
Process coordination
Process coordinationProcess coordination
Process coordination
Sweta Kumari Barnwal
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
Himanshu Rajput
 
Multithreading
MultithreadingMultithreading
Multithreading
Ravi Chythanya
 
Multi threading
Multi threadingMulti threading
Multi threading
gndu
 
You are to simulate a dispatcher using a priority queue system.New.pdf
You are to simulate a dispatcher using a priority queue system.New.pdfYou are to simulate a dispatcher using a priority queue system.New.pdf
You are to simulate a dispatcher using a priority queue system.New.pdf
gardenvarelianand
 
Copy of Multithreaded Programming.pptx
Copy of Multithreaded Programming.pptxCopy of Multithreaded Programming.pptx
Copy of Multithreaded Programming.pptx
YehanNk
 
Thread
ThreadThread
Multi-threaded web crawler in Ruby
Multi-threaded web crawler in RubyMulti-threaded web crawler in Ruby
Multi-threaded web crawler in Ruby
Polcode
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
Ducat India
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
Neeraj Kaushik
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
 

Similar to Multithreading and concurrency.pptx (20)

Multithreading
MultithreadingMultithreading
Multithreading
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
 
Java
JavaJava
Java
 
Process coordination
Process coordinationProcess coordination
Process coordination
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multi threading
Multi threadingMulti threading
Multi threading
 
You are to simulate a dispatcher using a priority queue system.New.pdf
You are to simulate a dispatcher using a priority queue system.New.pdfYou are to simulate a dispatcher using a priority queue system.New.pdf
You are to simulate a dispatcher using a priority queue system.New.pdf
 
Copy of Multithreaded Programming.pptx
Copy of Multithreaded Programming.pptxCopy of Multithreaded Programming.pptx
Copy of Multithreaded Programming.pptx
 
Thread
ThreadThread
Thread
 
Multi-threaded web crawler in Ruby
Multi-threaded web crawler in RubyMulti-threaded web crawler in Ruby
Multi-threaded web crawler in Ruby
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 

Recently uploaded

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 

Recently uploaded (20)

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 

Multithreading and concurrency.pptx

  • 2. Concurrency • is the tendency for things to happen at the same time in a system. ... Figure 1: Example of concurrency at work: parallel activities that do not interact have simple concurrency issues. It is when parallel activities interact or share the same resources that concurrency issues become important.
  • 3. • Concurrent • Two queues and one coffee machine. • Parallel • Two queues and two coffee machines.
  • 4.
  • 5.
  • 6. What are threads in C#? A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. In the main thread in the “Main” method we create three other threads, methods that display characters in the console and you can see that at the same time characters from the “Main” method are displaying, i.e. our program is multithreaded. You can also wait for the thread to finish working, then you need to add the Join() method You can see the differences that the threads are executing now in turn. We can also put the thread to sleep for a while, let’s put the thread y to sleep:
  • 27.
  • 28. Synchronization - lock Keyword Since we got inconsistent result, we need to find a way to programmatically enforce synchronized access to the shared resources. The System.Threading provides a number of synchronization types as we'll see in the following sections. In this section, we're going to use lock keyword for the synchronized access to shared resources. The lock allows us to define a scope of code that must be synchronized between threads so that incoming threads cannot interrupt the current thread. To use the lock, we need to specify a token that must be acquired by a thread to enter inside the scope which has been locked. So, when we want to lock down a private instance method, we can simply pass in an object reference to the current type:
  • 29. Synchronization - Mutex A Mutex is like the lock, however, it can work across multiple processes. But it is slower than the lock. Mutex allows us to call the WaitOne() method to lock and ReleaseMutex() to unlock. Note that a Mutex can be released only from the same thread which obtained it. The primary use for a cross-process Mutex is to ensure that only one instance of a program can run at a time.
  • 30. Synchronization - Semaphore A Semaphore restricts the number of simultaneous users of the shared resources up to a maximum number. It ensures not more than a specific number of concurrent threads, which can access a resource. A Semaphore is like a football team, it has a certain capacity Once it is full, no more player can enter and other players wait outside in the form of queue. For each player that leaves, another one enters from the head of the queue.
  • 31. • On a computer, the operating system loads and starts applications. Each application or service runs as a separate process on the machine. The following image illustrates that there are quite a few processes running than there are actual applications running in the system. Many of these processes are background operating system processes that are started automatically when the OS loads.
  • 32. • Obtaining Current Thread Information • To illustrate the basic use of the Thread type, suppose you have a console application in which the Current Thread property retrieves a Thread object that represents the currently executing thread.
  • 33.
  • 34. Tasks • The thread is a low-level tool designed to provide concurrency and therefore has some limitations. • It is easy to pass data to it, but it is not easy to get a return value from it. • You can not tell a thread to execute another thread when it ends. Instead, it is necessary to use the Join() method, which means blocking the thread at that time. • 3. Direct use of threads also worsens performance. The Task class is the solution to all these problems.
  • 36. In this example, the WriteY() method will be executed immediately, then we have to wait 3 seconds to execute the next threads, but the time must be given in milliseconds.
  • 37. While there is a problem with threads, look at the example below: At the same time, two threads are executed that start the same method, this will display the word “Done” twice in the console.
  • 38. How can you check the security of threads? The keyword “lock” is used for this. So, we will secure our threads in the example below. The solution used in the above example guarantees that one block can be put on one thread at a time, which means that the word “Done” will be displayed once.
  • 39. Thank you.. Have a nice day!