0% found this document useful (0 votes)
16 views8 pages

28 June 2022

This document discusses different ways to create and manage threads in Java. It covers implementing the Runnable interface versus extending the Thread class to create threads. It also summarizes the life cycle of a thread and explains methods like start(), run(), join(), getName(), setName(), and setPriority(). Finally, it distinguishes between single-threaded and multi-threaded applications.

Uploaded by

sandesh ahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

28 June 2022

This document discusses different ways to create and manage threads in Java. It covers implementing the Runnable interface versus extending the Thread class to create threads. It also summarizes the life cycle of a thread and explains methods like start(), run(), join(), getName(), setName(), and setPriority(). Finally, it distinguishes between single-threaded and multi-threaded applications.

Uploaded by

sandesh ahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Creating Thread by implementing Runnable interface

public class ClassA implements Runnable


{
public void run() t1.start(); { t1.run(); for(int i=0;i<5;i++)
t2.start(); System.out.println("Run method"); t2.run(); }
public static void main(String[] args)
{
ClassA a=new ClassA();
Thread t1=new Thread(a);
Thread t2=new Thread();
System.out.println("Java is awesome");
}
}
t1.start()
New Thread will be generated which is responsible for the
execution of ClassA run() method.
t1.run()
No new Thread will be generated but ClassA run() method will
be called just like a normal method call.
t2.start()
A new Thread will be generated which is responsible for the
implementation of Thread class run()method
t2.run()
No new Thread will be generated but Thread class run()
method will be called just like a normal method call.
Creating Thread by extending Thread class
public class ClassA extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
System.out.println("Run method");
}
public static void main(String[] args)
{
ClassA a=new ClassA();
a.start();
System.out.println("Java is awesome");
}
}
Life Cycle of a Thread
New Thread is created but not yet started.

Runnable A thread in the Runnable state is executing in the Java


virtual machine but it may be waiting for other resources
from the operating system such as processor
Blocked A thread in the blocked state is waiting to enter a
synchronized block/method or reenter a synchronized
block/method.
Waiting A thread will be in waiting state for a unspecified period
of time, due to calling one of the methods like
wait(),join() etc
Timed_waiting A thread will be in waiting state for another thread for
a specified waiting time is in this state
Terminated The thread has completed execution

A thread can be in only one state at a given point in time. Thread.getState()

Types of Thread Application ⚫ In


general there are two type of thread applications 1.
Single Threaded Application
2. Multi Threaded Application
Single Threaded Application:
⚫ When we invoke java application, JVM by default creates a
thread is called “main thread”.
⚫ In single threaded application, execution starts at main thread
and end at the same thread.
⚫ All the methods of single thread executes sequentially.
Multi threaded Application:
⚫ Creating a user thread from main thread referred as multi
threaded application.
⚫ Multi threaded application execution starts at main thread
only.
⚫ Program execution completes, when all the running
threads moved to dead state.
Understanding join() method
⚫ The join method allows the current executing thread to
wait for the completion of another thread.
⚫ Every join() method throws InterruptedException, hence
compulsory we should handle either by try catch or by
throws keyword. Otherwise we will get compile time
error.
Getting and setting name of a Thread:
⚫ Every Thread in java has some name it may be provided
explicitly by the programmer or automatically generated
by JVM.
⚫ Thread class defines the following methods to get and
set name of a Thread.

✓ public final String getName()

✓ public final void setName(String name)


Understanding Thread Priorities
⚫ In the Java programming language, every thread has a
priority.
⚫ We can increase or decrease the priority of any thread by
using setPriority(int newPriority) method.
⚫ We can get the priority of the thread by using
getPriority() method
⚫ Priority can either be given by JVM (5) while creating the
thread or it can be given by programmer explicitly. ⚫
Accepted value of priority for a thread is in range of 1 to 10.
⚫ Thread priorities are highly system-dependent we should
always keep in mind that underlying platform should
provide support for scheduling based on thread priority.
⚫ There are 3 static variables defined in Thread class

for priority.

public static int MIN_PRIORITY --->1

public static int NORM_PRIORITY --->5

public static int MAX_PRIORITY --->10

You might also like