Concurrency in Java: synchronized static methods

前端 未结 6 1048
礼貌的吻别
礼貌的吻别 2020-12-09 21:05

I want to understand how locking is done on static methods in Java.

let\'s say I have the following class:

class Foo {
    private static int bar = 0         


        
6条回答
  •  暖寄归人
    2020-12-09 22:07

    If you read http://download.oracle.com/javase/tutorial/essential/concurrency/locksync.html.

    It will tell you:

    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.

    which tells you all you need to know.

提交回复
热议问题