Multi ThreadingUpdated
Multi ThreadingUpdated
Presented By :-
Rajesh Kumar
Jain
Introduction
• Process and Thread are two basic units of Java program
execution.
• Process: A process is a program or application in execution.
• Thread: It can be called lightweight process
• Thread requires less resources to create and exists in the
process.
• Thread shares the process resources.
What is Thread?
A thread is a single sequential flow of control
within a program.
Thread does not have its own address space but
uses the memory and other resources of the
process in which it executes.
There may be several threads in one process
The Java Virtual Machine (JVM) manages these
and schedules them for execution.
What is Multithreading ?
Multithreading is a conceptual programming
paradigm where a program (process) is divided
into two or more subprograms (process), which
can be implemented at the same time in parallel.
For example, one subprogram can display an
animation on the screen while another may build
the next animation to be displayed.
A program that contains multiple flow of control
is known as multithreaded program.
Threads and Processes
CPU
main
run
GC
5
Cont..
Since threads in java are subprograms of a main
application program and share the same memory
space, they are known as lightweight threads or
lightweight processes.
‘threads running in parallel’ does not really mean
that they actually run at the same time.
Since all the threads are running on a single
processor, the flow of execution is shared between
the threads. The java interpreter handles the
switching of control between the threads in such a
way that it appears they are running
concurrently.
Cont..
Multithreading enables programmers to do multiple
things at one time.
For ex, we can send tasks such as printing into the
background and continue to perform some other
task in the foreground.
Threads are extensively used in java-enabled
browser such as HotJava. These browsers can
download a file to the local computer, display a web
page in the window, output another web page to a
printer and so on.
The ability of a language to support multithreads is
referred to as concurrency.
A Single Threaded Program
class ABC
{ Beginning
……….
...……..
Single-threaded
………. Body of
Execution
...……..
……….
End
...……..
}
A Multithreaded Program
Main Method
Module
switching switching
Process Based Multitasking Programming –
• In process based multitasking two or more processes and
programs can be run concurrently.
• In process based multitasking a process or a program is the
smallest unit.
• Program is a bigger unit than thread.
• Process based multitasking requires more overhead.
• Process requires its own address space.
• Process to Process communication is expensive.
• Here, it is unable to gain access over idle time of CPU.
• It is comparatively heavy weight.
• It has slower data rate multi-tasking.
Example – We can listen to music and browse internet at the same time.
The processes in this example are the music player and browser.
Thread Based Multitasking Programming –
• In thread based multitasking two or more threads can be run
concurrently.
• In thread based multitasking a thread is the smallest unit.
• Thread is a smaller unit.
• Thread based multitasking requires less overhead.
• Threads share same address space.
• Thread to Thread communication is not expensive.
• It allows taking gain access over idle time taken by CPU.
• It is comparatively light weight.
• It has faster data rate multi-tasking.
Examples – Using a browser we can navigate through the
webpage and at the same time download a file. In this
example, navigation is one thread and downloading is another
thread.
• In a word-processing application like MS Word, we can type
text in one thread and spell checker checks for mistakes in
another thread.
Thread Life Cycle
During the life time of a thread, there are many
states it can enter. They include:
1. Newborn state
2. Runnable State
3. Running State
4. Blocked State
5. Dead State
State diagram of a thread
New Newborn
Thread
start
stop
Killed
Thread
suspended resume stop
sleep(t)
wait notify
Idle Thread
(Not Runnable) Blocked
1. Newborn State
When we create a thread object, the thread is born
and is said to be in newborn state. The thread is not
yet scheduled for running. At this state, we can do
only one of the following with it:
Schedule it for running using start() method.
Kill it using stop() method
If scheduled, it moves to the runnable state. If we
attempt to use any other method at this stage, an
exception will be thrown.
Scheduling a Newborn Thread
Newborn
start stop
Runnable Dead
State
State
2. Runnable state (start())
The runnable state means that the thread is ready
for execution and is waiting for the availability of the
processor(CPU).
That is, the thread has joined the queue of threads
that are waiting for execution.
If all threads have equal priority, then they are given
time slots for execution in round robin fashion. i.e.
first-come, first-serve manner.
Cont..
The thread that relinquishes control joins the queue
at the end and again waits for its turn. This process
of assigning time to threads is known as time-
slicing.
If we want a thread to relinquish control to another
thread of equal priority before its turn comes, we
can do so by using the yield() method.
Releasing Control Using yield()
yield()
……...... ……......
suspended
resume
Runnable Sleeping
Running
3. Wait() and notify() methods :- blocked
until certain condition occurs
wait
notify
Thread(Runnable r)
Thread(Runnable r, String s)
Thread(String s)
synchronized method.
Thank You..