aspose file tools*
The moose likes Java in General and the fly likes Deadlock threads in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Deadlock threads in Java" Watch "Deadlock threads in Java" New topic
Author

Deadlock threads in Java

NavneetNick Agarwal
Greenhorn

Joined: Oct 01, 2012
Posts: 5
Hello Guys,

I am preparing for OCP 7 exam. I need your help so that I am able to clear the exam with good percentile. I am completely new to this forum. I am unable to clear this topic. Please help me.

Do Deadlock threads release once their sleep() method's sleep duration expired?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.

See this recent thread on the subject.


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Oh, and welcome to the Ranch!
NavneetNick Agarwal
Greenhorn

Joined: Oct 01, 2012
Posts: 5
Hello Guys,

I am preparing for OCP 7 exam. I need your help so that I am able to clear the exam with good percentile. I am completely new to this forum. I am unable to clear this topic. Please help me.

Do Deadlock threads release once their sleep() method's sleep duration expired?



As I read about the topic mentioned by you, I got my answer..

It means Deadlock may occur due to sleep() is invoked for a thread and it is not necessary that deadlock threads release once their sleep() method's sleep duration expired. and more probably 'NO'

And thanks a lot to clear my point on deadlock. This forum is really helpful.
NavneetNick Agarwal
Greenhorn

Joined: Oct 01, 2012
Posts: 5
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?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
NavneetNick Agarwal
Greenhorn

Joined: Oct 01, 2012
Posts: 5
Thanks Jeff! It is more clear now to me & I need to study more on deadlock.
 
 
subject: Deadlock threads in Java