Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • This is very dangerous. You've now introduced a client-side (user's of this class) lock ordering requirement. If two threads are calling method1() and method2() in a different order, they are likely to deadlock, but the user of this class has no idea that this is the case. Commented Jan 14, 2009 at 11:31
  • 10
    Granularity not provided by "synchronized(this)" is out of the scope of my question. And shouldn't your lock fields be final? Commented Jan 14, 2009 at 12:23
  • 14
    in order to have a deadlock we should perform a call from block synchronized by A to the block synchronized by B. daveb, you are wrong ... Commented Jan 14, 2009 at 12:33
  • 5
    There is no deadlock in this example as far as I can see. I accept it is just pseudocode but I would use one of the implementations of java.util.concurrent.locks.Lock like java.util.concurrent.locks.ReentrantLock Commented Jan 14, 2009 at 14:13