Updated March 8, 2023
Introduction Java Interview Question and Answers on Multithreading
Before proceeding to the Java interview question on Multithreading, let’s discuss something some facts about multithreading.
What is the thread?
It is the smallest unit of processing a particular work is called a thread. It is lightweight in nature so easy to use. Now it comes to our mind that why we should need it, multithreading is used to achieve multitasking. Multitasking could be achieved in another way also, and it is known as Multiprocessing.
However, multithreading is more popular, and it is efficient to use over multiprocessing because threads have properties that it uses a shared memory area, whereas, in the case of multi-process, it consumes a lot of memory and other resources, which is not efficient.
So the process of executing multiple threads simultaneously to do some work is called as multithreading.
Some advantages of using multithreading:
- Threads work independently, so it is independent of other operations going in the systems, and thus it is possible to perform multiple operations at the same time.
- Time consumption is less with the use of multithreading as many operations can be performed simultaneously.
- Threads are independent, as discussed already, so an exception that occurs in a single thread will not interfere with the operations of other threads.
- The cost of communications between two threads is low.
We can achieve multitasking in the following two ways:
- Process-based Multitasking (Multiprocessing)
- Thread-based Multitasking (Multithreading)
Life Cycle of a Thread
- New
- Runnable
- Running
- Non-Runnable (Blocked)
- Terminated
How are threads created?
Now, this is the most important and the first point by which we can create the thread. There are two ways by which threads can be created, which are discussed below:
- By extending Thread class
- By implementing the Runnable interface
NOTE:
When a new thread was created, the following tasks are performed:
- In a new call stack, a thread will be created.
- The thread changes its state from New to Runnable.
- When the thread is started its finds the run () method, and then it will execute.
Example:
By extending Thread class
class MyThread extends Thread
{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
MyThread t1=new MyThread ();
t1.start();
}
}
Example:
By implementing Runnable interface
class MyThread implements Runnable
{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
MyThread m1=new MyThread ();
Thread t1 =new Thread(m1);
t1.start();
}
}
Scheduling in Thread
As they all know, that schedule means to do a plan a task at a particular time. Thread Scheduling is a similar mechanism of scheduling a particular thread which maintains some protocols.
There are two different ways by which Thread scheduling works in Java.
- Pre-emptive Scheduling
- Time Slicing.
Pre-emptive Scheduling
JMV decides the highest priority task and starts execution until it enters the waiting or dead states or a higher priority task comes into existence.
Time Slicing
JVM start executing a particular thread for a specific time period and then move the same thread to the pool and choose other thread based on priority and other factors for executions. This process continues until all the thread ends its process.
Now, if you are looking for a job related to Java Multithreading, you need to prepare for the 2022 Java Interview Question on Multithreading. It is true that every interview is different as per the different job profiles. Here, we have prepared the important Java Interview Questions and answers on Multithreading which will help you get success in your interview.
In this 2022 Java Interview Question on Multithreading article, we shall present the 10 most important and frequently asked Java Interview Question on Multithreading. These interview questions are divided into two parts are as follows:
Part 1 – Java Interview Question on Multithreading (Basic)
This first part covers the basic Interview Questions and answers on Multithreading.
Q1. Differentiate between Process and Thread?
Answer:
A process is a program or application, whereas a Thread is a single task to be executed within a process. The thread is lightweight, whereas the process is heavyweight. Thread requires less, whereas Process requires more resource; thus, it is termed as heavily weighted in nature.
Q2. Differentiate between user Thread and daemon Thread?
Answer:
A thread created in java is termed as User Thread. A Daemon Thread always runs in the background, and its complete life cycle depends on the main thread. A daemon thread running in the background will not prevent JVM from terminating it. Child thread created from a daemon thread will also be a daemon thread.
Let us move to the next Java Interview Questions on Multithreading.
Q3. The different way of creating a Thread in Java?
Answer:
Threads in Java can be created in two ways:
- By extending Thread class.
- By implementing Runnable Interface.
Q4. What is the lifecycle of Thread?
Answer:
There is the common Java Interview Questions on Multithreading asked in an interview. Following are the life cycle of a Thread:
- New
- Runnable.
- Running.
- Blocked.
- Terminated.
Q5. What happens if we call the run () method of a Thread class?
Answer:
Calling the run () method directly will compile and execute the program successfully, but the same program will not be treated as Thread because no new call stack will be created, and the program starts its execution in the same call stack where the main is running.
For creating a Thread that should run with a new call stack, one has to use the start () method of the Thread class.
Part 2 – Java Interview Question on Multithreading(Advanced)
Let us now have a look at the advanced Interview Questions and Answers on Multithreading.
Q6. Can we pause the execution of a Thread at a specific time?
Answer:
Yes, this can be achieved in java by calling the sleep () of the Thread class. The sleep () method also takes an argument that indicates the time in milliseconds.
Q7. How can we achieve scheduling of thread in java?
Answer:
Yes, thread scheduling in java is possible. Threads in Java can be scheduled in two ways, i.e. Time Slicing and Pre-emptive Scheduling.
Let us move to the next Java Interview Questions on Multithreading.
Q8. Can a Thread be started twice?
Answer:
No, a thread cannot be started twice. If we try to start a thread twice, it will throw “java.lang.IllegalThreadStateException”.
Q9. What is a shutdown hook in Java?
Answer:
This is the most popular Java Interview Questions on Multithreading asked in an interview. A shutdown hook is a mechanism that is used for clean-up resources when the JVM shuts down normally or abruptly.
Q10. What is volatile?
Answer:
Volatile is a keyword in java, and it can be used with variables. If a variable is declared as volatile, all the threads will read the value of the same variable from the main memory rather than from the cache; thus, it prevents error read when multiple threads use the same variable in their operations.
Recommended Articles
This has been a guide to the list of Java Interview Questions and answers on Multithreading so that the candidate can crackdown these Interview Questions on Multithreading easily. Here in this post, we have studied top Java Interview Questions on Multithreading which is often asked in interviews. You may also look at the following articles to learn more –