+(void)initialize 是否执行任何线程锁定?
查看“initialize”的定义:
+ (void)initialize
讨论
运行时将初始化发送到程序中的每个类,恰好在该类或继承自该类的任何类第一次发送之前来自程序内部的消息。 (因此,如果不使用该类,则该方法可能永远不会被调用。)运行时以线程安全的方式将初始化消息发送到类。超类在其子类之前接收此消息。
它指出,initialize 是以“线程安全方式”发送的。在幕后,运行时是创建一个锁来使该调用线程安全,还是仅从运行时的工作方式来看它本质上是线程安全的?如果它确实锁定,这是否意味着如果您实现 +initialize ,运行时会创建一个本来不会创建的锁?
Looking at the definition for "initialize":
+ (void)initialize
Discussion
The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.
it is stated that initialize is sent in a "thread-safe manner". Under the covers, is the runtime creating a lock to make that call thread safe or is it inherently thread-safe just from the way the runtime works? If it does lock, does that mean if you implement +initialize the runtime creates a lock it would not otherwise have created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法回答它是否锁定,但无论你是否实现
+initialize
,它仍然会被调用。默认实现可能会执行某些操作,但仍然会被调用。因此,如果运行时确实锁定,那么无论该方法是否由您的子类实现,都会创建锁定。I can't answer whether or not it locks, but regardless of whether or not you implement
+initialize
, it's still called. The default implementation may do something, but it's still called. So if the runtime does lock, then the lock is created regardless of whether or not the method is implemented by your subclass.