Provides information about Threads in Java. different ways of creating and running the thread and also provides the information about Life Cycle of the Thread
2. SSBN Degree College, ATP M Vishnuvardhan
Introduction
The ability of an Operating Systems to perform multiple jobs
concurrently is called as Multi-Tasking. Similarly in
programming, we divide the program in to multiple units and
execute them concurrently is called as Multi-Threading. Multi-
Threading improves the performance of the Program and makes
the programs to execute in a faster way
Java Supports Multi-Threading.
3. SSBN Degree College, ATP M Vishnuvardhan
Thread – Defined
A Thread is a piece of control which performs a job.
A thread is considered as light weight process since it
doesn't put burden on the CPU by demanding new
resources when a new thread is created instead it shares
the resources of its parent thread.
But a process always burden on the CPU by demanding
new resources when a new Process is created.
In order to deal with the threads java defined Thread
class in java.lang package
4. SSBN Degree College, ATP M Vishnuvardhan
Creation of Threads
In java Threads can be created in two ways
By extending Thread class
By implementing Runnable interface
class NumThread extends Thread
{
====
}
class NumThread implements Runnable
{
====
}
5. SSBN Degree College, ATP M Vishnuvardhan
Creation of Threads
Steps for creating thread by extending Thread class
1.Create a class by extending Thread class
2.Override run() method defined in Thread class. The run() is
considered as heart of the Thread since it holds the job of the Thread
3.Create an object for the newly created thread
4.Start the thread using start() method present in the Thread class
class NumThread extends Thread
{
public void run()
{
//job of the thread
}
}
NumThread T1=new NumThread();
T1.start();
6. SSBN Degree College, ATP M Vishnuvardhan
Creation of Threads
Steps for creating thread by implementing Runnable interface
1.Create a class by implementing Runnable interface
2.Override run() method declared in Runnable interface.
3.Create an object for the newly created class
4.Create a Thread class object by passing the reference of new
created object
5.Now start the thread using start() method
class NumThread implements Runnable
{
public void run()
{
//job of the thread
}
}
NumThread T1=new NumThread();
T1.start(); // gives error
Thread T2=new Thread(T1);
T2.start();
7. SSBN Degree College, ATP M Vishnuvardhan
Life Cycle of the Thread
start()
suspend()
sleep()
wait()
resume()
notify()
RunnableRunning
New Born
Blocked
Dead
yeild()
stop()
stop()
stop()
8. SSBN Degree College, ATP M Vishnuvardhan
Thread Methods
void start(): used to start a Thread
void stop(): used to stop a Thread
void run() : used to specify the job of the Thread
void sleep( int milliseconds): used to suspend the Thread for
a definite time
void suspend(): used to suspend a Thread for a indefinite
time until the Thread uses resume() method
void resume(): used to resume a suspended thread
void wait(): used to suspend a thread until other Thread
notifies it using notify()
void notify(): used to notify a waiting Thread.
9. SSBN Degree College, ATP M Vishnuvardhan
Thread Exceptions
IllegalThreadStateException: Occurs when a Thread is moved
to invalid state of thread.
InterruptedException: Occurs when a Thread is interrupted.
Generally sleep() generates this exception
10. SSBN Degree College, ATP M Vishnuvardhan
Thread Priority
Priority is an integer number which is associated with each
thread. Which specifies the priority of the Threads.
In general threads contains three priorities. These are defined
as constants in Thread class.
MIN_PRIRORITY --- 1
NORM_PRIRORITY --- 5
MAX_PRIRORITY --- 10
Priority for threads can given using following methods
int getPriority(): gets the priority of the thread
void setPriority(int priority): sets the priority for the thread
Eg: T1.setPriority(Thread.MAX_PRIORITY);
11. SSBN Degree College, ATP M Vishnuvardhan
Need of Synchronization
Account
accountNo
Name
Balance
getBalance()
Deposit()
withDraw()
Thread A Thread B
deposit(5000) withDraw(3000)
12. SSBN Degree College, ATP M Vishnuvardhan
Synchronization
It is common that two or more threads do the same job. This
doesn't cause any problem as long as the job doesn’t contain
critical section.
A section is said to be critical section when the statements
share common data.
When multiple threads work on critical section some times they
result in Data Integrity failure.
Synchronization is a technique of allowing only one thread in to
critical section at a time.
It can be applied in two levels
Method level Synchronization
Block level Synchronization
13. SSBN Degree College, ATP M Vishnuvardhan
Method level Synchronization
If all the statements inside a method are critical section
then we apply synchronization at method level. i.e, only one
thread is allowed in that method until that thread completes its
job no other thread is allowed to enter in to the method.
Syntax: synchronized returnType methodName ( <<parms>>)
{
======
}
Eg: synchronized double getBalance()
{
return balance;
}
14. SSBN Degree College, ATP M Vishnuvardhan
Block level Synchronization
If only few statements inside a method are critical section but
not all the statements then we apply synchronization at block level. i.e,
only one thread is allowed in that block until that thread completes its
job no other thread is allowed to enter in to that block.
Syntax:
returnType methodName ( <<parms>>)
{
=====
synchronized(this)
{
//critical section
=====
}
======
}
Eg: double getBalance()
{
== ====
synchronized(this)
{ return balance; }
}