gpt4 book ai didi

java - Java中的静态和非静态同步

转载 作者:行者123 更新时间:2023-12-01 21:59:26 25 4
gpt4 key购买 nike

this question中有这样的说法那个

there is no link between synchronized static methods and sync'ed non static methods

好吧,这很简单,但是如果非静态方法中的代码调用静态方法怎么办?该线程是否会同时持有静态(与类关联)和非静态(与类实例关联)监视器?

最佳答案

Will this thread hold both static(associated with class) and non-static (associated with an instance of a class) monitors?

是的,那是因为,

as per Java Language Specification 8.4.3.6,

For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.

毕竟 2 个监视器将被单独定义。

关于java - Java中的静态和非静态同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33823496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com