SlideShare une entreprise Scribd logo
1  sur  50
Threading
• A multithreaded program contains two or
more parts that can run concurrently.
• Each part of such a program is called a
thread, and each thread defines a separate
path of execution.
• Thus, multithreading is a specialized form of
multitasking.
• Threads improve performance.
• The advantage of threading is the ability to
create applications that use more than one
thread of execution.
• For example, a process can have a user
interface thread that manages interactions
with the user and worker threads that
perform other tasks while the user interface
thread waits for user input.
• They are handled in the .NET Framework with
classes from the base class library.
Multithreading
• There are two distinct types of multitasking:
process-based and thread-based.
• A process is a program that is executing.
• Thus, process-based multitasking is the
feature that allows your computer to run two
or more programs concurrently.
• In process-based multitasking, a program is the
smallest unit of code that can be dispatched by
the scheduler.
• A thread is a dispatchable unit of executable code.
• In a thread-based multitasking environment, all
processes have at least one thread, but they can
have more.
• This means that a single program can perform two
or more tasks at once.
• Process-based multitasking handles the concurrent
execution of programs.
• Thread-based multitasking deals with the
concurrent execution of pieces of the same
program.
• The principal advantage of multithreading is that
it enables you to write very efficient programs
because it lets you utilize the idle time that is
present in most programs.
• A thread can be in one of several states. In
general terms, it can be running.
• It can be ready to run as soon as it gets CPU
time.
• A running thread can be suspended, which is a
temporary halt to its execution.
• It can later be resumed.
• A thread can be blocked when waiting for a
resource. A thread can be terminated, in which
case its execution ends and cannot be resumed.
• The .NET Framework defines two types of
threads: foreground and background.
• By default, when you create a thread, it is a
foreground thread, but you can change it to a
background thread.
• The only difference between foreground and
background threads is that a background
thread will be automatically terminated when
all foreground threads in its process have
stopped.
• C# and the .NET Framework support both
process-based and thread-based multitasking.
• Thus, you can create and manage both
processes and threads.
• The classes that support multithreaded
programming are defined in the
System.Threading namespace.

• using System.Threading;
The Thread Class
• The multithreading system is built upon the
Thread class, which encapsulates a thread of
execution.
• The Thread class is sealed , which means that
it cannot be inherited.
• Thread defines several methods and
properties that help manage threads.
Creating and Starting a Thread
• To create a thread, instantiate an object of type
Thread, which is a class defined in
System.Threading.
• The simplest Thread constructor is shown here:
public Thread(ThreadStart entryPoint)
• Here, entryPoint is the name of the method that
will be called to begin execution of the thread.
• ThreadStart is a delegate defined by the .NET
Framework as shown here:
public delegate void ThreadStart( )
• Thus, your entry point method must have a
void return type and take no arguments.
• the new thread will not start running until you
call its Start( ) method, which is defined by
Thread.
• The Start( ) method has two forms. One is
public void Start( )
Example
class MyThread
{
public int Count;
string TN;
public MyThread(string name) {
Count = 0;
TN = name;
}
// Entry point of thread.

public void Run() {
Console.WriteLine(TN + " starting.");
do {
Thread.Sleep(500);
Console.WriteLine("In " + TN +Count+", Count is " +
Count);
Count++;
} while(Count < 10);
Console.WriteLine(TN + " terminating.");
}
}
class MultiThread {
static void Main() {
Console.WriteLine("Main thread starting.")
MyThread mt = new MyThread("Emp #");
// construct a thread from that object.

Thread newThrd = new Thread(mt.Run);
// Finally, start execution of the thread.

newThrd.Start();
Console.WriteLine("Main thread ending.");
}}}
• Example:
This program creates two
instances of the Thread and uses the
ThreadStart class to specify their target
methods A and B.
• The threads are started and then they are
joined.
using System.Threading;
class Program
{
static void Main()
{
Thread thread1 = new Thread(new ThreadStart(A));
Thread thread2 = new Thread(new ThreadStart(B));
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
}
static void A()
{
Thread.Sleep(100);
Console.WriteLine('A');
}
static void B()
{
Thread.Sleep(1000);

Console.WriteLine('B');
}}
• ThreadStart: You can start threads by passing an
instance of the ThreadStart type in the C#
language.
• Join: When you join a thread, the current thread
stops and waits for the target thread to exit. You
can use Join to ensure all threads are completed
at a certain point in your program.
• Sleep: By using the Sleep method, you can pause
a thread and not have it incur any actual CPU
usage. Instead, the thread is simply delayed for a
certain amount of time, measured in
milliseconds.
Some Simple Improvements
using System.Threading;
class MyThread {
public int Count;
public Thread Thrd;
public MyThread(string name) {
Count = 0;
Thrd = new Thread(this.Run);
Thrd.Name = name;
Thrd.Start();
}
void Run() {
Console.WriteLine(Thrd.Name + " starting.");
do {
Thread.Sleep(500);
Console.WriteLine("In " + Thrd.Name +
", Count is " + Count);
Count++;
} while(Count < 10);
Console.WriteLine(Thrd.Name + " terminating.");
}
}
class MultiThreadImproved {
static void Main() {
Console.WriteLine("Main thread starting.");
MyThread mt = new MyThread("Child #1");
do {
Console.Write(".");
Thread.Sleep(100);
} while (mt.Count != 10);
Console.WriteLine("Main thread ending.");

}
}
Creating Multiple Threads
using System;
using System.Threading;
class MyThread {
public int Count;
public Thread Thrd;
public MyThread(string name) {
Count = 0;
Thrd = new Thread(this.Run);
Thrd.Name = name;
Thrd.Start();
}
// Entry point of thread.

void Run() {
Console.WriteLine(Thrd.Name + " starting.");
do {
Thread.Sleep(500);
Console.WriteLine("In " + Thrd.Name +", Count is “ +
Count);
Count++;
} while(Count < 10);
Console.WriteLine(Thrd.Name + " terminating.");
}
}
class MoreThreads {
static void Main() {
Console.WriteLine("Main thread starting.");
// Construct three threads.

MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
Console.WriteLine("Main thread ending.");
}}
Determining When a Thread End
• IsAlive: Thread provides two means by which
you can determine whether a thread has
ended.
• First, you can interrogate the read-only IsAlive
property for the thread.
• It is defined like this:
public bool IsAlive { get; }
• IsAlive returns true if the thread upon which it
is called is still running. It returns false
otherwise.
class MoreThreads {
static void Main() {
Console.WriteLine("Main thread starting.")
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
do {
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Thrd.IsAlive && mt2.Thrd.IsAlive &&
mt3.Thrd.IsAlive);
Console.WriteLine("Main thread ending.");
}}
Join
• Another way to wait for a thread to finish is to
call Join( ) .
• Its simplest form is shown here:
• public void Join( )

• Join( ) waits until the thread on which it is
called terminates.
• Its name comes from the concept of the
calling thread waiting until the specified
thread joins it.
static void Main() {
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
Console.WriteLine("Child #1 joined.");
mt2.Thrd.Join();
Console.WriteLine("Child #2 joined.");
Next…
Thread Priorities
• ThreadPriority defines the following five
priority settings:

ThreadPriority.Highest
ThreadPriority.AboveNormal
ThreadPriority.Normal
ThreadPriority.BelowNormal
ThreadPriority.Lowest
• The default priority setting for a thread is
ThreadPriority.Normal.
• To understand how priorities affect thread
execution, we will use an example that
executes two threads, one having a higher
priority than the other.
• The threads are created as instances of the
MyThread class.
• The Run( ) method contains a loop that
counts the number of iterations.
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

class MyThread {
public int Count;
public Thread Thrd;
static bool stop = false;
static string currentName;
/* Construct a new thread. Notice that this
constructor does not actually start the
threads running. */
public MyThread(string name) {
Count = 0;
Thrd = new Thread(this.Run);
Thrd.Name = name;
currentName = name;
}
// Begin execution of new thread.
void Run() {
Console.WriteLine(Thrd.Name + " starting.");
do {
Count++;
if(currentName != Thrd.Name) {
currentName = Thrd.Name;
Console.WriteLine("In " + currentName);
}
} while(stop == false && Count < 1000);
stop = true;
Console.WriteLine(Thrd.Name + " terminating.");
}}
class PriorityDemo {
static void Main() {
MyThread mt1 = new MyThread("High Priority");
MyThread mt2 = new MyThread("Low Priority");
// Set the priorities.

mt1.Thrd.Priority = ThreadPriority.Highest;
mt2.Thrd.Priority = ThreadPriority.BelowNormal;
mt1.Thrd.Start();
mt2.Thrd.Start();
mt1.Thrd.Join();
mt2.Thrd.Join();
Console.WriteLine(mt1.Thrd.Name + " thread counted to " + mt1.Count);
Console.WriteLine(mt2.Thrd.Name + " thread counted to " + mt2.Count);
}}
the high-priority thread got approximately 98 percent of the CPU
time. Of course, the precise output you see may vary, depending
on the speed of your CPU and the number of other tasks
running on the system. Which version of Windows you are
running will also have an effect.
Synchronization
• When using multiple threads, you will sometimes
need to coordinate the activities of two or more
of the threads.
• The process by which this is achieved is called
synchronization.
• The most common reason for using
synchronization is when two or more threads need
access to a shared resource that can be used by
only one thread at a time.
• For example, when one thread is writing to a file,
a second thread must be prevented from doing so
at the same time.
• The key to synchronization is the concept of a
lock, which controls access to a block of code
within an object.
• When an object is locked by one thread, no
other thread can gain access to the locked
block of code.
• When the thread releases the lock, the object
is available for use by another thread.
• Synchronization is supported by the keyword
lock .
• The general form of lock is shown here:
lock(lockObj) {
// statements to be synchronized

}
• lockObj is a reference to the object being
synchronized.
• If you want to synchronize only a single
statement, the curly braces are not needed.
• A key point to understand about lock is that
the lock-on object should not be publically
accessible. Why?
• Because it is possible that another piece of
code that is outside your control could lock on
the object and never release it.
class Program
{
static readonly object objectRef = new object();
static readonly object objectRef1 = new object();
public void A1()
{
lock (objectRef1)
{
//Thread.Sleep(100);
Console.WriteLine(Environment.UserName);
}
}
static void A()
{
lock (objectRef)
{
//Thread.Sleep(100);
Console.WriteLine(Environment.SystemDirectory);
}
}
static void Main()
{
Program ob = new Program();
for (int i = 0; i < 10; i++)
{
//ThreadStart start = new ThreadStart(A);
// new Thread(start).Start();
Thread th1 = new Thread(new ThreadStart (A));
th1.Start();
}
for (int i = 0; i < 10; i++)
{
// ThreadStart start1 = new ThreadStart(A1);
//new Thread(start1).Start();
Thread th2 = new Thread(ob.A1);
th2.Start();
}
} }
Monitor Class
• The Monitor Class Provides a mechanism that
synchronizes access to objects. The Monitor
class is a collection of static methods that
provides access to the monitor associated
with a particular object, which is specified
through the method's first argument. the class
provide following method.
• Monitor.Enter() : Acquires an exclusive lock on the
specified object. This action also marks the beginning
of a critical section.
• Monitor.Exit() : Releases an exclusive lock on the
specified object. This action also marks the end of a
critical section protected by the locked object.
• Monitor.Pules() : Notifies a thread in the waiting queue
of a change in the locked object's state.
• Monitor.Wait() : Releases the lock on an object and
blocks the current thread until it reacquires the lock.
• Monitor.PulesAll() : Notifies all waiting threads of a
change in the object's state.
• Monitor.TryEnter() : Attempts to acquire an exclusive
lock on the specified object.
object Class
• C# defines one special class called object that
is an implicit base class of all other classes
and for all other types (including the value
types).
• In other words, all other types are derived
from object .
• This means that a reference variable of type
object can refer to an object of any other
type.
• feature added to the .NET Framework by version
4.0 is the Task Parallel Library (TPL). This library
enhances multithreaded programming in two
important ways.
• First, it simplifies the creation and use of multiple
threads.
• Second, it automatically makes use of multiple
processors.
• Another parallel programming feature added by
.NET 4.0 is PLINQ, which stands for Parallel
Language Integrated Query. PLINQ enables you
to write queries that automatically make use of
multiple processors and parallelism when
appropriate.

Contenu connexe

Tendances

.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs.NET: Thread Synchronization Constructs
.NET: Thread Synchronization ConstructsSasha Kravchuk
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreadingjehan1987
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronizationcaswenson
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in JavaM. Raihan
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamNavaneethan Naveen
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresHitendra Kumar
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Thread model of java
Thread model of javaThread model of java
Thread model of javamyrajendra
 

Tendances (20)

.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Java threads
Java threadsJava threads
Java threads
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
.Net Threading
.Net Threading.Net Threading
.Net Threading
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data Structures
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Thread
ThreadThread
Thread
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Java thread
Java threadJava thread
Java thread
 

En vedette

Threading in c#
Threading in c#Threading in c#
Threading in c#gohsiauken
 
C# Advanced L04-Threading
C# Advanced L04-ThreadingC# Advanced L04-Threading
C# Advanced L04-ThreadingMohammad Shaker
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event HandlingJussi Pohjolainen
 
07 iec t1_s1_oo_ps_session_10
07 iec t1_s1_oo_ps_session_1007 iec t1_s1_oo_ps_session_10
07 iec t1_s1_oo_ps_session_10Niit Care
 
Multi threading design pattern
Multi threading design patternMulti threading design pattern
Multi threading design patternYan Wang
 

En vedette (8)

Threading in c#
Threading in c#Threading in c#
Threading in c#
 
C# Advanced L04-Threading
C# Advanced L04-ThreadingC# Advanced L04-Threading
C# Advanced L04-Threading
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
 
Real time web
Real time webReal time web
Real time web
 
Thread
ThreadThread
Thread
 
07 iec t1_s1_oo_ps_session_10
07 iec t1_s1_oo_ps_session_1007 iec t1_s1_oo_ps_session_10
07 iec t1_s1_oo_ps_session_10
 
Multi threading design pattern
Multi threading design patternMulti threading design pattern
Multi threading design pattern
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
 

Similaire à Threading

Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programmingraksharao
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxArulmozhivarman8
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreadingssusere538f7
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
Multithreaded programming
Multithreaded programmingMultithreaded programming
Multithreaded programmingSonam Sharma
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#Rizwan Ali
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfgiridharsripathi
 
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPTRAVI MAURYA
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptxssuserfcae42
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxSaiDhanushM
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Sachintha Gunasena
 

Similaire à Threading (20)

Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
 
MULTI THREADING.pptx
MULTI THREADING.pptxMULTI THREADING.pptx
MULTI THREADING.pptx
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Threads
ThreadsThreads
Threads
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
Threads
ThreadsThreads
Threads
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreaded programming
Multithreaded programmingMultithreaded programming
Multithreaded programming
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdf
 
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPT
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptx
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
 
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
 
multithreading
multithreadingmultithreading
multithreading
 

Plus de abhay singh (15)

Iso 27001
Iso 27001Iso 27001
Iso 27001
 
Web service
Web serviceWeb service
Web service
 
Unsafe
UnsafeUnsafe
Unsafe
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Networking and socket
Networking and socketNetworking and socket
Networking and socket
 
Namespace
NamespaceNamespace
Namespace
 
Inheritance
InheritanceInheritance
Inheritance
 
Generic
GenericGeneric
Generic
 
Gdi
GdiGdi
Gdi
 
Exception
ExceptionException
Exception
 
Delegate
DelegateDelegate
Delegate
 
Constructor
ConstructorConstructor
Constructor
 
Collection
CollectionCollection
Collection
 
Ado
AdoAdo
Ado
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 

Dernier

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Dernier (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

Threading

  • 2. • A multithreaded program contains two or more parts that can run concurrently. • Each part of such a program is called a thread, and each thread defines a separate path of execution. • Thus, multithreading is a specialized form of multitasking. • Threads improve performance.
  • 3. • The advantage of threading is the ability to create applications that use more than one thread of execution. • For example, a process can have a user interface thread that manages interactions with the user and worker threads that perform other tasks while the user interface thread waits for user input. • They are handled in the .NET Framework with classes from the base class library.
  • 4. Multithreading • There are two distinct types of multitasking: process-based and thread-based. • A process is a program that is executing. • Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently. • In process-based multitasking, a program is the smallest unit of code that can be dispatched by the scheduler.
  • 5. • A thread is a dispatchable unit of executable code. • In a thread-based multitasking environment, all processes have at least one thread, but they can have more. • This means that a single program can perform two or more tasks at once. • Process-based multitasking handles the concurrent execution of programs. • Thread-based multitasking deals with the concurrent execution of pieces of the same program.
  • 6. • The principal advantage of multithreading is that it enables you to write very efficient programs because it lets you utilize the idle time that is present in most programs. • A thread can be in one of several states. In general terms, it can be running. • It can be ready to run as soon as it gets CPU time. • A running thread can be suspended, which is a temporary halt to its execution. • It can later be resumed. • A thread can be blocked when waiting for a resource. A thread can be terminated, in which case its execution ends and cannot be resumed.
  • 7. • The .NET Framework defines two types of threads: foreground and background. • By default, when you create a thread, it is a foreground thread, but you can change it to a background thread. • The only difference between foreground and background threads is that a background thread will be automatically terminated when all foreground threads in its process have stopped.
  • 8. • C# and the .NET Framework support both process-based and thread-based multitasking. • Thus, you can create and manage both processes and threads. • The classes that support multithreaded programming are defined in the System.Threading namespace. • using System.Threading;
  • 9. The Thread Class • The multithreading system is built upon the Thread class, which encapsulates a thread of execution. • The Thread class is sealed , which means that it cannot be inherited. • Thread defines several methods and properties that help manage threads.
  • 10. Creating and Starting a Thread • To create a thread, instantiate an object of type Thread, which is a class defined in System.Threading. • The simplest Thread constructor is shown here: public Thread(ThreadStart entryPoint) • Here, entryPoint is the name of the method that will be called to begin execution of the thread. • ThreadStart is a delegate defined by the .NET Framework as shown here: public delegate void ThreadStart( )
  • 11. • Thus, your entry point method must have a void return type and take no arguments. • the new thread will not start running until you call its Start( ) method, which is defined by Thread. • The Start( ) method has two forms. One is public void Start( )
  • 12. Example class MyThread { public int Count; string TN; public MyThread(string name) { Count = 0; TN = name; }
  • 13. // Entry point of thread. public void Run() { Console.WriteLine(TN + " starting."); do { Thread.Sleep(500); Console.WriteLine("In " + TN +Count+", Count is " + Count); Count++; } while(Count < 10); Console.WriteLine(TN + " terminating."); } }
  • 14. class MultiThread { static void Main() { Console.WriteLine("Main thread starting.") MyThread mt = new MyThread("Emp #"); // construct a thread from that object. Thread newThrd = new Thread(mt.Run); // Finally, start execution of the thread. newThrd.Start(); Console.WriteLine("Main thread ending."); }}}
  • 15.
  • 16. • Example: This program creates two instances of the Thread and uses the ThreadStart class to specify their target methods A and B. • The threads are started and then they are joined.
  • 17. using System.Threading; class Program { static void Main() { Thread thread1 = new Thread(new ThreadStart(A)); Thread thread2 = new Thread(new ThreadStart(B)); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); }
  • 18. static void A() { Thread.Sleep(100); Console.WriteLine('A'); } static void B() { Thread.Sleep(1000); Console.WriteLine('B'); }}
  • 19. • ThreadStart: You can start threads by passing an instance of the ThreadStart type in the C# language. • Join: When you join a thread, the current thread stops and waits for the target thread to exit. You can use Join to ensure all threads are completed at a certain point in your program. • Sleep: By using the Sleep method, you can pause a thread and not have it incur any actual CPU usage. Instead, the thread is simply delayed for a certain amount of time, measured in milliseconds.
  • 20. Some Simple Improvements using System.Threading; class MyThread { public int Count; public Thread Thrd; public MyThread(string name) { Count = 0; Thrd = new Thread(this.Run); Thrd.Name = name; Thrd.Start(); }
  • 21. void Run() { Console.WriteLine(Thrd.Name + " starting."); do { Thread.Sleep(500); Console.WriteLine("In " + Thrd.Name + ", Count is " + Count); Count++; } while(Count < 10); Console.WriteLine(Thrd.Name + " terminating."); } } class MultiThreadImproved { static void Main() {
  • 22. Console.WriteLine("Main thread starting."); MyThread mt = new MyThread("Child #1"); do { Console.Write("."); Thread.Sleep(100); } while (mt.Count != 10); Console.WriteLine("Main thread ending."); } }
  • 23. Creating Multiple Threads using System; using System.Threading; class MyThread { public int Count; public Thread Thrd; public MyThread(string name) { Count = 0; Thrd = new Thread(this.Run); Thrd.Name = name; Thrd.Start(); }
  • 24. // Entry point of thread. void Run() { Console.WriteLine(Thrd.Name + " starting."); do { Thread.Sleep(500); Console.WriteLine("In " + Thrd.Name +", Count is “ + Count); Count++; } while(Count < 10); Console.WriteLine(Thrd.Name + " terminating."); } }
  • 25. class MoreThreads { static void Main() { Console.WriteLine("Main thread starting."); // Construct three threads. MyThread mt1 = new MyThread("Child #1"); MyThread mt2 = new MyThread("Child #2"); MyThread mt3 = new MyThread("Child #3"); Console.WriteLine("Main thread ending."); }}
  • 26.
  • 27. Determining When a Thread End • IsAlive: Thread provides two means by which you can determine whether a thread has ended. • First, you can interrogate the read-only IsAlive property for the thread. • It is defined like this: public bool IsAlive { get; } • IsAlive returns true if the thread upon which it is called is still running. It returns false otherwise.
  • 28. class MoreThreads { static void Main() { Console.WriteLine("Main thread starting.") // Construct three threads. MyThread mt1 = new MyThread("Child #1"); MyThread mt2 = new MyThread("Child #2"); MyThread mt3 = new MyThread("Child #3"); do { Console.Write("."); Thread.Sleep(100); } while (mt1.Thrd.IsAlive && mt2.Thrd.IsAlive && mt3.Thrd.IsAlive); Console.WriteLine("Main thread ending."); }}
  • 29. Join • Another way to wait for a thread to finish is to call Join( ) . • Its simplest form is shown here: • public void Join( ) • Join( ) waits until the thread on which it is called terminates. • Its name comes from the concept of the calling thread waiting until the specified thread joins it.
  • 30. static void Main() { Console.WriteLine("Main thread starting."); // Construct three threads. MyThread mt1 = new MyThread("Child #1"); MyThread mt2 = new MyThread("Child #2"); MyThread mt3 = new MyThread("Child #3"); mt1.Thrd.Join(); Console.WriteLine("Child #1 joined."); mt2.Thrd.Join(); Console.WriteLine("Child #2 joined.");
  • 32. Thread Priorities • ThreadPriority defines the following five priority settings: ThreadPriority.Highest ThreadPriority.AboveNormal ThreadPriority.Normal ThreadPriority.BelowNormal ThreadPriority.Lowest • The default priority setting for a thread is ThreadPriority.Normal.
  • 33. • To understand how priorities affect thread execution, we will use an example that executes two threads, one having a higher priority than the other. • The threads are created as instances of the MyThread class. • The Run( ) method contains a loop that counts the number of iterations.
  • 34. • • • • • • • • • • • • • • • • • • • • • • • • • • • class MyThread { public int Count; public Thread Thrd; static bool stop = false; static string currentName; /* Construct a new thread. Notice that this constructor does not actually start the threads running. */ public MyThread(string name) { Count = 0; Thrd = new Thread(this.Run); Thrd.Name = name; currentName = name; } // Begin execution of new thread. void Run() { Console.WriteLine(Thrd.Name + " starting."); do { Count++; if(currentName != Thrd.Name) { currentName = Thrd.Name; Console.WriteLine("In " + currentName); } } while(stop == false && Count < 1000); stop = true; Console.WriteLine(Thrd.Name + " terminating."); }}
  • 35. class PriorityDemo { static void Main() { MyThread mt1 = new MyThread("High Priority"); MyThread mt2 = new MyThread("Low Priority"); // Set the priorities. mt1.Thrd.Priority = ThreadPriority.Highest; mt2.Thrd.Priority = ThreadPriority.BelowNormal; mt1.Thrd.Start(); mt2.Thrd.Start(); mt1.Thrd.Join(); mt2.Thrd.Join(); Console.WriteLine(mt1.Thrd.Name + " thread counted to " + mt1.Count); Console.WriteLine(mt2.Thrd.Name + " thread counted to " + mt2.Count); }}
  • 36. the high-priority thread got approximately 98 percent of the CPU time. Of course, the precise output you see may vary, depending on the speed of your CPU and the number of other tasks running on the system. Which version of Windows you are running will also have an effect.
  • 37. Synchronization • When using multiple threads, you will sometimes need to coordinate the activities of two or more of the threads. • The process by which this is achieved is called synchronization. • The most common reason for using synchronization is when two or more threads need access to a shared resource that can be used by only one thread at a time. • For example, when one thread is writing to a file, a second thread must be prevented from doing so at the same time.
  • 38. • The key to synchronization is the concept of a lock, which controls access to a block of code within an object. • When an object is locked by one thread, no other thread can gain access to the locked block of code. • When the thread releases the lock, the object is available for use by another thread. • Synchronization is supported by the keyword lock .
  • 39. • The general form of lock is shown here: lock(lockObj) { // statements to be synchronized } • lockObj is a reference to the object being synchronized. • If you want to synchronize only a single statement, the curly braces are not needed.
  • 40. • A key point to understand about lock is that the lock-on object should not be publically accessible. Why? • Because it is possible that another piece of code that is outside your control could lock on the object and never release it.
  • 41. class Program { static readonly object objectRef = new object(); static readonly object objectRef1 = new object(); public void A1() { lock (objectRef1) { //Thread.Sleep(100); Console.WriteLine(Environment.UserName); } }
  • 42. static void A() { lock (objectRef) { //Thread.Sleep(100); Console.WriteLine(Environment.SystemDirectory); } } static void Main() { Program ob = new Program();
  • 43. for (int i = 0; i < 10; i++) { //ThreadStart start = new ThreadStart(A); // new Thread(start).Start(); Thread th1 = new Thread(new ThreadStart (A)); th1.Start(); } for (int i = 0; i < 10; i++) { // ThreadStart start1 = new ThreadStart(A1); //new Thread(start1).Start(); Thread th2 = new Thread(ob.A1); th2.Start(); } } }
  • 44.
  • 45. Monitor Class • The Monitor Class Provides a mechanism that synchronizes access to objects. The Monitor class is a collection of static methods that provides access to the monitor associated with a particular object, which is specified through the method's first argument. the class provide following method.
  • 46. • Monitor.Enter() : Acquires an exclusive lock on the specified object. This action also marks the beginning of a critical section. • Monitor.Exit() : Releases an exclusive lock on the specified object. This action also marks the end of a critical section protected by the locked object. • Monitor.Pules() : Notifies a thread in the waiting queue of a change in the locked object's state. • Monitor.Wait() : Releases the lock on an object and blocks the current thread until it reacquires the lock. • Monitor.PulesAll() : Notifies all waiting threads of a change in the object's state. • Monitor.TryEnter() : Attempts to acquire an exclusive lock on the specified object.
  • 47.
  • 48. object Class • C# defines one special class called object that is an implicit base class of all other classes and for all other types (including the value types). • In other words, all other types are derived from object . • This means that a reference variable of type object can refer to an object of any other type.
  • 49.
  • 50. • feature added to the .NET Framework by version 4.0 is the Task Parallel Library (TPL). This library enhances multithreaded programming in two important ways. • First, it simplifies the creation and use of multiple threads. • Second, it automatically makes use of multiple processors. • Another parallel programming feature added by .NET 4.0 is PLINQ, which stands for Parallel Language Integrated Query. PLINQ enables you to write queries that automatically make use of multiple processors and parallelism when appropriate.