Advanced Java Programming - Old Questions
1. What is multithreading? Why is it important to develop in computer programs? Discuss life cycle of thread in detail.
The process of executing multiple threads simultaneously is known as multithreading. The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.
Multithreading is important due to following reasons:
- It doesn't block the user because threads are independent and we can perform multiple operations at the same time.
- We can perform many operations together, so it saves time.
- Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.
Example:
Program to create two threads. The first thread should print numbers from 1 to 10 at intervals of 0.5 second and the second thread should print numbers from 11 to 20 at the interval of 1 second.
class First extends Thread
{
@Override
public void run()
{
for (int i=1; i<=10; i++)
{
System.out.println(i);
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
}
class Second extends Thread
{
@Override
public void run()
{
for (int i=11; i<=20; i++)
{
System.out.println(i);
try{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
}
public class ThreadInterval
{
public static void main(String[] args)
{
Thread first = new First();
Thread second= new Second();
first.start();
second.start();
}
}
Life Cycle of Thread
A thread in Java at any point of time exists in any one of the following states.
- New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread. In simple words, a thread has been created, but it has not yet been started. A thread is started by calling its start() method.
- Runnable: The thread is in the runnable state after the invocation of start() method, but the thread scheduler has not selected it to be the running thread. A thread starts life in the Ready-to-run state by calling the start method and wait for its turn. The thread scheduler decides which thread runs and for how long.
- Running: When the thread starts executing, then the state is changed to a “running” state. The scheduler selects one thread from the thread pool, and it starts executing in the application.
- Dead: This is the state when the thread is terminated. The thread is in running state and as soon as it completed processing it is in “dead state”. Once a thread is in this state, the thread cannot even run again.
- Blocked (Non-runnable state):This is the state when the thread is still alive but is currently not eligible to run. A thread that is blocked waiting for a monitor lock is in this state. A running thread can transit to one of the non-runnable states depending on the situation. A thread remains in a non-runnable state until a special transition occurs. A thread doesn’t go directly to the running state from a non-runnable state but transits first to the Ready-to-run state.