您的位置:首页 > 移动开发 > IOS开发

iOS开发多线程-线程间通讯

2015-09-25 23:28 555 查看
一、NSThread 线程间的通讯

- (void)demoAboutNSThread
{
NSLog(@"demoAboutNSThread %@", [NSThread currentThread]);
NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(longTimeOperation) object:nil];
[thread start];
}

- (void)longTimeOperation
{
NSLog(@"longTimeOperation %@", [NSThread currentThread]);
[self performSelectorOnMainThread:@selector(mainThreadOperation) withObject:nil waitUntilDone:NO];
}

- (void)mainThreadOperation
{
NSLog(@"mainThreadOperation %@",[NSThread currentThread]);
}


二、GCD 线程间通讯

- (void)dispatchDemo
{
NSLog(@" start %@",[NSThread currentThread]);

dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@" 耗时从左 %@",[NSThread currentThread]);

dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@" 回到主线程 %@", [NSThread currentThread]);
});

});

NSLog(@"end %@",[NSThread currentThread]);
}


三、NSOperation 线程间的通讯

- (void)demoAboutNSOperation
{
NSOperation * block =  [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"block %@",[NSThread currentThread]);
}];

[self.queue addOperation:block];

[self.queue addOperationWithBlock:^{
NSLog(@"耗时操作 %@",[NSThread currentThread]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@" mainQueue %@",[NSThread currentThread]);
}];
}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: