Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Locking trouble...

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have two objects that are monitors. A client calls a method on the first, which is synchronized so therefore gets ownership of the first objects lock. The client then enters a method from the second object, which is also synchronized, and so the thread now owns two locks. The client then calls wait on the second object.

The problem is that wait releases it's lock on the second object, but i don't think it does so on the first, this causes deadlock.

Is there a way to make the thread give up both locks on the wait method of the second object? I need both locks, but I only want to wait on the second object.

Is there any way to do this?

Any help is appreciated..

James.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are absolutely right, only the second lock is released... but put yourself into the position of the first object. How do you know that when you call the second object's method, that it will do a wait() that is thread safe? There is no way that the JVM can know that.

And if it is thread safe, why are you still synchronized? If at the time, you call the second object, you don't need the lock, you should release it -- use synchronized blocks, and get the method call out of the block.

And if you need the lock all the way to the point of the wait() method call, then unfortunately, you may have to refactor your algorithm. Consider using an external locking mechanism, that both objects can share.

Henry
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Redesign so that you don't need both locks. Rarely should you consciously require 2 locks at the same time.
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic