NavneetNick Agarwal wrote:
It means Deadlock may occur due to sleep() is invoked for a thread
Not really. That is, invoking sleep() cannot itself cause deadlock. What it can do is give another thread a better chance to run, so that deadlock can occur before the current thread releases a lock.
Deadlock is when two or more threads are each waiting on a resource that the other holds. For instance:
If T1 gets X before T2 does, and if T2 gets Y before X does, then T1 holds X's lock and is waiting for Y's lock, and T2 holds Y's lock and is waiting for X's lock. Neither thread can proceed, because they're each waiting on a lock that the other holds, and neither can release its lock to let the other proceed until it obtains the lock that the other holds but that the other can't release because it can't proceed without the other lock, and so on.
That's unlikely with this code, because the two locks are obtained immediately after each other. If you put this in a loop for millions of iterations, you might hit a deadlock, but it would take some fairly tricky timing.
You can make a deadlock more likely by adding sleep() calls in each thread between its first and second sync blocks.
and it is not necessary that deadlock threads release once their sleep() method's sleep duration expired. and more probably 'NO'
I have absolutely no idea what you're trying to say here.