InterviewSolution
| 1. |
Avoid Deadlock in Java? |
|
Answer» A deadlock is a situation that usually occurs in multi-threading or multi-tasking. It means that two or more THREADS are waiting indefinitely on each other to release the resources they require to complete their EXECUTION. DEADLOCKS can be avoided in Java by trying to avoid the possibilities that give rise to them. These possibilities cannot be completely erased but they can definitely be lessened. Some of the ways to avoid deadlocks in Java are given as follows:
Using unnecessary locks can lead to deadlock so only those members should be locked that are ACTUALLY required.
Nested locks to multiple threads are the main reason for deadlocks. So, locks to multiple threads should be avoided if one thread has already been locked.
If one thread is waiting for another then deadlock occurs. So, Thread.join can be used if the deadlock condition appears for the maximum time required for the execution. |
|