Course Title
JAVA PROGRAMMING
[Link]
1. Introduction 3. Create
Thread [Link]
Priorities
2. Thread Life
Cycle [Link]( ),
[Link]
join( )
Methods
[Link]
Thread : Introduction
A thread is a flow of execution.
Threads allows a program to operate more efficiently by doing
multiple things at the same time.
Threads can be used to perform complicated tasks in the
background without interrupting the main program.
A thread is a:
Facility to allow multiple activities within a single process
Referred as lightweight process
A thread is a series of executed statements
Each thread has its own program counter, stack and local
variables
A thread is a nested sequence of method calls
Its shares memory, files and per-process state
[Link]
Life cycle of a Thread (Thread States)
A thread can be in one of the five states. According to sun, there
is only 4 states in thread life cycle in java new, runnable, non-
runnable and terminated. There is no running state.
The life cycle of the thread in java is controlled by JVM.
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
[Link]
Life cycle of a Thread (Thread States)
[Link]
Thread States
1) New
The thread is in new state if you create an instance of Thread class but
before the invocation of start() method.
2) Runnable
The thread is in runnable state after invocation of start() method, but
the thread scheduler has not selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not
eligible to run.
5) Terminated
A thread is in terminated or dead state when its run() method exits.
[Link]
Create Thread
There are two ways to create a thread:
• Extending the [Link] class
• Implementing the [Link] Interface
Commonly used Constructors of Thread class:
Thread class provide constructors and methods to create and perform
operations on a thread. Thread class extends Object class and implements
Runnable interface.
• Thread()
• Thread(String name)
• Thread(Runnable r)
[Link] • Thread(Runnable r, String name)
Extends Thread : Example
Syntax : class Multi extends Thread
{
Class class_name extends Thread public void run()
{ {
public void run() [Link]("thread is running...");
{ // body of statements }
} public static void main(String ab[])
public static void main(String args[]) {
{ Multi t1=new Multi();
Classname object=new constructor(); [Link]();
[Link](); }
} }
}
Output:
[Link] thread is running...
Implementing Runnable interface : Example
class Multi3 implements Runnable
Syntax: {
Class class_name implements public void run()
Runnable_interface {
{ [Link]("thread is running...");
public void run() }
{ // body of statements public static void main(String ab[])
} {
public static void main(String args[]) Multi3 m1=new Multi3();
{ Thread t1 =new Thread(m1);
Classname object1=new constructor(); [Link]();
Thread threadobject=new Thread(object1); }
[Link](); }
}
} Output:
thread is running...
[Link]
The major difference is that when a class extends the
Thread class, it cannot extend any other class, but by
implementing the Runnable interface, it is possible to extend from
another class as well.
Syntax :
class MyClass extends OtherClass implements Runnable
A thread is in terminated or dead state when its _________
method exits.
Answer: C) run()
[Link]
Thread Methods
Public void start()
Starts the thread in a separate path of execution, then invokes the run()
method on this Thread object.
Public void run()
If this Thread object was instantiated using a separate Runnable target,
the run() method is invoked on that Runnable object.
Public final void setName(String name)
Changes the name of the Thread object. There is also a getName()
method for retrieving the name.
Public final void setPriority(int priority)
Sets the priority of this Thread object. The possible values are between
1 and 10.
[Link]
Thread Methods
public final void setDaemon(boolean on)
A parameter of true denotes this Thread as a daemon thread.
public final void join(long millisec)
The current thread invokes this method on a second thread, causing
the current thread to block until the second thread terminates or the specified
number of milliseconds passes.
public void interrupt()
Interrupts this thread, causing it to continue execution if it was
blocked for any reason.
public final boolean isAlive()
Returns true if the thread is alive, which is any time after the thread
has been started but before it runs to completion.
[Link]
Yield() Method
Condition is checked and when i==2 yield()
method is evoked taking control to
thread B
Causes the currently
running thread to yield
to any other threads of
the same priority that
are waiting to be
scheduled
[Link]
stop() Method
The stop() method kills the thread on execution
Condition is checked and when i==2
stop() method is evoked causing
termination of thread execution
[Link]
sleep() Method
Causes the currently running thread to block for at least the specified
number of milliseconds.
[Link]
suspend() and resume() method
A suspended thread can be revived by using the resume() method.
[Link]
Thread Priorities
Every Java thread has a priority that helps the operating system
determine the order in which threads are scheduled.
Java thread priorities are in the range between MIN_PRIORITY
(a constant of 1) and MAX_PRIORITY (a constant of 10).
By default, every thread is given priority NORM_PRIORITY (a
constant of 5).
[Link]
Thread Priorities : Example
[Link]
isAlive() and join() method
The [Link]() method tests if this thread is alive.
A thread is alive if it has been started and has not yet died.
public final boolean isAlive()
This method returns true if this thread is alive, false otherwise.
join() method waits for a thread to die.
It causes the currently thread to stop executing until the thread it
joins with completes its task.
[Link]
isAlive() and join() : Example
[Link]
__________ method waits for a thread to die.
Answer: A) join()
[Link]
summary
A thread is a flow of execution.
Threads allows a program to operate more efficiently by doing
multiple things at the same time.
In thread life cycle in java new, runnable, non-runnable and
terminated. There is no running state.
There are two ways to create a thread:
• Extending the [Link] class
• Implementing the [Link] Interface
Thread Methods are Yield(),stop(), sleep(),suspend(), resume(),
setPriority(), isAlive() and join().
[Link]
Quiz
Scan QR Code (or) Link
[Link]
[Link]