You are on page 1of 20

Multi Threading

Multi Tasking, Multi Processing


 Program :-A sequence of instructions
that can be executed by a computer.
 Multi Tasking :- Multitasking is the
ability to execute more than one
Program (task) at the same time.

There are two basic types of


multitasking: preemptive and
cooperative
 Preemptive Multitasking :-the
operating system divides the CPU
time to each program in slices.
 Cooperative :-cooperative multitasking,
each program can control the CPU for
as long as it needs it .

OS/2, Windows 95, Windows NT, the Amiga


operating system and UNIX use preemptive
multitasking, whereas Microsoft Windows 3.x and the
MultiFinder (for Macintosh computers) use
cooperative multitasking.
Multi Processing
 Multi Processing :- Multiprocessing is running a
system with more than one processor .
Multiprocessing can be said to be either asymmetric or
symmetric
Asymmetric :-Asymmetric multiprocessing designates
some processors to perform system tasks only, and
others to run applications only.
Symmetric :- [SMP] Symmetric multiprocessing, often
abbreviated SMP, allows either system or user tasks
to run on any processor.
Multi Threading
Each application/program that is running on a
system is a process. (Set of some code.)
Again within the main Process more than one sub process
can be divided those can be given specific processor
time to do the allocated task.
The sub processes having their own execution path can be
defined as a thread.
An ability of programming witch supports to create more
than one thread within a single application (process)
called as Multi threading.
Multithreading is a powerful tool for
creating high performance applications,
especially those that require a great user
interaction,Data base interaction,
Networking interaction…

Tasks those should be handled using Multithreading:-


 Printing
 Asynchronous Connection
 Asynchronous Query etc.

As an Example :- MS Word Application.


more than one task as printing,Editing,Editing and many more can be performed at a
time using multithreading.
Program2
1 Sec.
½ Sec.
--------

½ Sec.
2 Sec.
--------- O/S Processor

Program1 1 Sec.
---------
Multithreading in . NET

.NET Runtime (CLR) is maintaining a service called


as Thread-Support that enables .NET application to
divide into more than one Threads.
System.Threading Namespace
Thread Class
ThreadStart Delegate

task1()
task2()

ThreadStart ts1=new ThreadStart(task1)


ThreadStart ts2=new ThreadStart(task2)

Thread t1=new Thread(ts1)


Thread t2=new Thread(ts2)

Note:- Delegate is nothing but like a Function Pointer.


In VB.NET ThreadStart(AddressOf ts1)
Basic Example of Thread using VB and Csharp
[Console Application]
Advantage of Multi Threading
 Improved responsiveness :-If the
application is performing operations that take a perceivably
long time to complete, these operations can be put into a
separate thread which will allow the application to continue
to be responsive to the user.
 Faster application :-if there are a number of
calculations to be performed or the contents of a file are
being processed, then there the application can be made
faster by performing multiple operations at the same time.
 Prioritization :-Threads can be assigned a
priority which would allow higher priority tasks to take
precedence over lower priority tasks .
Disadvantage of Multiple Thread
 Programming and debugging is more
complex :-With multithreaded applications
there is always a risk of Deadlocks and Debugging
of an application becomes more difficult.
 Threads add overhead to the system :-
To maintain a track of the large number of
threads,obviously going to consume processor
time.
If there are too many threads then each thread may
not be given sufficient time to execute.In addition,
each thread is scheduled for execution less
frequently due to the volume and time slice
committed to each thread.
II Example of Thread
[Windows Application]
Life Cycle of Thread
Sleep Wakeups

Thread Start Suspend Resume

Abort
Members of Thread Class
 Start() :- To start a particular Thread.
 **Sleep(m.sec.) :-To rest a thread for specified duration.
 Suspend() :-To halt a thread until explicitly resumed.
 Resume() :-To restart execution of a suspended thread.
 Abort() :-To stop a running thread execution permanently .
 **Currentthread:-Returns the thread currently running.
 ThreadState:-Returns the state of the thread-
running,suspended…
 Priority:- Specifies a priority of a thread.

Note:- ** Shared or Static Member.


III Example to implement other
methods and events of thread.
synchronize access to resources in multithreaded applications.

Since each thread executes asynchronously in a


multithreaded application.
The asynchronous nature of threads must be
coordinated means that access to resources such
as file handles, network connections, and
memory.
Otherwise, two or more threads could access the
same resource at the same time, each unaware of
the other's actions. The result is unpredictable
data corruption.
The lock keyword can be used to ensure that a block of
code runs to completion without interruption by other
threads.
A lock statement begins with the keyword lock, which is
given an object as an argument, and followed by a code
block that is to be executed by only one thread at a time.

public void Function(){


Object lockThis = new Object();
lock(lockThis)
{
// Access thread-sensitive resources.
}
}
VB.NET
Private Sub f1()

SyncLock ("updating_f1.txt")

‘Code to handle a particular file

End SyncLock

End Sub
Thread Class
ThreadStart Delegate

task1()
task2()

ThreadStart ts1=new
Setting Priority to a particular Thread
ThreadStart(task1)
ThreadStart ts2=new
ThreadStart(task2)

Thread t1=new Thread(ts1)


Thread t2=new Thread(ts2)
t1.Priority=ThreadPriority.Lowest
Note:- Delegate is nothing but like
a Function Pointer.
Normal
In VB.NET ThreadStart(AddressOfHighest
ts1)
Basic Example of Thread using

You might also like