Java threads:
Life cycle of thread:
There are four states in thread lifecycle new (start) ,runnable , non-runnable (wait) and terminate. In Java life cycle of a thread is controlled by JVM. The thread states are
1. New (start)
2. Runnable
3. Run
4. Non-runnable (wait)
5. Terminate
New:
If you create an instance of Thread class and before start() thread it is in the new state.
Runnable:
After stating thread it is in a runnable state, the thread scheduler has not selected to running state.
Run:
If thread scheduler is selected then thread comes in to running state.
Wait (non-runnable):
If thread is alive, but not in running state is called waiting state.
Terminate:
If the thread is in a dead state is called thread is terminated.
Creating thread:
In two ways we can create a thread
1. By extending Thread class
2. By implementing Runnable interface
Thread class:
The thread class has many method and constructors to perform operations on thread. In java Thread class extends Object class and implements Runnable interface and Thread class present in java.lang.Thread package.
Declaration:
public class Thread extends Object
implements Runnable
Thread class constructors:
Thread()
Thread(Runnable r)
Thread(Runnable r, String name)
Thread(String name)
Java program on Thread class
public class Single extends Thread{
public void run(){
System.out.println("single thread is started");
}
public static void main(String args[]){
Single s = new Single();
s.start();
}
}
Runnable interface:
The runnable interface can be implemented by any class and it have only one method that is run() method.
public void run(): This method is used to preform action in thread.
Java Program:
public class Sample2 implements Runnable{
public void run(){
System.out.println("Runnable interface is implemented");
}
public static void main(String args[]){
Sample2 s = new Sample2();
Thread t1 =new Thread(s);
t1.start();
}
}
Difference between implementing Runnable and extending Thread class:
1.Interfaces are loosely coupled and classes are tightly coupled.
2. Runnable interface have only one method that is Run() but in Thread class have many methods
3.Java doesn’t support multiple inheritances if we extend Thread class we can’t extend another class but there is no problem with an interface as we want we can implement interfaces into a single class.
4.Reusability is high in the interface compare with classes.
Type of threads in java:
A thread is a lightweight process, a program under execution is known as
thread. The Threads are sharing address space value and it has own stack value. Mostly thread based architectures are using now. The
threads based architectures are very fast.
In java, there are two types of threads that are,
1. Single thread
2. Multithreading
Single threading:
Single threading is the processing of one command at a time and it executes one task at a time.
Multithreading:
In Multithreading context switching is used to the multi-processing system. Multithreading is a process of executing multiple threads (commands) simultaneously.
Example:
Cinema hall one person is collecting tickets and another person is showing seat.
Advantages of Multithreading:
1. You can perform multiple operations at a time.
2. Exception handling is essay because Threads are independent. If an exception occurred in the thread it doesn’t effect another thread.
Interview Questions:
1) What is Thread in Java?
2) What is the difference between Thread and Process in Java?
3) How do you implement Thread in Java?
4) Explain different types thread in java with an example?
5) Explain the life cycle of the thread?
6) What are the differences between implementing runnable interface and Thread class?
7) Explain about multithreading?
8) What are the advantages of multithreading?