Threads in Java
Threads in Java
Threads in Java
In Java
Multithreading
•Multithreading in java is a process of executing multiple threads simultaneously.
•But we use multithreading than multiprocessing because threads share a common memory area.
•They don’t allocate separate memory area so saves memory, and context-switching between the threads
takes less time than process.
1) It doesn’t block the user because threads are independent and you can perform multiple operations at same
time.
3) Threads are independent so it doesn’t affect other threads if exception occur in a single thread.
4) A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of execution.
5) Threads are independent, if there occurs exception in one thread, it doesn’t affect other threads. It shares a
common memory area.
MULTI-THREADING VS MULTI-TASKING
Multithreading Multitasking
Multithreading Multitasking
It is a programming concept in which a program or It is an operating system concept in which multiple tasks
process is divided into two or more sub programs. are performed simultaneously.
The processor has to switch between different parts of The processor has to switch between different programs
thread or program
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
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.
How to create thread
There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
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.
Commonly used Constructors of Thread class:
•Thread()
•Thread(String name)
•Thread(Runnable r)
•Thread(Runnable r,String name)
Java Thread Example by extending Thread class
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
OUTPUT: Thread is running…
By Implementing Runnable Interface
If multiple threads are simultaneously trying to access the same resource strange results may occur. To overcome them java
synchronization is used. The operations performed on the resource must be synchronized.
Monitor is the key to synchronization. A monitor is an object that is used as a mutually exclusive lock.Only one thread can own a
monitor at a given time. When a thread acquires a lock, it is said to have entered the monitor. All other threads attempting to enter the
locked monitor will be suspended until the first thread exits the monitor (other threads are waiting at that time) .
}
class MyThread1 extends Thread{ public class TestSynchronization2{
Table t; public static void main(String args[]){
MyThread1(Table t){ Table obj = new Table();//only one object
this.t=t; MyThread1 t1=new MyThread1(obj);
} MyThread2 t2=new MyThread2(obj);
public void run(){ t1.start();
t.printTable(5); t2.start();
} }
} }
class MyThread2 extends Thread{
Table t; Output: 5
MyThread2(Table t){ 10
this.t=t; 15
20
}
25
public void run(){ 100
t.printTable(100); 200
} 300
} 400
500
2. Using synchronized Statement:
This is the general form of the synchronized statement:
synchronized(objRef)
{
// statements to be synchronized
}
objRef is a reference to the object being synchronized. A synchronized block ensures that a call to a
synchronized method that is a member of objRef’s class occurs only after the current thread has
successfully entered objRef’s monitor.
Example: class MyThread1 extends Thread{ public class TestSynchronizedBlock1{
class Table{ Table t; public static void main(String args[]){
void printTable(int n){ MyThread1(Table t){ Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
synchronized(this){//synchronized block this.t=t;
MyThread2 t2=new MyThread2(obj);
for(int i=1;i<=5;i++){ }
t1.start();
System.out.println(n*i); public void run(){ t2.start();
try{ t.printTable(5); }
Thread.sleep(400); } }
}catch(Exception e){System.out.println(e); Output: 5
} } 10
class MyThread2 extends Thread{ 15
}
20
} Table t;
25
}//end of the method MyThread2(Table t){ 100
} this.t=t; 200
}
public void run(){
t.printTable(100);
}
}
Deadlocks