• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deadlock threads in Java

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and welcome to the Ranch!
 
NavneetNick Agarwal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeff! It is more clear now to me & I need to study more on deadlock.
reply
    Bookmark Topic Watch Topic
  • New Topic