Java Multithreading Interview Questions Set 1
Categories: Java 8(JDK1.8)
Differentiate between process and thread?
There are the following differences between the process and thread.
a) A Program in the execution is called the process whereas; A thread is a subset of the process
b) Processes are independent whereas threads are the subset of process.
c) Process have different address space in memory, while threads contain a shared address space.
d) Context switching is faster between the threads as compared to processes.
e) Inter-process communication is slower and expensive than inter-thread communication.
f) Any change in Parent process doesn't affect the child process whereas changes in parent thread can affect the child thread.
What do you understand by inter-thread communication?
a) The process of communication between synchronized threads is termed as inter-thread communication.
b) Inter-thread communication is used to avoid thread polling in Java.
c) The thread is paused running in its critical section, and another thread is allowed to enter (or lock) in the same critical section to be executed.
d) It can be obtained by wait(), notify(), and notifyAll() methods.
What is the purpose of wait() method in Java?
The wait() method is provided by the Object class in Java. This method is used for inter-thread communication in Java. The java.lang.Object.wait() is used to pause the current thread, and wait until another thread does not call the notify() or notifyAll() method. Its syntax is given below.
public final void wait()
Why must wait() method be called from the synchronized block?
We must call the wait method otherwise it will throw java.lang.IllegalMonitorStateException exception. Moreover, we need wait() method for inter-thread communication with notify() and notifyAll(). Therefore It must be present in the synchronized block for the proper and correct communication.
What are the advantages of multithreading?
Multithreading programming has the following advantages:
a) Multithreading allows an application/program to be always reactive for input, even already running with some background tasks
b) Multithreading allows the faster execution of tasks, as threads execute independently.
c) Multithreading provides better utilization of cache memory as threads share the common memory resources.
d) Multithreading reduces the number of the required server as one server can execute multiple threads at a time.
What are the states in the lifecycle of a Thread?
A thread can have one of the following states during its lifetime:
a) New: In this state, a Thread class object is created using a new operator, but the thread is not alive. Thread doesn't start until we call the start() method.
b) Runnable: In this state, the thread is ready to run after calling the start() method. However, the thread is not yet selected by the thread scheduler.
c) Running: In this state, the thread scheduler picks the thread from the ready state, and the thread is running.
d) Waiting/Blocked: In this state, a thread is not running but still alive, or it is waiting for the other thread to finish.
e) Dead/Terminated: A thread is in terminated or dead state when the run() method exits.
What is the difference between preemptive scheduling and time slicing?
Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under 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.
What is context switching?
In Context switching the state of the process (or thread) is stored so that it can be restored and execution can be resumed from the same point later. Context switching enables the multiple processes to share the same CPU.
Differentiate between the Thread class and Runnable interface for creating a Thread?
The Thread can be created by using two ways.
a) By extending the Thread class
b) By implementing the Runnable interface
However, the primary differences between both the ways are given below:
a) By extending the Thread class, we cannot extend any other class, as Java does not allow multiple inheritances while implementing the Runnable interface; we can also extend other base class(if required).
b) By extending the Thread class, each of thread creates the unique object and associates with it while implementing the Runnable interface; multiple threads share the same object
c) Thread class provides various inbuilt methods such as getPriority(), isAlive and many more while the Runnable interface provides a single method, i.e., run().
What does join() method?
The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. Join method is overloaded in Thread class in the following ways.
a) public void join()throws InterruptedException
b) public void join(long milliseconds)throws InterruptedException