Java Multi Threading
Java Multi Threading
main
run
GC
Advantages of multithreading
1. Reduces the computation time.
2. Improves performance of an
application.
3. Threads distribute the same address
space so it saves the memory.
4. Context switching between threads is
usually less costly than between
processes.
5. Cost of communication between
threads is comparatively low.
Applications
When we execute an application:
1. The JVM creates a Thread object whose
task is defined by the main() method
2. The JVM starts the thread
3. The thread executes the statements of
the program one by one
4. After executing all the statements, the
method returns and the thread dies
A single threaded program
class ABC
{
….
public void main(..) begin
{
… body
..
end
}
} 7
A Multithreaded Program
Main Thread
start
start start
PC client
Internet
Server
Local Area Network
PDA
Threading Mechanisms...
1. Create a class that extends the
Thread class
2. Create a class that implements the
In Runnable
both cases the interface
run() method should be implemented
10
1st method: Extending
Thread class
Threads are implemented as objects that
contains a method called run()
class MyThread extends Thread
{
public void run()
{
// thread body of
execution
}
}
Create a thread:
MyThread thr1 = new MyThread();
Start Execution of threads:
class MyThread extends Thread {
public void run() {
System.out.println(" this thread is running ... ");
}
}
class ThreadEx1 {
public static void main(String [] args ) {
MyThread t = new MyThread();
t.start();
}
}
2nd method:
Threads by implementing Runnable interface
class MyThread implements Runnable
{
public void run()
{
// thread body of execution
}
}
Creating Object:
MyThread myObject = new MyThread();
Creating Thread Object:
Thread thr1 = new
Thread( myObject );
Start Execution:
A Runnable Object
When running the Runnable object, a
Thread object is created from the Runnable
object
The Thread object’s run() method calls
the Runnable object’s run() method
Allows threads to run inside any object,
regardless of inheritance
An example
class MyThread implements Runnable {
public void run() {
System.out.println(" this thread is
running ... ");
}
}
class ThreadEx2 {
public static void main(String [] args ) {
MyThread o=new MyThread();
Thread t = new Thread(o);
t.start();
}
A Program with Three Java
Threads
Write a program that creates 3 threads
run.
void sleep(int m) or sleep(int m,
int n)
The thread sleeps for m
Life Cycle of Thread
During Life time of a thread there are many
states it can enter.
1. Newborn state
2. Runnable state
3. Running state
4. Blocked state
5. Dead state
A thread is always in one of these five states.
It can move from one state to another via a
variety of ways.
Thread State Diagram
Newborn
new ThreadExample(); Thread
stop(
thread.start(); )
wait() resume()
sleep() notify() stop(
Suspend() )
stop();
start();
Runnable Threads
Running Thread
Running State
Running means that processor has given its time
to the thread for its execution.
The thread runs until it relinquishes control on
its own or it is interrupted by higher priority
thread.
The running thread may relinquish its control in
one of the following situations.
1. It has been suspended using suspend()
method.
2. It has been made to sleep
3. It has been told to wait until some event occurs.
1. It has been suspended using suspend() method.
suspend()
resume()
after(t)
notify
Runnable Waiting
Running
Blocked State
A thread is blocked when it is prevented from
entering into the runnable and running state.
This happens when thread is suspended,
sleeping, or waiting in order to satisfy certain
requirements.
A blocked thread is “not runnable” but not
dead and therefor fully qualified to run again.
Dead State
Every thread has life cycle. A thread ends its
life when it has completed its execution. It is
natural death.
However we can kill it by sending the stop
Demo
ThreadMethods
Scheduling Threads
start()
Ready queue
Newly created
threads
Currently executed
thread
I/O operation completes