wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes is it a DeadLock Condition? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "is it a DeadLock Condition?" Watch "is it a DeadLock Condition?" New topic
Author

is it a DeadLock Condition?

satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
I assume, Thread-A has acquired the lock on Object-1 and Thread-B has acquired the lock on Object-2. At the same time, Thread-B is trying to acquire the lock on Object-1 which is already owned by Thread-A. Similarly Thread-A is trying to acquire the lock on Object-2 which is already owned by Thread-B.

The above case may lead to Deadlock situation. I was trying to simulate the deadlock situation by writing the following


Will the above case lead to deadlock condition?

sat
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
What happens when you execute it?
You can often answer the "will it" question by running the code.
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Deadlock simulator is printing on the console and the program is not terminated. This is the behaviour of this program after running.
I want to know whether this example leads to a deadlock situation.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
Have you read the API doc for join()?
Your code does a join on the current thread. The other class has NO bearing on how your code hangs. You can remove it and the program hangs in the join.
I was suprised the join didn't throw an exception. How can a thread continue executing after it ends???
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Norm:
I was suprised the join didn't throw an exception. How can a thread continue executing after it ends???

The thread is not executing after it has ended. The main thread has not at all finished execution.

In the provided code, the main thread is trying to join with itself.
Join mandates that the thread on which the join is called must complete before the thread from which the join is called can resume.
Since, the thread calling join and on which the join is called are same, the join will never return.
I am not sure whether this scenario categorized into a "deadlock" but definetly the program never completes.
[ July 28, 2008: Message edited by: Nitesh Kant ]

apigee, a better way to API!
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Yes, thread myThread has completed but main thread is not completed. I wanted to know whether this condition will lead to deadlock situation.
Shall we call it as starvation?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
join causes the current thread to wait until the other thread ends.
If the 'other thread' is the current thread (ie there is only one thread)
how can a thread wait until that thread itself ends, to continue?
Seems like the system should catch that because the thread would never exit the join.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is it a DeadLock Condition?