12 Java Multithreading Interview Questions
Most of the lucrative positions in Java Programming require excellent core Java multithreading knowledge and skills. It is a much sought-after skill as only a handful of Java programmers are really hands-on with the multithreading concepts and concurrency problems.
Although every interview and its scope is different, below is the list of top most frequently asked Java multithreading questions:
1. What is Multithreading?
In simple terms, the process of executing many threads simultaneously is called Multithreading. Multithreading is akin to multitasking and is used when no process in the thread can wait for the other process to complete and all have to finish at the same time.
2. What exactly is a Thread?
A thread is a sub-process that has a separate path of execution. Each thread runs independently in its own execution stack. There can be multiple threads in a single process. Even though the processes execute separately, they share process resources.
3. What are the benefits of Multithreading?
First and foremost, Multithreading allows for quick execution of tasks as each thread is executed independently.
In Multithreading, programs are always ready for input in spite of a background task running simultaneously.
Since threads share the common resources, the cache memory is more efficiently used.
Server dependency is reduced as one server is capable of executing multiple threads at the same time.
CPU is never idle waiting for a program thread to free resources. Multiple thread execution is much better than multiple process execution.
4. What are the key differences between a process and a Thread?
A process is a program in execution.
A thread is a sub-process.
Processes have separate memory addresses whereas threads share address space in memory.
Context switching between processes is faster than context switching between threads.
Communication between threads is much faster than communication between processes.
Since processes don’t share any resources, any change to the parent process won’t affect the child process whereas changes to the parent thread will impact the child thread.
5. Explain Inter thread communication.
Communication between 2 or more synchronized threads is termed as inter thread communication. Inter thread communication helps to prevent thread polling in Java.
Even if one thread is executing and then paused, another thread can enter or lock into the same point and still be executed.
wait(), notifyall(), notify() methods are used to implement inter thread communication.
6. Explain the function of wait() method in Java.
The wait() method is provided by the Object class in Java for the purpose of inter thread communication. Java.lang.Object.wait() is the method to pause the current thread. The thread is paused until another thread calls the notify() or notifyall() method.
The wait() method must be called from the synchronised method for proper communication along with notify() and notifyall() methods.
Also Read: How do you Create a Plan B for your Career
7. Explain the life cycle of a Thread.
A thread can go through any of the following stages in its life cycle:
New: In the New state, a Thread class is initiated using the New operator but boot yet started.
Runnable: In this state, the thread is started using the start() method.
Running: This is the state where the thread scheduler picks up the thread from the ready state and makes in running.
Waiting: This state, also called the Blocked state, is alive but running as it is waiting for another thread to finish or a resource to free up.
Terminated: A thread is in the dead or terminated state when it exits with the run() method.
8. Explain Context Switching.
The state of the thread or the process is stored in Context Switching so that it can be restored and execution resumed at a later point of time. In this way, context switching facilitates multiple processes to share CPU resource.
9. Explain different ways to create a Thread.
There are two ways to create a thread- by extending the thread class or by implementing the thread class via Interface. The differences in these both methods are:
If we extend from the Thread class, we can’t extend from any other class as Java doesn’t permit multiple inheritance. But if the Runnable interface is implemented, even the base class can be extended, if needed.
If we extend from the Thread class, each associated thread can create a unique object. While implementing a Runnable interface, multiple threads have to share the same object.
There are various inbuilt methods in Thread class such as getPriority(), isAlive and so on while runnable interface provides only one method, run().
10. Differentiate between the wait() and sleep() method.
The wait() method has to be defined in the Object class while the sleep() method is defined in the Thread class.
Implementing the wait() method releases the lock whereas implementing the sleep() method does not release the lock.
11. Can a Thread be started twice?
A thread cannot be started twice as once a thread is started and executed, it goes to the Dead state. If an attempt is made to restart the thread, the compiler will throw a runtime exception.
12. What does Synchronization mean?
When multiple threads are trying to access the same resources, synchronization is needed to control the access to avoid any consistency issues as well as thread interference.
Java implements the process of synchronization to allow only a single thread to execute at a time. This prevents errors from multiple thread execution at once.
Synchronization can be implemented either by the synchronized method, synchronized block, or by static synchronization.
Looking for Multithreading Jobs in top cities? Click at the links below:
• Multithreading Jobs in Gurgaon
• Multithreading Jobs in Mumbai
• Multithreading Jobs in Chennai
• Multithreading Jobs in Noida
• Multithreading Jobs in Pune