Hi Friends,
In this post I'm sharing multithreading interview questions asked in Ericsson.
My other interview posts also help in clearing the java interview rounds.
You may find below links very helpful in any java interview.
Java Interview Questions and Answers:
Question 1:
Can a thread acquire multiple locks at the same time and how?
Answer:
Yes, a thread can acquire locks on multiple objects at the same time.
Also, a thread can acquire lock on the same object multiple times.
Locks obtained using synchronized are implicitly reentrant.
Whenever we use nested synchronized blocks with 2 different objects, then the same thread can acquire lock on both of these objects. But care needs to be taken in this case, as if we take two pairs of synchronized blocks with different order of similar objects, then it may cause deadlock.
Question 2:
What are the steps to avoid deadlock in java programs?
Answer:
There are multiple ways in which we can avoid deadlocks:
- Avoid using nested synchronized blocks
- Always request and release locks in the same order.
- Use synchronization only whenever required.
- If two threads are waiting for each other to release shared resources, then this issue can be resolved by letting each waiting thread retry the operation at random interval until they successfully acquire the resource.
- Do not perform operations that can block while holding a lock. e.g. Do not perform file I/O over the network while holding a lock.
Question 3:
When we call Thread object's run() method, then in which thread that run() method gets executed?
Answer:
Calling run() method on Thread's object will cause execution of that method in current thread.
If a thread object was constructed by instantiating the subclass of Thread and fails to override run() method, then any calls to the run() method will invoke Thread.run(), which does nothing.
So, we should never call run() method on thread object.
Question 4:
When we can and should use ThreadGroup?
Answer:
Each thread in java is assigned to one ThreadGroup upon it's creation. These groups are implemented by java.lang.ThreadGroup class.
When thread group name is not specified, the main default thread group is assigned by JVM.
ThreadGroups are useful for keeping threads organized.
There are few useful methods which can be used for specific use cases.
ThreadGroup.activeCount():
- Returns an estimated number of active threads in the current thread's thread group and it's subgroups.
ThreadGroup.enumerate():
- It copies into the specified array every active thread in this thread group and it's subgroups.
Question 5:
How to decide which method to call : notify() or notifyAll() ?
Answer:
Whenever we call notify() or notifyAll() methods, it is guaranteed that only one thread will get the lock and resume execution.
Calling notify() is permitted under following conditions:
- All waiting threads have identical condition predicates.
- All threads perform the same set of operations after waking up. That is, any one thread can be selected to wake up and resume for a single invocation of notify.
- Only one thread is required to wake upon the notification.
Question 6:
Why to use Thread pool for handling multiple tasks?
Answer:
Thread Pools allows a system to limit the number of simultaneous requests that it process to a number that it can comfortable serve.
If we don't use Thread Pool then, for every request, creating a new thread consumes a lot of time and resources and for task processing.
Describing here some of the benefits of using Thread Pool:
- Thread pool minimizes the overhead of thread lifecycle management because the threads in a thread pool can be reused and can be efficiently added or removed.
- It also reduces the time and resources required for thread creation.
- It also ensure graceful degradation of service when traffic bursts.
Question 7:
What is bounded thread pool and what happens when we execute interdependent tasks in bounded thread pool?
Answer:
A bounded thread pool is a pool in which we specify an upper limit on the number of threads that can currently execute in a thread pool.
We should not use bounded thread pool to execute tasks that depend on the completion of other tasks in the pool.
A form of deadlock called thread-starvation deadlock arises when all the threads executing in the pool are blocked on tasks that are waiting on an internal queue for an available thread in which to execute.
That's all from this post.
Thanks for reading !!
Thank you for sharing this Multithreading in Android Question. This will help a lot in Interview.
ReplyDelete