Java同步静态方法:对象或类上的锁
The Java documentation says:
it is not possible for two invocations of synchronized methods on the same object to interleave.
What does this mean for a static method? Since a static method has no associated object, will the synchronized keyword lock on the class, instead of the object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只是为了向 Oscar 的答案添加一些细节(非常简洁!),Java 语言规范的相关部分是 8.4.3.6,'同步方法':
Just to add a little detail to Oscar's (pleasingly succinct!) answer, the relevant section on the Java Language Specification is 8.4.3.6, 'synchronized Methods':
是的。 :)
Yes. :)
您必须小心的一点(一些程序员通常会陷入该陷阱)是同步静态方法和同步非静态方法之间没有链接,即:
Main:
Thread 1:
Thread 2:
f() 和 g () 彼此不同步,因此可以完全并发执行。
One point you have to be careful about (several programmers generally fall in that trap) is that there is no link between synchronized static methods and sync'ed non static methods, ie:
Main:
Thread 1:
Thread 2:
f() and g() are not synchronized with each other and thus can execute totally concurrently.
除非您按如下方式实现 g():
当我想在对象的不同实例之间实现互斥时(例如,访问外部资源时需要),我发现此模式也很有用。
Unless you implement g() as follows:
I find this pattern useful also when I want to implement mutual exclusion between different instances of the object (which is needed when accesing an external resource, for example).
查看有关 内在锁和同步 的 Oracle 文档页面
Have a look at oracle documentation page on Intrinsic Locks and Synchronization
静态方法也有一个关联的对象。 属于JDK工具包中的Class.class文件。 当 .class 文件加载到内存中时,Class.class 创建它的一个实例,称为模板对象。
例如:- 当您尝试从现有客户类创建对象(例如
将 Customer.class 加载到 RAM 中)时。 此时,JDK 工具包中的 Class.class 创建了一个名为 Template 对象的对象,并将该 Customer.class 加载到该模板对象中。该 Customer.class 的静态成员成为该模板对象中的属性和方法。
所以静态方法或属性也有一个对象
A static method also has an associated object. It belongs to Class.class file in JDK toolkit. When the .class file load into the ram, the Class.class creates a instance of it called template object.
Eg :- when you try to create object from existing customer class like
The Customer.class load into RAM. In that moment Class.class in JDK toolkit creates an Object called Template object and load that Customer.class into that template object.Static members of that Customer.class become attributes and methods in that template object.
So a static method or attribute also has an object
下面的示例使类锁和对象锁之间的关系更加清晰,希望下面的示例对其他人也有帮助:)
例如,我们有以下方法,一个获取类锁,另一个获取对象锁:
所以,现在我们可以有以下场景:
当线程时使用相同对象尝试同时访问
objLock
ORstaticLock
方法(即两个线程都尝试访问相同方法)<前><代码>线程-0 0
线程-0 1
线程0 2
线程-0 3
线程-0 4
线程 1 0
线程 1 1
线程 1 2
线程 1 3
线程 1 4
当使用相同对象的线程尝试同时访问
staticLock
和objLock
方法时(尝试访问不同的方法)<前><代码>线程-0 0
线程 1 0
线程-0 1
线程 1 1
线程0 2
线程 1 2
线程 1 3
线程-0 3
线程-0 4
线程 1 4
staticLock
方法<前><代码>线程-0 0
线程-0 1
线程0 2
线程-0 3
线程-0 4
线程 1 0
线程 1 1
线程 1 2
线程 1 3
线程 1 4
当使用不同对象的线程尝试访问时
objLock
方法<前><代码>线程-0 0
线程 1 0
线程-0 1
线程 1 1
线程0 2
线程 1 2
线程 1 3
线程-0 3
线程-0 4
线程 1 4
Below examples gives more clarity between class and object lock, hope below example will help others as well :)
For example we have below methods one acquire class and other acquire object lock :
So, now we can have following scenarios :
When threads using same Object tries to access
objLock
ORstaticLock
method same time (i.e. both threads are trying to access same method)When threads using same Object tries to access
staticLock
andobjLock
methods same time (tries accessing different methods)When threads using different Object tries to access
staticLock
methodWhen threads using different Object tries to access
objLock
method来源: https://javarevisited.blogspot.com/ 2012/03/mixing-static-and-non-static.html
Source: https://javarevisited.blogspot.com/2012/03/mixing-static-and-non-static.html