• 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

is it a DeadLock Condition?

 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic