If a class A is having a public static method which is tagged by 'synchronized' keyword, then Is there a possibility to have class level lock?
When there is a lock in such class, Can We instantiate object of that class or it has nothing to do with instantiation?
If a class A is having a public static method which is tagged by 'synchronized' keyword, then Is there a possibility to have class level lock?
Yes, there would be a class level lock on class A.
When there is a lock in such class, Can We instantiate object of that class or it has nothing to do with instantiation?
When there is a lock on a static method, it only affects other synchronized static methods. You can still create a new instance of that class.
Yes, the lock will be maintained on the Class object.
Quoted from Locks In Synchronized Methods
You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. 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 distinct from the lock for any instance of the class.
试图执行同一类的另一个静态同步方法的所有其他线程,或在该Class
实例上同步的任何其他方法,将被阻塞。
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.