Title: Multithreading
1Multithreading
2Introduction
- Multithreading is a programming mechanism where
multiple programming actions are processed
simultaneously. - An analogy would be the human body Respiration,
blood circulation, and digestion can occur
concurrently. - The .Net Framework Class Library makes
concurrency primitives available to programmers. - An example
- A user wishes to download a large audio or video
clip from the World Wide Web. - The user does not want to wait until the entire
clip downloads before starting the playback. - To solve this problem, the programmer can put
multiple threads to work - One thread downloads a clip.
- Another thread plays the clip.
- These activities, or tasks, occur concurrently.
- To avoid choppy playback, we synchronize the
threads so that the player thread does not begin
until there is sufficient amount of clip in
memory. - Garbage Collection is another example of
multithreading.
3Thread States (Continued)
- At any time, a thread is said to be in one of
several thread states. - During the life-cycle of a thread, it will
transition from one state to another. - Two classes are needed for multithreaded
applications Thread and Monitor.
(System.Threading namespace) - A new thread begins its lifecyle in the Unstarted
state. The thread remains in this state until
your program calls Thread method Start. - When the later method is called, the thread is
placed in the Started (Ready) state. - The thread that invoked Start and the newly
Started thread and any other threads in the
program execute concurrently. - The highest priority started thread enters the
Running state. It begins executing when the
operating system assigns a processor to the
thread.
4Thread States (Continued)
- When a thread becomes a running thread, it
executes its ThreadStart delegate, which
specifies the actions the thread will perform
during its lifecycle. - The ThreadStart delegate is an argument to the
Thread Constructor. - The ThreadStart delegate is a void method that
takes no arguments. - A Running thread enters the Stopped (or Dead)
state when its ThreadStart delegate terminates. - A program can force a thread into the Stopped
state by calling the Abort method. - Method Abort throws a ThreadAbortException which
causes the thread to terminate. - When a thread is in a stopped state and there are
no references to the thread object, the garbage
collector can remove the thread object from
memory. - A thread enteres the Blocked state when the
thread issues an input/output request. - A blocked thread cannot use a processor, even
when one is available.
5Thread States (Continued)
- There are three ways in which a Running thread
enters the WaitSleepJoin state. - If a thread encounters code that is cannot
execute yet (normally because a condition is not
satisfied), the thread can call Monitor method
Wait to enter the WaitSleepJoin state. Once in
this state, the thread returns to the Started
state when another thread invokes Monitor method
Pulse or PulseAll. Method Pulse moves the next
waiting thread back to the started state. - A running thread can call Thread method Sleep to
enter the WaitSleepJoin state for a period of
milliseconds specified as the argument to Sleep.
A sleeping thread returns to the Started state
when its designated sleep time expires. Any
thread that enters the WaitSleepJoin state by
calling Monitor method Wait or by calling Thread
method Sleep also leaves the WaitSleepJoin state
and returns to the Started state if the sleeping
or waiting threads interrupt method is called by
another thread in the program. - If a dependent thread cannot continue executing
unless another thread terminates, the dependent
thread calls the other threads Join method to
join the two threads. The dependent thread
leaves the WaitSleepJoin state when the other
thread finishes execution (i.e., enters the
Stopped state) - If a running threads Suspend method is called,
the running thread enters the suspend state. A
suspended thread returns to the started state
when another thread in the program invokes the
threads Resume method
6Life Cycle of a Thread
Unstarted
Start
Pulse PulseAll Interrupt Sleep interval expires
Started
Quantum expiration
Dispatch Assign a Processor
I/O Completion
Running
Issue I/O request
Wait Sleep, Join
complete
Suspend
Blocked
WaitSleepJoin
Suspended
Stopped
Resume
7Thread Priorities Thread Scheduling
- Every thread has a priority in the range between
ThreadPriority.Lowest to ThreadPriority.Highest.
The values come from the ThreadPriority
enumeration found in namespace System.Threading.
The enumeration consists of Lowest, BelowNormal,
Normal, AboveNormal, and Highest. By default,
each thread has a priority of Normal. - Timeslicing is when the operating system enables
thread of equal priority to share a processor. - With timeslicing, each thread recieves a brief
burst of processor time, called a quantum. - At the completion of the quantum, the processor
is taken away from the thread and given to the
next thread of equal priority, if one is
available. - The job of the operating system thread scheduler
is to keep the highest-priority thread running at
all times and, if there are more than one
highest-priority thread, to ensure that all such
threads execute for a quantum in round-robin
fashion.
8Thread Priority in Scheduling
- Multilevel Priority Queue
- A single processor computer
- A B execute for a quantum in round-robin
fashion until both complete execution - Next, thread C runs to completion
- Threads D,E,F run in round-robin fashion until
all are completed. - New high priority threads could indefinitely
postpone execution of lower priority threads,
This is called starvation. - Question What are the ways in which a thread
ceases execution?
Highest
A
B
C
Above Normal
Normal
Below Normal
E
D
F
Thread Tester
Lowest
G
9Thread Synchronization Class Monitor
- In many instances, multiple threads of execution
manipulate shared data. The threads accessing
data maybe modifying the data and this may cause
incorrect values. - Suppose the data is an array in which threads
could update separate parts concurrently. It is
likely that part of the data will reflect the
information from one thread while another part
of the data will reflect the information from a
different thread. - The problem of multiple concurrent access can be
solved by giving one thread at a time exclusive
access to code that manipulates the shared data.
Other threads needing to access the shared data
are placed in a wait state. This technique is
called mutual exclusion or thread
synchronization. - C uses .NET Framework monitors to perform
synchronization. - Class monitor provides the methods for locking
objects to implement synchronized access to
shared data.
10Thread Synchronization Class Monitor
- When a thread needs to acquire exclusive control
over an object, the thread invokes the Monitor
method Enter to acquire a lock on the object. - Each object has a SyncBlock that maintains the
state of the objects lock. - Methods of the class Monitor use the data in
SyncBlock to determine the state of the objects
lock. - After acquiring a lock, a thread can manipulate
an objects data. - When the thread that locked the shared object no
longer requires the lock, the thread invokes the
Monitor method Exit in order to release the
object. - C provides another means of manipulating an
objects lock using the keyword lock. - lock( objectReference )
-
- // code that requires synchronization goes
here -
- If a thread determines that it cannot perform its
task on a locked object, the thread can call
Monitor method Wait and pass as an argument the
object on which the thread will wait until the
thread can perform its task. - This discussion is best presented with examples
(Circular Buffer).
11Producer/Consumer Relationship without Thread
Synchronization
Write more data
Yes
Yes
Producer Finished Writing?
Empty Buffer?
Pulse
Wait
No
No
Producer
Consumer
Shared Data
Write
Read
No
Finished Reading?
Read More
Consumer Finished Reading?
Yes
Wait
Pulse
No
Yes
This program shows the Hazards of not using
Synchronized Threads
Wake up the Writer
This program demonstrates Thread
synchronization Between producer consumer
12(No Transcript)
13Circular Buffer Code Example
14Click on a button to view the source
Circular Buffer
Priority
Timeslice
Sleep
Deadlock
Starvation
Synchronized
Unsynchronized
15Assignment
- Â
- Write a simple multi-threading program that adds
and removes elements in an ArrayList. - Use a Producer button to create a producer thread
that adds elements to the the ArrayList. - The maximum number of elements that can be added
to the ArrayList is 100 and the producer thread
has to wait until the consumer thread(s)
remove(s) elements from the ArrayList. The
consumer thread is started by pressing a consume
button. As the consumer consumes, the producer
thread adds more elements to the ArrayList.