Deadlock and sleep() are unrelated, except in that sleep() is often used as a synthetic means of forcing deadlocks to occur in example code.
Deadlock occurs when threads block in an un-ending way. A common way this occurs is if you have two different locks, thread1 gets lock1, and thread2 gets lock2. Then thread1 tries to get lock2 (but can't because thread2 has it) and at the same time thread2 tries to get lock1 (which it can't do either). In this case both threads are stuck in deadlock because neither can proceed.
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.
NavneetNick Agarwal wrote:One more question arise that...
Let's assume Synchronized block is used incorrectly so Is it possible that a single threaded application may occur deadlock?
That's not a good question without more clarification.
It is impossible to get deadlock with only one thread. However, except for possibly certain specialized JVMs, there's always more than one thread running, even if your app only uses one thread.
So are they talking about the (near) impossibility of a case where the JVM truly only runs one thread? If so, then the answer is no, deadlock cannot occur.
Or are they talking about the more realistic case where the Java app doesn't start any threads of its own, doesn't use Swing, etc., so that there's only one application thread but still additional threads that the JVM uses for its own administrative purposes? In this case, yes, I'm pretty sure you can get deadlock.
Joined: Oct 01, 2012
Thanks Jeff! It is more clear now to me & I need to study more on deadlock.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com