2

I was wondering if

synchronize (lock) {
    ... 
}

Where lock is an instance of java.util.concurrent.locks.Lock, treats lock like any other object or as the try-finally idiom i.e.

 lock.lock(); 
 try {
     ... 
 } finally { 
    lock.unlock();
 }

3 Answers 3

13

Lock documentation:

Note that Lock instances are just normal objects and can themselves be used as the target in a synchronized statement. Acquiring the monitor lock of a Lock instance has no specified relationship with invoking any of the lock() methods of that instance. It is recommended that to avoid confusion you never use Lock instances in this way, except within their own implementation.

So basically, it's treated as any other object. And, don't do that.

Sign up to request clarification or add additional context in comments.

1 Comment

@hhafez: "Don't do that" because the documentation says "It is recommended that to avoid confusion you never use Lock instances in this way, except within their own implementation."
3

It will treat the lock just like any other object.

Comments

-1

The lock statement in the C# programming language can be applied to restrict access to a specific part of code to only one thread at a time.

1 Comment

This is a Java question, not a C# question. (I did not vote down.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.