Java Multi Threading Interview Questions

Q41. What is the difference between sleep() and wait() method, with regards to lock?

Ans: When thread calls sleep(), method it is not necessary to have it in synchronized block. However, if you are in synchronized block and you call sleep() method on thread. It will not release the lock. And also note that sleep is a Thread class method and not a part of “Common object”.

Q42. What is the difference between sleep() , join() and yield() method of Thread class?

Ans : sleep() : causes the thread to definitely stop executing for a given amount of time; if no other thread or process needs to be run, the CPU will be idle (and probably enter a power saving mode).

yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent.

join() If any executing thread t1 calls join() on t2 i.e; t2.join() immediately t1 will enter into waiting state until t2 completes its execution.

Q43. What do you mean by missed signals?

Ans: It is possible that a thread can miss a signal. Let’s assume there are two threads ThreadA and ThreadB. ThreadA is going to enter in wait() state and ThreadB is going to notify(). However, ThreadB notify() ThreadA before ThreadA enter in wait() block. In this case notify() signal from ThreadB is missed by ThreadA. Hence, ThreadA remain in wait state forever, which could be a big problem. Missed signal problem can be avoided using proper code block around shared object.

Q44. What is a spurious wakeup?

Ans: It is possible because of any surprise reason like hardware issue, thread gets signal. Lets assume again we have two threads ThreadA and ThreadB , ThreadA is waiting on notification from ThreadB. However, ThreadA gets notification that ThreadB has done with its processing. This is a false signal and known as a spurious wakeup of ThreadA.

To avoid spurious wakeup issue, we should check wait state of shared object in while loop. So in case of false signal it has to check condition on shared object again.

Q45. When should notifyAll() used?

Ans : notifyAll() should be used when many threads are waiting for signal.

Q46. Why we should avoid wait() or getting lock on a Constant strings or global objects?

Ans : We should avoid Constant String, because JVM internally store different Constant String with same value as a Single object.

String str1=new String(“HadoopExam.com”);
String str2=new String(“HadoopExam.com”);

JVM internally stored both the str1 and str2 as two separate reference variable pointing to single objects in String constant pool. Hence, if two different threads are working on two different object references can get a wrong/false signal.

Same problem occurs with globally shared objects.

Q47. What is Thread deadlock?

Ans: Deadlock is situation, when two or more threads are blocked and waiting to get lock on objects. Lets assume there are two threads ThreadA and ThreadB and there two shared objects obj1 and obj2. ThreadA is holding a lock on obj1 and also needs lock on obj2 to proceed further. Similarly ThreadB is holding lock on obj2 and needs lock on obj1 to proceed further. Now both ThreadA and ThreadB is waiting for each other to release the lock and no one can proceed further. This situation is called deadlock.

Q48. Why deadlock happens?

Ans: Deadlock occurs because two or more threads are waiting for each other locks at the same time. However, order of getting and releasing lock is not proper it can arise deadlock.

Q49. How can deadlock be prevented?

Ans: to prevent deadlock following techniques can be used

· Lock Ordering: Locks needs to be taken and released in same order by each thread

· Lock Timeout and retry: Threads which are waiting to get lock on threads does not get locks in specified time limit. Then it should release all the locks it has taken needs to re-try after sometime.

· Deadlock Detection: Detect the deadlock and try to avoid. You can use thread priority as well to avoid deadlock.

Q50. What is thread starvation and what causes it?

Ans: Starvation means thread is waiting for infinite time or for longer periods because of it is not getting lock or CPU time. Following are the reasons for thread starvation

1. Lower priority thread will never get a chance, because higher priority threads always exists in application.

2. Thread is waiting for indefinitely, because it is not able to get lock on thread.

Premium Training : Spark Full Length Training : with Hands On Lab

Previous Next

Home Spark Hadoop NiFi Java












Disclaimer :

1. Hortonworks® is a registered trademark of Hortonworks.

2. Cloudera® is a registered trademark of Cloudera Inc

3. Azure® is aregistered trademark of Microsoft Inc.

4. Oracle®, Java® are registered trademark of Oracle Inc

5. SAS® is a registered trademark of SAS Inc

6. IBM® is a registered trademark of IBM Inc

7. DataStax ® is a registered trademark of DataStax

8. MapR® is a registered trademark of MapR Inc.

2014-2017 © HadoopExam.com | Dont Copy , it's bad Karma |