RunLoop 和 线程加锁 atomic 的特性
2015-11-03 16:30
281 查看
#import "CustomOperation.h"
@implementation CustomOperation
{
NSMutableData *_data;
BOOL _isFinished;
}
//定制的Opration类的入口方法
- (void)main
{
_isFinished =
NO;
NSLog(@"CustomOpration run");
_data = [[NSMutableDataalloc]init];
NSURL *url = [NSURLURLWithString:_strUrl];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
[NSURLConnectionconnectionWithRequest:requestdelegate:self];
//NSURLconnection类的对象会在发起http请求的同时会向当前线程的runloop中添加一个数据源,再请求完成(成功或失败)时移除输入源。当收到服务器的响应,收到发来的数据、请求完成(成功或失败)时其对应的输入源就有事件要处理
while (!_isFinished) {
//输入源:时iOS中的一种事件处理机制,iOS中的很多东西都用到了输入源,比如按钮,定时器,URLConnection等等。
//每个线程都有一个runloop,其作用就是循环检测并处理输入源的数件
//runUnitDate:这个方法就是执行上述操作直到某个特定时间,这里我们把时间写成了distantFuture表示一直执行上述操作
//当runloop没有任何数据源,runUnitDate:返回
[[NSRunLoopcurrentRunLoop]runUntilDate:[NSDatedistantFuture]];
}
[_targetperformSelectorOnMainThread:_actionwithObject:_datawaitUntilDone:NO];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading");
_isFinished =
YES;
}
// 加锁
@synchronized( /** 要上锁的对象 */ ) {
// 要上锁的代码
}
例如
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
@synchronized(self) {
// A 代码
NSData *data = [@"你好 我来了" dataUsingEncoding:NSUTF8StringEncoding];
[_outputStream write:data.bytes maxLength:data.length];
}
}
atomic 是线程安全的 如果用了这个修饰符修饰属性 那么会在这个属性的 setter 方法中自动加锁,但是还是要少用的 因为会耗费 CPU 资源 而noatomic 修饰的属性 不会给 setter 方法加锁,节省 CPU 资源
以上代码表示在点击屏幕时候,当有一条线程访问 A 代码时候就会给 A 代码加锁 直到线程执行完成,才会去掉 A 代码的锁,其他线程才可以访问
@implementation CustomOperation
{
NSMutableData *_data;
BOOL _isFinished;
}
//定制的Opration类的入口方法
- (void)main
{
_isFinished =
NO;
NSLog(@"CustomOpration run");
_data = [[NSMutableDataalloc]init];
NSURL *url = [NSURLURLWithString:_strUrl];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
[NSURLConnectionconnectionWithRequest:requestdelegate:self];
//NSURLconnection类的对象会在发起http请求的同时会向当前线程的runloop中添加一个数据源,再请求完成(成功或失败)时移除输入源。当收到服务器的响应,收到发来的数据、请求完成(成功或失败)时其对应的输入源就有事件要处理
while (!_isFinished) {
//输入源:时iOS中的一种事件处理机制,iOS中的很多东西都用到了输入源,比如按钮,定时器,URLConnection等等。
//每个线程都有一个runloop,其作用就是循环检测并处理输入源的数件
//runUnitDate:这个方法就是执行上述操作直到某个特定时间,这里我们把时间写成了distantFuture表示一直执行上述操作
//当runloop没有任何数据源,runUnitDate:返回
[[NSRunLoopcurrentRunLoop]runUntilDate:[NSDatedistantFuture]];
}
[_targetperformSelectorOnMainThread:_actionwithObject:_datawaitUntilDone:NO];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading");
_isFinished =
YES;
}
// 加锁
@synchronized( /** 要上锁的对象 */ ) {
// 要上锁的代码
}
例如
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
@synchronized(self) {
// A 代码
NSData *data = [@"你好 我来了" dataUsingEncoding:NSUTF8StringEncoding];
[_outputStream write:data.bytes maxLength:data.length];
}
}
atomic 是线程安全的 如果用了这个修饰符修饰属性 那么会在这个属性的 setter 方法中自动加锁,但是还是要少用的 因为会耗费 CPU 资源 而noatomic 修饰的属性 不会给 setter 方法加锁,节省 CPU 资源
以上代码表示在点击屏幕时候,当有一条线程访问 A 代码时候就会给 A 代码加锁 直到线程执行完成,才会去掉 A 代码的锁,其他线程才可以访问
相关文章推荐
- 重新定位svn地址的方法(windows和linux),svn switch(sw)的帮助信息
- App 开发:Hybrid 架构下的 HTML5 应用加速方案
- 如何理解ip路由和操作linux的路由表
- 【政府网站普查】政府网站的理想很丰满,现实却很骨感
- opencv由Mat相乘想到的几个问题
- 使用apache ab对nginx+varnish+tomcat进行压力测试的结果报告
- linux-flock文件锁之实际运用
- Nginx学习之六-nginx核心进程模型
- opencart 实现全面伪静态
- kafka常用运维命令
- 在linux下新增一块硬盘的操作。(包含大于2T的硬盘在linux下挂载操作)
- CentOS中操作Psql
- linux中的文件锁flock
- linux查看java jdk安装路径和设置环境变量
- linux ftp上传文件用java代码实现
- Centos6.5使用Siege压力测试
- 机顶盒开发 笔记
- ecshop出现警告:Strict standards: Redefining already defined constructor for class
- Linux 主机root登陆以后远程SSH才能访问
- How to Setup OpenStack to use Local Disks for Instances