By Asst. Professor Dept. of Computer Application ST - Joseph's College (Arts & Science) Kovur, Chennai
By Asst. Professor Dept. of Computer Application ST - Joseph's College (Arts & Science) Kovur, Chennai
VINOTH R
Asst. professor
Dept. of Computer Application
St.Joseph’s college(Arts & Science)
Kovur, Chennai
TOPIC INCLUDES:
Introduction to Thread
Creation of Thread
Life cycle of Thread
Stopping and Blocking a Thread
Using Thread Methods
Thread Priority
Thread Synchronization
DeadLock
INTRODUCTION TO THREAD
• Process and Thread are two basic units of Java
program execution.
• Process: A process is a self contained execution
environment and it can be seen as a program or
application.
• Thread: It can be called lightweight process
• Thread requires less resources to create and exists in the
process
• Thread shares the process resources
INTRODUCTION Contd.
MULTITHREADING
• Multithreading in java is a process of
executing multiple processes simultaneously
• A program is divided into two or more
subprograms, which can be implemented at
the same time in parallel.
• Multiprocessing and multithreading, both are
used to achieve multitasking.
• Java Multithreading is mostly used in games,
animation etc.
MULTITHREADING Contd.
MULTITHREADING Contd.
ADVANTAGE:
It doesn't block the user
can perform many operations together so it
saves time.
Threads are independent so it doesn't
affect other threads
CREATING THREAD
• Threads are implemented in the form of objects.
• The run() and start() are two inbuilt methods
which helps to thread implementation
• The run() method is the heart and soul of any
thread
– It makes up the entire body of a thread
• The run() method can be initiating with the help
of start() method.
CREATING THREAD Contd.
CREATING THREAD
1. By extending Thread class
2. By implementing Runnable
interface
CREATING THREAD Contd.
1. By Extending Thread class
class Multi extends Thread // Extending thread class
{
public void run() // run() method declared
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi t1=new Multi(); //object initiated
t1.start(); // run() method called through start()
}
}
void run() This method is the entry point of the thread. Execution of
thread starts from this method.
This method suspend the thread for mentioned time
void sleep(int sleeptime) duration in argument (sleeptime in ms)
ThreadName.setPriority(int Number);
Thread priority contd.
• The intNumber is an integer value to which the
thread's priority is set. The Thread class defines
several priority constants:
1. public static int MIN_PRIORITY =1
2. public static int NORM_PRIORITY = 5
3. public static int MAX_PRIORITY = 10
• The default setting is NORM_PRIORITY. Most user-
level processes should use NORM_PRIORITY.
Java synchronization
• Generally threads use their own data and
methods provided inside their run() methods.
• But if we wish to use data and methods outside
the thread’s run() method, they may compete
for the same resources and may lead to serious
problems.
• Java enables us to overcome this problem using
a technique known as Synchronization.
For ex.: One thread may try to read a record from
a file while another is still writing to the same file.
Java synchronization contd.
• When the method declared as synchronized,
Java creates a "monitor" and hands it over to
the thread that calls the method first time.
synchronized (lock-object)
{
.......... // code here is synchronized
}
deadlock
• Deadlock describes a situation where two or
more threads are blocked forever, waiting for
each other.
• when two or more threads are waiting to gain
control on a resource.
For example, assume that the thread A must
access Method1 before it can release Method2, but
the thread B cannot release Method1 until it gets
holds of Method2.
deadlock
THANK YOU…