Java Multi-Threading Interview Preparation Guide
Download PDF

Java multi-threading frequently Asked Questions by expert members with experience in Java Multi-Threading. These interview questions and answers on Java multi-threading will help you strengthen your technical skills, prepare for the interviews and quickly revise the concepts. So get preparation for the Java multi-threading job interview

36 Java Multi-Threading Questions and Answers:

Table of Contents

Java Multi-Threading Interview Questions and Answers
Java Multi-Threading Interview Questions and Answers

1 :: Explain Preemptive scheduling and time slicing?

In preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.

In time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks.
The scheduler then determines which task should execute next, based on priority and other factors.
Briefly explain daemon thread.

Daemon thread is a low priority thread which runs in the background performs garbage collection operation for the java runtime system.

2 :: What are the two types of multitasking?

Two types of multitasking are Process-based and Thread-based

3 :: Difference between Process and Thread?

A process can contain multiple threads.
A process has its own memory address space whereas a thread doesn't.
Threads share the heap belonging to their parent process.
One process cannot corrupt another process
A thread can write the memory used by another thread.

4 :: What is time slicing?

Timeslicing is the method of allocating CPU time to individual threads in a priority schedule.

6 :: Name the methods available in the Thread class?

isAlive(),
join(),
resume(),
suspend(),
stop(),
start(),
sleep(),
destroy()

7 :: Write the signature of the constructor of a thread class?

Thread(Runnable threadobject,String threadName)
{

}

9 :: Name the mechanism defined by java for the Resources to be used by only one Thread at a time?

Synchronisation

17 :: Explain volatile?

When more than one threads access the same variable then each thread will maintain its own copy of that variable in the form of local cache.
When a thread is changing the value of variable, it is actually changing the local cache not the main variable memory.
Every thread which is using the same variable doesn’t know anything about the values changed by another thread.
To overcome this problem, if you declare a variable as volatile, then whenever a thread is changing the value of a volatile, it is actually changing to the main memory. So, all other threads which are accessing the same variable can get the changed value.

18 :: What is daemon thread and how it differs from user thread?

A user thread is created by the application (user) while the daemon thread is created by the JVM to provide services to user threads.
If user threads are exists then only daemon threads exists.
You can make a user thread as daemon thread by using setDaemon(true) method.
In an application when only daemon threads are running (i.e. no user thread is running) then JVM will shut down the application and subsequently stops all daemon threads.
To keep the application running, at least one user thread should be running.

19 :: What is Thread leak?

Thread leak is when application does not release references of the thread object and those threads do not get garbage collected.
Number of such unused threads increases with time and it can cause issues in the application like long response time.

To overcome this problem we can do the following

1. By maintaining a log for all entry and exit point of thread.
2. Check how the new thread is created and how it is closed.
3. By using exception handling etc.

20 :: Explain the method of Thread class with example?

In this method of creating thread, we have to extend the Thread class and override the run() method in our class to create a Thread.
We have to create an object of the our class.
Once the object is created then we have to invoke the start() method and it will generate a new thread of execution.

For example

public class MyThread extends Thread{
public void run(){
// code to execute under the thread
}
public static void main(String [] args){
MyThread c = new MyThread();
c.start();
}
}

21 :: What are the different states of a threads lifecycle?

Following are the different states of threads:

1. New – A thread is called in New state until the start() method is called for the object of that thread. The thread is not alive in this state.

2. Runnable – The thread is called in Runnable state after the start() method is called for the object of that thread. The thread may enter into the Runnable state from Running state. The thread is called alive in this state.

3. Running – The thread is called in Running state when the thread scheduler take it from the Runnable thread’s pool.

4. Waiting/Blocked/Sleeping – In these states the thread is alive but it’s not in Runnable state. A running thread is called in these states after wait() or sleep() method is called that thread or when the thread is blocked while waiting for I/O resources.

5. Dead – After the execution of run() method is complete then the thread is called in dead state. Once a thread is dead then it cannot be start again.

22 :: What is difference between thread and process?

Process has its own memory address while the thread share the address of the process by which it is created.
Thread can access the data segment of its process directly while processes can access their own copy of the data segment of its parent process.
Threads have almost no overheads while processes may have overheads.
We can create a new thread easily but to create new process we have to duplicate the parent process.
If any changes in main thread are occurred it can affect the behavior of the other threads of the same process. While in case of process if any changes is occurred in parent process it won’t affect the behavior of parent process.
The direct communication is possible between threads of same process while in case of same sibling processes only interprocess communication is possible.

23 :: What happens if we invoke run method without calling the start method for a thread instance?

If we instantiate a thread it is called in new state until the Start() method is called.
If we don't call a start() method for that thread instance, the thread is not called alive.
If we invoke run method without calling the start method for a thread instance, the code in run() method wil not be executed by a new thread but it will be executed by the existing thread only.

24 :: What are the advantages or usage of threads in Java?

Following are the advantage of using threads in Java.

Threads support concurrent operations. For example, Server can handle multiple requests coming from different clients by managing separate thread for each request.
Threads often result in simpler programs. Updating of separate views can be managed by separate Thread to give continuous updates.
Threads provide a high degree of control on application.
Threading gives the concurrency in our application by simplified coding.
Using threading, a computer with more than one CPU can execute multiple threads on different functional units without using time sharing.
The cost of communication between threads is relatively low.

25 :: Explain the method of Runnable interface with example?

In this method of creating thread, we have to implement the Runnable interface and implement the run() method in our class.
We have to create an object of our class.
Then we you have to pass the reference of that object for creating a new object of Thread
Invoke the start method using this Thread object which will create a new thread of execution.

For example

public class MyThread implements Runnable{
public void run(){
// code to execute under the thread
}
public static void main(String [] args){
MyThread c = new NewThread();
Thread t = new Thread(c);
t.start();
}
}