关于@synchronized(self)的用法
来源:互联网 发布:河南省大数据谷是哪里 编辑:程序博客网 时间:2024/06/02 07:30
# import "NetworkManager.h"
static NetworkManager *network = nil;
@implementation NetworkManager
+ (NetworkManager *)getNetworkInstance{
@synchronized(self){
if (nil == network){
network = [[NetworkManager alloc] init];
{
}
return network;
}
英文文档:
Using the @synchronized Directive(指令)
The @synchronized directive is a convenient way to create mutex locks(互斥锁) on the fly inObjective-C code. The @synchronized directive does what any other mutex lock woulddo—it prevents(预防) different threads from acquiring the same lock at the same time. Inthis case, however, you do not have to create the mutex or lock object directly. Instead, you simply use any Objective-C object as a lock token with @synchronized。
The object passed to the @synchronized directive is a unique identifier used to distinguish theprotected block. If you execute the preceding method in two different threads, passing a different objectfor the anObj parameter on each thread, each would take its lock and continue processing without being blocked by the other. If you pass the same object in both cases, however, one of the threads would acquire the lock first and the other would block until the first thread completed the critical section.
As a precautionary measure, the @synchronized block implicitly adds an exception handler to theprotected code. This handler automatically releases the mutex in the event that an exception is thrown.This means that in order to use the @synchronized directive, you must also enable Objective-C exception handling in your code. If you do not want the additional overhead caused by the implicitexception handler, you should consider using the lock classes.
For more information about the @synchronized directive, see The Objective-C ProgrammingLanguage.
- 关于@synchronized(self)的用法
- 关于@synchronized(self)的用法
- 关于@synchronized(self)的用法
- 关于@synchronized(self)的用法
- 关于@synchronized(self)的用法
- 关于@synchronized(self)的用法
- 关于@synchronized(self)的用法
- 关于@synchronized(self)的用法
- @synchronized(self)的用法
- 关于 @synchronized(self) 的理解
- @synchronized(self)的涵义
- @synchronized(self) 的作用
- @synchronized(self)的作用
- iphone关于self.用法的一些总结
- 关于self.用法的一些总结
- iphone关于self.用法的一些总结
- 关于self.用法的一些总结
- 关于 self.用法的一些总结
- Java集合类详解
- hibernate hbm.xml 配置文件中的主键生成策略
- winsocket 之收发数据出问题
- JS微信打飞机游戏(四)
- C#中使Label控件中的文字实现从左到右滚动效果
- 关于@synchronized(self)的用法
- ubuntu搭建可匿名访问ftp服务器
- 数据库脚本
- Java自定义日志输出文件
- Linux系统中的关机命令
- CSS实例(七):工具条效果
- LA4256 离散+超级汇点 通过连边转移状态
- fatal error C1189: #error : This file requires _WIN32_WINNT to be #defined a
- 异步编程设计模式