Multi-Threading
Task: Work
Single Tasking: Performing only one task at a time is called as Single Tasking
Ex:
1) Explain the topic
2) Dictate the notes
3) Ask questions
-> If we perform single tasking then it will take lot of time to complete all our work.
Multi-Tasking: Performing multiple tasks at a time is called as Multi-Tasking
Ex:
1) Walking & listening music
2) Speaking and Writing
3) Reading book & eating
-> If we perform multi-tasking then we complete multiple works at a time.
-> Multi-Tasking we can achieve in 2 ways
1) Process Based Multi-Tasking
Ex: Windows OS
2) Thread Based Multi-Tasking
-> To execute our program logics parallels then we need to go for Thread Based
Multi-Tasking
-> Using Thread Based Multi-Tasking our program can complete the work quickly
-> To implement Thread Based Multi-Tasking we will use Multi-Threading
-> Java Supports Multi-Threading
Use case to go for Multi-Threading
1) Send sms to all customers at a time
2) Send Email to all customers at a time
3) Generate & Send Bank Statements to all customers in email
Note: The main aim of Multi-Tasking is used execute our program logic parallels so
that we can complete more work in less time.
-> For Every Java program execution, JVM will create one thread by default. That
thread is called as Main thread.
// Java Program to get the details of Main thread
public class Demo {
public static void main(String[] args) {
Thread currentThread = [Link]();
[Link]([Link]());//main
Note: Thread is a predefined class available in [Link] package. In Thread class we
have a static method currentThread ( ).
User Defined Threads
-> In Java we can create Thread in 2 ways
1) By extending Thread class
2) By Implementing Runnable interface
// Java program to create user defined thread using Thread class
package azinit;
public class Demo1 extends Thread{
public void run() {
[Link]("run() method called...");
public static void main(String[] args) {
Demo1 d = new Demo1();
Thread t = new Thread(d);
[Link]();
}
// Java program to create the thread using Runnable interface
package azinit;
public class Demo2 implements Runnable {
public void run() {
[Link]("run() method called...");
public static void main(String[] args) {
Demo2 d = new Demo2();
Thread t = new Thread(d);
[Link]();
}
Q) What is the difference between extending Thread class and implementing
Runnable interface, which is recommended?
-> If we extend properties from Thread class then we can't extend properties from
any other class because java doesn't support multiple inheritances. (We are closing
gate for Inheritance)
-> If we implement Runnable interface then in future we can extend properties from
any class based on requirement. (Our gate is open for inheritance)
Note: Implementing Runnable interface is always recommended.
What is Thread Scheduler
-> Thread Scheduler is a program in the JVM which is responsible to schedule Threads
execution and resources allocation required for the thread.
-> When we call start ( ) method then Thread Scheduler will start its operation.
1) Allocating Resources
2) Thread Scheduling
3) Thread Execution by calling run ( ) method
start( ) method vs run( ) method
-> To start thread execution we will call start ( ) method
[Link] ( )
->once start ( ) method is called then Thread Scheduler will come into picture to
execute our thread
->start ( ) method will call run ( ) method internally
->inside run ( ) method we will write the logic which should be executed by the
thread.
Can we call run( ) method directly without calling start( ) method
-> Yes, we can call run ( ) method directly but it will execute like a normal method
(there is no use) by "main" thread.
-> If we want to execute run ( ) method as a thread method then we should call start
( ) method then internally it will call run ( ) method (Thread Scheduler will take care
of thread execution)
package azinit;
public class Demo3 implements Runnable {
@Override
public void run() {
[Link]("run() method started...");
Thread t = [Link]();
[Link]([Link]());
[Link]("run() method ended...");
public static void main(String[] args) {
Demo3 d = new Demo3();
Thread t = new Thread(d);
//[Link]();
[Link]();
}
=> If we call start ( ) method then run ( ) method will be executed by our user defined
thread (we can see thread name as Thread-0)
=>if we call run ( ) method then run ( ) method will be executed by "main" thread (we
can see thread name as main)
What is Thread Life Cycle
-> Thread Life cycle contains several phases of Thread execution
1) New
2) Runnable
3) Running
4) Blocked
5) Terminated
New: A thread begins its life cycle in the new state. Thread remains in the new state
until we will call start( ) method.
Runnable: After calling start ( ) method, thread comes from new state to runnable
state.
Running: A thread comes to running state when Thread Scheduler will pick up that
thread for execution.
Blocked: A thread is in waiting state if it waits for another thread to complete its task.
Terminated: A thread enters into terminated state once it completes its task.
// Java Program on Thread Sleep
package azinit;
public class Demo4 implements Runnable {
@Override
public void run() {
[Link]("run() method started...");
try {
[Link](5000);//blocked state
}catch (InterruptedException e) {
[Link]();
[Link]("run() method ended...");
public static void main(String[] args) {
Demo4 d = new Demo4();
Thread t = new Thread(d);
[Link]();
}
// Java program to start multiple threads to perform same activity
package azinit;
public class Demo5 implements Runnable {
@Override
public void run() {
[Link]("run() method started..."
+[Link]().getName());
try {
[Link](15000);//blocked state
}catch (InterruptedException e) {
[Link]();
[Link]("run() method ended..."
+[Link]().getName());
public static void main(String[] args) {
Demo5 d = new Demo5();
Thread t1 = new Thread(d);
[Link](Thread.MAX_PRIORITY);//10
[Link]("Thread-1");
Thread t2 = new Thread(d);
[Link](Thread.NORM_PRIORITY);//5
[Link]("Thread-2");
Thread t3 = new Thread(d);
[Link](Thread.MIN_PRIORITY);//1
[Link]("Thread-3");
[Link]();//runnable state
[Link]();//runnable state
[Link]();//runnable state
}
Note: We shouldn't start one thread more than one time.
package azinit;
public class Demo2 implements Runnable {
public void run() {
[Link]("run() method called...");
public static void main(String[] args) {
Demo2 d = new Demo2();
Thread t = new Thread(d);
[Link]();
[Link]();//[Link]
Callable Interface
-> This interface introduced in java 1.5
->using Callable interface also we can create the Thread
-> This interface contains call ( ) method.
Syntax: public Object call ( )
What is the difference between Runnable & Callable interfaces
-> Runnable is a functional interface which contains run ( ) method
-> Callable is a functional interface which contains call ( ) method
-> Runnable run ( ) method returns void (no return type)
-> Callable call ( ) method returns Object
-> Runnable interface present in [Link] package
-> Callable interface present in [Link] package
ExecutorService
-> Executor Service introduced in java 1.5v
-> Using ExecutorService we can implement multi-threading
-> Using Executors we can create thread pool
-> Using ExecutorService we can submit tasks to pool of threads.
-> ExecutorService will re-use threads available in the pool to complete all submitted
tasks.
// Java Program on Executor Service with Callable interface
package azinit;
import [Link];
import [Link];
import [Link];
import [Link];
public class Demo6 implements Callable {
@Override
public Object call() throws Exception {
[Link]("call() - method executed...");
return "success";
public static void main(String[] args) throws Exception {
Demo6 d = new Demo6();
ExecutorService exService = [Link](10);
for(int i=1; i<=15; i++) {
Future submit = [Link](d);
[Link]([Link]().toString());
[Link]();
Daemon Thread
We have 3 types of threads in java
1) Default thread created by JVM ( main thread )
2) User Defined Threads ( Thread class, Runnable interface, Callable interface )
3) Daemon Threads
Note: The thread which runs in the background is called as Daemon Thread. Daemon
Threads also called as low priority threads.
Ex: Garbage Collector is a daemon thread
-> We can make our thread as Daemon Thread using setDaemon( ) method
// Java Program to Make thread as Daemon
package azinit;
public class Demo7 implements Runnable {
@Override
public void run() {
if([Link]().isDaemon()) {
[Link]("Daemon Thread Executed...");
}else {
[Link]("Normal Thread Executed...");
public static void main(String[] args) {
Demo7 d = new Demo7();
Thread t = new Thread(d);
[Link](true);
[Link]();
-> When JVM reaches end of main method, it will terminate our program. If JVM
founds Daemon thread running it terminates that daemon thread and then it will
shut down the program.
-> JVM will not care about Daemon Threads running status to stop the program
execution.
Synchronization
String ----> Immutable class
StringBuffer ----> Mutable class & synchronized class (Thread safe class)
StringBuilder ---> Mutable class & not-synchronized class (Not Thread Safe class)
Synchronized means Thread safe ===> only one thread can access the object /
resource at a time
Not-Synchronized means Not Thread Safe ===> Multiple threads can access same
resource / object at a time
package azinit;
public class MovieTicketBooking {
int avilableTickets = 100;
public void run() {
if(avilableTickets > 0) {
//logic to bookTicket
--avilableTickets;
psvm(){
Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = new Thread();
t1...t20---start
-> In the program, multiple threads are trying to book tickets at a time
Note: If multiple threads access the same object at a time then there is a chance of
getting data inconsistency problem.
=> To avoid data inconsistency problem, we need to use Synchronization concept
=> Synchronization means allowing only one thread to execute our resource / object
/ logic at a time
Note: By Using Synchronization we can achieve Thread Safety but it will slow down
our execution process.
How to achieve synchronization
-> Using 'synchronized' keyword we can implement synchronization
-> synchronized keyword we can use at two places
1) At method level
2) At block level
Syntax For Synchronized Block:
public void m1( ){
// pre-logic
synchronized ( object ) {
// imp business logic
// post-logic
Syntax For Synchronized Method :
public synchronized void m1( ) {
// important business logic
}
// Java Program with Synchronized Method
package azinit;
public class Demo8 implements Runnable {
public synchronized void printNums() {
for(int i=1; i<=10; i++) {
[Link]([Link]().getName()+ "=>"
+i);
try {
[Link](1000);
}catch(Exception e) {
[Link]();
@Override
public void run() {
printNums();
public static void main(String[] args) {
Demo8 d = new Demo8();
Thread t1 = new Thread(d);
[Link]("Thread-1");
[Link]();
Thread t2 = new Thread(d);
[Link]("Thread-2");
[Link]();
Note: In the above program we are starting 2 [Link] threads will access
printNums ( ) method to print the numbers from 1 to 10.
-> If printNums ( ) method having synchronized keyword then two threads will
execute the method sequentially one after other .
-> If we remove synchronized keyword from the printNums ( ) method then two
threads will access that method at a time.
Note: We can see the difference in the output.
Working with Threads using Anonymous Implementation
package azinit;
import [Link];
import [Link];
import [Link];
public class MyThread {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
[Link]("run() method logic-1");
}
};
[Link]();
Runnable r = new Runnable() {
@Override
public void run() {
[Link]("run method() logic-2");
};
Thread t2 = new Thread(r);
[Link]();
Callable c = new Callable() {
public Object call() throws Exception{
[Link]("call() method logic-3");
return null;
};
ExecutorService exService = [Link](1);
[Link](c);
}
Dead Lock
-> Dead Lock means ambiguity problem among the threads
-> If 2 threads are waiting for each other to release the resources is called as dead
lock.
-> Once we get into dead lock situation then we can't do anything
Ex:
Thread-1 holding resource-1 and waiting for resource-2
Thread-2 holding resource-2 and waiting for resource-1
Note:
Thread-1 will not release resource-1 hence thread-2 will be in waiting state forever
for resource-1
Thread-2 will not release resource-2 hence thread-1 will be in waiting state forever
for resource-2
// Java program which will give dead lock
package azinit;
public class DeadLock {
public static void main(String[] args) {
String s1 = "hi";
String s2 = "Hello";
Thread t1 = new Thread() {
public void run() {
synchronized(s1) {
[Link]("Thread-1 locked resource-
1");
try {
[Link](100);
}catch(InterruptedException e) {
[Link]();
synchronized(s2) {
[Link]("Thread-1 waiting for
resource-2");
};
Thread t2 = new Thread() {
public void run() {
synchronized(s2) {
[Link]("Thread-2 locked resource-
2");
try {
[Link](100);
}catch(InterruptedException e) {
[Link]();
synchronized(s1) {
[Link]("Thread-2 waiting for
resource-1");
}
};
[Link]();
[Link]();
join ( ) method
->join ( ) method is used to hold second thread execution until first thread execution
got completed
package azinit;
public class Demo9 {
public static void main(String[] args)throws Exception {
Thread t1 = new Thread() {
public void run() {
for(int i=1; i<=5; i++) {
[Link]([Link]().getName()+"=>" +i);
try {
[Link](100);
} catch(InterruptedException e) {
[Link]();
};
[Link]("Thread-1");
Thread t2 = new Thread() {
public void run() {
for(int i=1; i<=5; i++) {
[Link]([Link]().getName()+"=>" +i);
try {
[Link](100);
[Link]();
}catch(InterruptedException e) {
[Link]();
};
[Link]("Thread-2");
[Link]();
[Link]();
[Link]();
}
yield ( ) method
->yield ( ) method is used to give chance for other equal priority threads to execute
// Java program with yield ( ) method
package azinit;
public class YieldDemo {
public static void main(String[] args) {
Thread producer = new Producer();
Thread consumer = new Consumer();
[Link]();
[Link]();
class Producer extends Thread{
public void run() {
for(int i=1; i<3; i++) {
[Link]("Producer: Produced Item" +i);
[Link]();
class Consumer extends Thread {
public void run() {
for(int i=1; i<3; i++) {
[Link]("Consumer: Consumed Item" +i);
[Link]();
Inter Thread Communication
-> It is used to establish communication among the threads
-> To achieve inter thread communication we have below 3 methods in Object class
1) wait ( )
2) notify ( )
3) notifyAll ( )
Q) Why these 3 methods available in Object class, why not in Thread class?
-> If these methods available in Thread class then we have to extend Thread class. In
future we can't extend from any other java class bcz java is against for Multiple
Inheritance.
-> If these methods available in Runnable interface then everybody should
implement this method even if they don't need inter thread communication.
-> To overcome all these problems, java kept these methods in Object class so that
every class will have access for these methods.
// Java Program to establish inter thread communication
package azinit;
public class Customer {
int amount = 10000;
synchronized void withdraw(int amount) {
[Link]("going to withdraw...");
if([Link]<amount) {
[Link]("Less balance; waiting for
deposit...");
try {
wait();
}catch (Exception e) {
[Link]();
[Link] -= amount;
[Link]("withdraw completed...");
synchronized void deposit(int amount) {
[Link]("going to deposit...");
[Link] += amount;
[Link]("depostit completed...");
notify();
public static void main(String[] args) {
final Customer c = new Customer();
new Thread() {
public void run() {
[Link](15000);
}.start();
try {
[Link](2000);
}catch(InterruptedException e) {
[Link]();
new Thread() {
public void run() {
[Link](10000);
}.start();
}
Multi Threading Summary
1) What is Multi Tasking
2) What is Multi Threading
3) Advantages of Multi Threading
4) Default Thread in JVM (main)
5) Getting info of main thread ( [Link]( ) )
6) Creating User Defined Threads
7) By Extending Thread class
8) By Implementing Runnable interface
9) By implementing Callable interface
10) run ( ) method vs call ( )
11) Executor Service
12) run ( ) vs start ( ) method
13) Thread Life Cycle
14) Thread Schedular
15) Synchronization (method & block)
16) What is Thread Safety
17) Thread creation with Anonymous implementation
18) Dead Lock (Java Program to give dead lock)
19) join ( ) method vs yield ( ) method
20) Inter Thread Communication
21) Daemon Threads