简体   繁体   中英

How JVM determines instance when using intrinsic lock?

Example:

Object lock = createLock();
synchronized (lock) {
    //some statements
}

I have some lock instance, but don't know is it singleton instance for all application or only local, for this method?

How can I determine this, without finding the place where this object created and how JVM determines this for different threads?

You can only determine the scope of the lock by inspection of the code it comes from. The JVM does not need to determine anything about the object, it merely does what it is told and uses the provided object as the lock token. If it is not a singleton then the locking mechanism fails.

docs.oracle.com/javase/tutorial/essential/concurrency/… "Internal" mean just internal object which we use in synchronized statements

This is talking about the lock built in to the header of every object. If you have an Object instance, you have one of these locks.

Primitives don't have such a lock.

java.util.concurrent.Lock has a non-intrinsic lock, though confusingly it has an intrinsic on as wel..

The variable, lock, in your example is just that: It's a variable.

A synchronized statement locks the lock of the object that it's given. In your example, it locks the object to which your lock variable refers.

So, does your lock variable refer to a singleton? Is it a local variable? Is it an instance variable in some class? We can't answer those questions because you haven't shown us the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM