UNIT3 Part2 (Multithreading)
UNIT3 Part2 (Multithreading)
Unit-3
Basics
• Process
– A program that is executing.
• Thread
– Similar to a program that has single flow of control.
– It has beginning, body, and end.
• Multitasking
– Executing several programs simultaneously.
• Multithreading
– Dividing program into 2 or more subprograms, which can be
implemented at same time(in parallel).
• Process based multitasking
– Allowing computer to run 2 or more programs concurrently.
• Thread based multitasking
– Single program can perform 2 or more tasks simultaneously.
Process
• A program that is executing.
• Process based multitasking is a feature that
allows your computer to run 2 or more programs
concurrently.
• Processes are heavyweight tasks that require
their own separate address spaces.
• Disadvantages:
– Inter rocess communication is expensive and limited.
– Context switching from one process to another is
costly.
Thread
• It is a small unit.
• Similar to a program that has single flow of control.
• It has beginning, body, and end.
• Threads are lightweight.
• They share the same address space and cooperatively
share the same heavy weight process.
• Inter thread communication is inexpensive and context
switching from one thread to other is low cost.
• Multithreading enables you to write very efficient
programs that make maximum use of the CPU.
Thread
Drawback: event
loop with polling
5 States:
- Newborn
- Runnable
- Running
- Blocked
- Dead
Features of Multithreading
• Thread Priorities
• Synchronization
• Messaging
Thread Priorities
• Integers (least number means min priority)
• Determines how thread should be treated
w.r.t other threads.
• It is used to decide when to switch from one
running thread to the next.
• Threads with same priority will follow round
robin fashion.
Synchronization
• Used to overcome asynchronous behaviour.
• Monitor:
– It is a inter process synchronization mechanism.
– Once a thread is inside a sync. Method,
no other thread can call any other
sync. Method on the same object.
Messaging
• Java provides a clean, low cost way for 2 or
more threads to talk to each other, via calls to
predefined methods that all objects have.
• Java’s messaging system allows a thread to
enter a sync. Method on an object, and then
wait there until some other thread explicitly
notifies it to come out.
Creating Thread
• There are 2 ways. Using –
– Thread class ( from java.lang package)
– Runnable interface ( from java.lang package)
Using Thread class
• Methods in Thread class:
– Run(): entry point for the thread
– Start(): to start any thread ( it calls run method in it)
– Stop(): to stop the thread
– Sleep(): to block the thread at specified time
– Suspend(): block the thread until further orders
– Resume(): to invoke suspended threads
– Wait(): block the thread until certain condition occurs
– Notify(): to invoke wait threads
– getName(): to obtain threads name
– getPriority(): to obtain threads priority
– isAlive(): to determine thread is alive or not
Using Thread class
• Note: The main thread is a thread that all of the
other threads have been using.
• Steps:
– Import java.lang package
– Declare a class extending Thread class
– Override run() method that is responsible for
executing the sequence of code that the thread will
execute.
– Create a thread object and call the start() method to
initiate the thread execution.
Using Runnable Interface
• Runnable interface defines only one method
called run().
• Run() method can call other methods, use
other classes, and declare variables just like
main thread.
• Start() method causes the JVM to call run()
method.
• Example program.
Thread Priorities
• Thread Priorities are used by the thread scheduler to
decide when each thread should be allowed to run.
• To set a thread priority, use setPriority() method.
– Syntax: void setPriority(int level);
• Instead of level value, we can use properties :
MIN_PRIORITY(1), NORM_PRIORITY(5) and
MAX_PRIPORITY(10).
• To obtain the current priority, use getPriority()
method.
– Syntax: int getPriority();
• Example program