Professional Documents
Culture Documents
Multi-Threaded Programming
Terminologies
Process: A program under execution is called as process.
Thread: A smallest component of a process that can be executed independently.
OR
A thread is an independent path of execution within a single program.
Multi-Threaded Programming: A process with two or more threads is being
executed concurrently.
Process based multi-tasking: It allows executing two or more processes
.in
concurrently. Example: Browsing the internet, editing PowerPoint presentation
concurrently.
Thread based multi-tasking: It allows executing two or more threads
concurrently. Example: Assume a program will be having 3 methods namely A(),
C() concurrently.
Difference between Process and Thread
ac
B(), C(). Say thread T1, executes A(), thread T2 executes B(), Thread T3 executes
tu
Process Thread
.v
Each process has unique address space Threads shares same address space
w
Java cannot handle process based multi- Java can handle thread based multi-
tasking tasking
Thread Priorities:
o It is an integer number assigned to threads.
o A priority indicates how one thread should be treated over other threads.
o Thread priority is used to decide when to switch from one running thread
to other.
o A method of switching from one running thread or process to other thread
or process respectively is known as context switching.
When context switching happens
o A thread can voluntarily relinquish control i.e. running thread releases
the CPU to run highest priority thread voluntarily or A thread can be pre-
empted by a higher priority thread.
Main Thread
o When Java program starts execution, one thread begins immediately, that
thread is termed as main thread.
o Main thread is required for two reasons, namely
.in
This thread spawns child threads
Performs various shutdown activities before completing execution.
o Main thread is a last thread to complete execution.
Thread Life Cycle
ac
There are five states a thread can possess in its life time, namely
tu
1. New: Thread is in new state, when an instance of thread class is created and
before calling start( ) method.
2. Runnable: A thread is in runnable state after invocation of start() method,
.v
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
w
it.
4. Non-Runnable (Blocked): This is the state when the thread is still alive,
but is currently not eligible to run.
w
Constructors
w
Thread( )
w
Thread(String Name)
Thread(Runnable R)
w
Functions Description
Calls run( ) method to begin thread
public void start( )
execution
Contains code that is to be executed by
public void run( )
thread
Sends currently executing thread to
public void sleep( )
sleep mode
public void sleep(long milliSecs) Sends currently executing thread to
sleep mode for specified amount of time
public int getPriority( ) Returns priority of the Thread
public void setPriority(int priority) Changes priority of current thread
public String getName( ) Returns name of current thread
Returns reference of the currently
public Thread currentThread( )
executing thread
public int getId( ) Returns ID of the thread
Returns true, if thread is alive
public boolean isAlive( ) Returns false, if thread has completed
execution
public void join( ) Waits for thread to die
Waits for thread to die for specified milli
public void join(long ms)
seconds
.in
How to create Thread in Java?
There are two ways to create threads in Java, namely
1. By extending Thread class
2. By implementing Runnable interface
Creating Thread By extending Thread class
ac
tu
Follow below steps to create thread by extending Thread class,
1. Extend Sub class by Thread class
.v
/* ThreadDemo.java */
class thrExample extends Thread{
w
.in
super(thName);
}
public void run(){
ac
System.out.println("---- New Thread ----");
System.out.println("Current Thread : " +getName());
System.out.println("Thread Priority: " +getPriority());
tu
System.out.println("Thread Status : " +isAlive());
System.out.println("Thread Id : " +getId());
.v
}
}
w
.in
1. Create sub class by implementing Runnable interface
2. Override run( ) method inside the subclass
3. Create Thread object by calling Thread constructor and pass object of sub
ac
class as parameter for which Thread should be created
4. Call start( ) method by Thread object
Below program demonstrates creation of thread by implementing
tu
Runnable interface.
/* ThreadRunn.java */
.v
{
System.out.println("Thread is executing using runnable
w
interface");
}
w
/* thExample.java */
.in
thExample(String thName){
t = new Thread(this, thName);
System.out.println("New Thread ----" +t.getName() +"----
is created");
}
t.start(); ac
tu
public void run(){
System.out.println("Thread --- " +t.getName() +" ---is
.v
executing");
}
w
}
public class threadRunMul {
w
new thExample("TWO");
}
}
Output:
New Thread ----ONE---- is created
New Thread ----TWO---- is created
Thread --- TWO ---is executing
Thread --- ONE ---is executing
isAlive( ) and join( )
1. Function belongs to : Thread class
Function Name: isAlive( )
Task: Checks whether the thread has finished execution
Prototype: final boolean isAlive( )
Return value: true, if thread upon which it is called is still running
false, if thread upon which it is called is completed execution
2. Function belongs to : Thread class
Function Name: join( )
Task: waits until the thread on which it is called terminates
Prototype: final void join( ) or final void join(long milliSecs)
.in
Return value: No return value
Below program demonstrates above two functions
}
public void run(){
w
try{
for(int i = 1; i <= 2; i++){
w
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println(t.getName() +" is Interrupted");
}
System.out.println(t.getName() +" is exiting");
}
}
public class thMulRun {
public static void main(String [] args){
thRun t1 = new thRun("ONE");
thRun t2 = new thRun("TWO");
System.out.println("Main Thread Started");
System.out.println("Thread ONE is alive "
+t1.t.isAlive());
System.out.println("Thread ONE is alive "
+t2.t.isAlive());
try{
System.out.println("Waiting for Child threads to
finish");
t1.t.join();
.in
t2.t.join();
}catch(InterruptedException e){
System.out.println("Main thread Interrupted");
}
ac
System.out.println("Main Thread Completed");
}
tu
}
Output:
.v
Thread TWO 1
Thread ONE 1
w
Thread TWO 2
Thread ONE 2
TWO is exiting
ONE is exiting
Main Thread Completed
Thread Priorities
Thread priorities are used by scheduler to decide when each thread should be
allowed to run.
Functions Associated with Thread Priorities
1. Function belongs to : Thread class
Function Name: setPriority( )
Task: Changes priority of current thread
Prototype: public void setPriority( )
Return value: No return value
2. Function belongs to : Thread class
Function Name: getName( )
.in
Task: Fetches the name of current thread on which it is called
Prototype: public String getName( )
Return value: Returns name of the thread
3. Function belongs to : Thread class
Function Name: setName( )
ac
tu
Task: Sets name to thread on which it is called
Prototype: public void setName(String Name)
.v
.in
t2.start();
}
}
Output: ac
Current Thread : Thread[main,5,main]
tu
---- New Thread ----
Current Thread : TWO
.v
Thread Priority: 5
Thread Status : true
w
Thread Id : 9
---- New Thread ----
w
.in
To synchronize the function, prefix the keyword synchronized in function
definition
Example: In the below program, thread executes the function “disp( )”, program
without synchronization.
Synchronized Methods ac
/* ThreadSyncOne.java (Without Synchronization)*/
tu
class First{
void disp(){
.v
System.out.println("Good");
try {
w
Thread.sleep(500);
} catch (InterruptedException ex) {
System.out.println("Thread Interrupted");
w
}
System.out.println("Morning");
w
}
}
class Two extends Thread{
First f = new First();
Two(First f1){
f = f1;
}
public void run(){
f.disp();
}
}
public class ThreadSyncOne {
public static void main(String [] args){
First f = new First();
Two t1 = new Two(f);
Two t2 = new Two(f);
t1.start();
t2.start();
}
}
Output:
Good
Good
.in
Morning
Morning
ac
Above Program using synchronized keyword
/* ThreadSyncOne.java (Using Synchronization)*/
class First{
tu
void disp(){
System.out.println("Good");
.v
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
w
System.out.println("Thread Interrupted");
}
w
System.out.println("Morning");
}
w
}
class Two extends Thread{
First f = new First();
Two(First f1){
f = f1;
}
public void run(){
f.disp();
}
}
public class ThreadSyncOne {
public static void main(String [] args){
First f = new First();
Two t1 = new Two(f);
Two t2 = new Two(f);
t1.start();
t2.start();
}
}
Output:
Good
Morning
.in
Good
Morning
Synchronized Blocks
ac
Synchronized blocks are required when,
o Synchronize access to objects of a class that was not designed for multi-
threading.
o Class does not use synchronized methods.
tu
During above conditions, synchronized blocks can be used.
synchronized(Object){
.v
//Statements to be synchronized
}
Where, Object is a reference to the object being referenced.
w
class Callme{
void disp(){
w
System.out.println("Good");
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
System.out.println("Thread Interrupted");
}
System.out.println("Morning");
}
}
.in
Caller c2 = new Caller(cmObj);
}
}
Output:
Good
Morning
ac
tu
Good
Morning
.v
o wait( )
o notify( )
w
o notifyAll( )
wait( )
The wait method releases the lock of current thread and moves it to waiting
state. Thread will awake, when other thread calls notify( ) or notifyAll( )
method for the object, or till specified time elapses.
Prototype:
1. public final void wait( ) throws InterruptedException
2. public final void wait(long timeout) throws InterruptedException
notify( )
Notifies the thread in waiting state on the same object to wake up.
Prototype:
1. public final void notify( )
notifyAll( )
Notifies all threads in waiting state on the same object to wake up.
Prototype:
1. public final void notifyAll( )
Process of Inter Thread Communication
.in
ac
tu
.v
3. Now thread goes to waiting state if you call wait() method on the object.
Otherwise it releases the lock and exits.
w
4. If you call notify() or notifyAll() method, thread moves to the notified state
(runnable state).
5. Now thread is available to acquire lock.
6. After completion of the task, thread releases the lock and exits the monitor
state of the object.
Difference between wait( ) and sleep( )
wait( ) sleep( )
sleep( ) method does not releases the
wait( ) method releases the lock
lock
Method of object class Method of Thread class
Non-static method Static method
Should be notified by notify( ) or
wakes up after specified time
notifyAll( )
Below program demonstrates the withdrawal and depositing money using inter
thread communication.
Scenario:
Available balance in the account is 5000.
.in
Say, Thread T1, tries to withdraw an amount of 10000. Since, balance is less,
thread cannot withdraw. It calls wait( ) and goes to waiting state until
another thread deposits money and wakes up this thread.
method and wakes up thread T1.
ac
Say, Thread T2, deposits 10000, now balance is 15000. Now, T2 calls notify( )
System.out.println("About to withdraw");
w
wait();
}catch(Exception e){
System.out.println("Waiting to deposit, but
done");
}
amount = amount - withAmount;
System.out.println("Withdrawl of " +withAmount +"
completed");
}
}
synchronized void deposit(int depAmount){
System.out.println("Depositing Amount");
amount = amount + depAmount;
System.out.println("Depositing " +depAmount);
notify();
}
}
.in
}
}.start();
new Thread(){
public void run(){
}
c.deposit(10000);
}.start();
ac
tu
}
}
.v
The producer’s job is to generate data, put it into the buffer, and start again.
At the same time, the consumer is consuming the data (i.e. removing it from
w
.in
// Create producer thread
Thread t1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
ac
tu
pc.produce();
}
catch(InterruptedException e)
.v
{
e.printStackTrace();
}
w
}
});
w
@Override
public void run()
{
try
{
pc.consume();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Start both threads
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
}
.in
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
{
int value = 0;
while (true)
ac
public void produce() throws InterruptedException
tu
{
synchronized (this)
{
.v
wait();
System.out.println("Producer produced-"
w
+ value);
w
.in
System.out.println("Consumer consumed-"
+ val);
}
}
Output:
w
Producer produced-0
Producer produced-1
w
Consumer consumed-0
Consumer consumed-1
w
Producer produced-2
Consumer consumed-2