Does lock Lock the whole object?

java lock object
c# lock static object
c# lock object best practice
you can synchronize the entire method or just a part of the method body.
another thread can hold the lock to synchronized methods of a different instance.
java lock vs synchronized
c# lock vs mutex
c# lock alternative
class Resource{
  public Lock lock = new ReentrantLock();

  public void A(){
    try{ .. }

  public void B(){
    try{ .. }


  public void C(){
    ... //Nothing to do with lock here

Now lets say I have 3 threads T1, T2, T3 and one Resource instance called resource.

The first thing that runs is T1 calls resource.A(). Now, lets say resource.A() takes 1500 minutes, if T2 calls resource.B() during the 100th minute (meaning the call from T1 is running), what happens?

When I perforamed the lock.lock() did it lock the object or did it lock the method?


Lock is obtained by a thread. So if one thread X holds the lock, other threads can't obtain the lock until it released by X.

In your case, method B can't obtain the lock before method A release the lock.

Synchronized block will lock the whole object or the method alone , All of the problems in question can be avoided by always locking on a The object that you choose to use with the lock statement doesn't have  The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.

The first thing that runs is T1 calls resource.a(). Now, lets say resource.a() takes 1500 minutes, if T2 calls resource.b() during the 100th minute (meaning the call from T1 is running), what happens?

What happens is that T2 is blocked until T1's call to resource.b() releases the lock. In this case, it will be blocked for 1400 minutes.

When I performed the lock.lock() did it lock the object or did it lock the method?

Neither really.

You didn't lock the Resource instance:

  1. The Resource instance has an intrinsic lock ... which is not being used here.
  2. Other threads calling resource.c() won't be blocked.

You didn't actually lock the methods. The locking is inside the methods. So for example, method a or method b could do stuff before calling lock.lock(), or after releasing it.

What you actually did is that you acquired the lock on the ReentrantLock object. In this case, two out of three of the methods use that lock object, so you are in effect locking the relevant parts of those methods against calls by different threads.

However, if the calls were made by the same thread (e.g. if a() called b()) then the 2nd call would not be blocked. (That is what "reentrant" means in this context.)

Best Practices When Using the Lock Statement, In Java, a synchronized block of code can only be executed by one thread at a time. Object level lock is mechanism when we want to synchronize a non-static method But I thought that if lock is acquired on the entire obj by putting sync on​  Lock Object: In SAP, lock objects are used to avoid the inconsistencies that can be caused by data modification or creation with respect to the database. An SAP system gives multiple users simultaneous access to the same database records and synchronizes all of these using the lock objects. The lock mode assigned to the lock object defines the

did it lock the object or did it lock the method?

Neither. It locked the object's intrinsic monitor.

A common rookie mistake is to think that if thread A synchronizes on some object, o, then other threads will not be able to use or modify object o. That is not true at all. The only thing that thread A prevents by entering a synchronized(o) block is, it prevents other threads from synchronizing on the same object at the same time.

If you want to ensure that only one thread at a time can use or modify an object, then you must ensure that every place where your code uses or modifies it is wrapped in a synchronized block that synchronizes on the same object.

Some programmers prefer to use a separate, private lock object:

private static final Object lock = new Object();

public void mymethod(...) {
    synchronized(lock) {
        ...access to this is protected by lock...

The advantage is, it denies other classes the ability to mess with the lock that protects your class's private data.

Object level lock vs Class level lock in Java, Holding a lock is how one thread tells other threads: “I'm changing this thing, don'​t cash machines, all of which can read and write the same account objects in  an object declared in my parent class that I use as the lock. But what I noticed is that in one of the childs methods, I lock that object, then while still in the critical section I call one of my inherited methods, and lock the same object. So it's esentially this object _lock = new object(); lock(_lock) {lock(_lock) {//Do Something}} Is this

Reading 23: Locks and Synchronization, In this case, the thread acquires the intrinsic lock for the Class object associated with the class. Thus access to class's static fields is controlled by a lock that's  A lock statement takes a reference to an object which may be “acquired” by a thread; such an object is called a “monitor”. Only one thread may acquire a particular monitor at one time. If a second thread attempts to acquire a particular monitor while a first thread is holding it, and it will wait, block, until the object is released.

Intrinsic Locks and Synchronization (The Java™ Tutorials , Synchronization : Synchronization is a modifier which is used for method and block only. With the help of synchronized modifier we can restrict a shared  description of handling lock objects is provided in the ABAP Dictionary Documentation under. Lock Objects [Extern]. The ABAP documentation explains the key elements of the lock concept with regard to. programming ABAP transactions in the section entitled The R/3 Lock Concept [Extern].

Object level and Class level locks in Java, While a lock is held, the thread that holds the lock can again acquire and Avoid using the same lock object instance for different shared  Locking the focus on the apple. The yellow reticle will flash a couple of times, and it will lock onto the object until you close the app or tap on another part of the screen. In my use, I noticed that highlights sometimes get blown out when focus lock occurs, so after you lock the focus you can tap on the sun icon

  • Lock locks the lock.
  • Ok I see, so if T2 calls resource.B(), it will wait until T1 gives up the lock?
  • you are right. If host of lock don't give up the lock, the lock will be never available to others.
  • Ok and one more question. Is it possible for T2 to call resource.C(), the method C() does nothing with the lock so if resource.A() is running that is called by T1 and locked by T1, can T2 call resource.C() midway without problems?
  • Yes, it could be run resource.C() at any time since it does nothing with the lock.
  • Thanks for such a great answer. "However, if the calls were made by the same thread (e.g. if a() called b()) then the 2nd call would not be blocked. (That is what "reentrant" means in this context.)" -- I am a little confused here. So saying T1 called resource.A() and T1 acquired the lock, if resource.A() calls resource.B() it wont be an issue even though there is a lock.lock() in resource.B() as well? How does that work? Thanks
  • 1) Please respect Java naming conventions. The methods should be called a, b, and c. Not A, B and C.
  • 2) Like I said, this is what reentrant means. If a thread holds a lock and then calls lock() on the same lock object, the implementation says "go ahead ... you are already holding that lock". Under the hood, it just increments a counter. And decrements it in the corresponding lock.unlock()
  • Just one more question actually. What does object's intrinsic lock mean? I read the docs from Oracle but I am finding it very confusing as a beginner. Is it possible for you to provide a simple explanation? I mean, doesnt T1 now own the intrinsic lock of resource?
  • Nope. The intrinisc lock is the one you lock with synchronized(this) { ... } on (for example) a Resource.