Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it a DeadLock Condition?

 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1220
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you execute it?
You can often answer the "will it" question by running the code.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1220
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1220
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic