UI 实现多线程方式之三 NSOperation+NSOperationQueue
2015-10-05 21:01
423 查看
#pragma mark 方式三 NSOperation
//第一种 NSInvocationOperation,单独使用必须手动开启线程。
NSInvocationOperation *invocation = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(subThread3)
object:nil];
[invocation
start];
//第二种 NSBlockOperation,单独使用必须用手动开启线程。
__weak
typeof(self) temp =
self;
NSBlockOperation *block = [NSBlockOperation
blockOperationWithBlock:^{
[temp
subThread4];
}];
[block
start];
====================================
//第三种 和NSOperationQueue结合使用
NSInvocationOperation *invocation1 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(subThread3)
object:nil];
//一定要记得 block内部会持有
对象,需要在外面 用 __weak
修饰。
__weak
typeof(self) temp =
self;
NSBlockOperation *block1 = [NSBlockOperation
blockOperationWithBlock:^{
[temp
subThread4];
}];
//可以通过NSOperationQueue创建队列,向队列中添加操作。完成多任务同时执行。
NSOperationQueue *queue = [[NSOperationQueue
alloc]init];
#warning 注意:一定要提前设置好并发数--最大并发数(同时最多只有两个线程分享处理器)
[queue setMaxConcurrentOperationCount:2];
//添加进队列中
[queue
addOperation:invocation1];
[queue
addOperation:block1];
==========================================
#pragma mark 子线程
-(void)subThread3
{
@autoreleasepool {
NSLog(@"子线程3");
}
}
-(void)subThread4
{
@autoreleasepool {
NSLog(@"子线程4");
}
}
//第一种 NSInvocationOperation,单独使用必须手动开启线程。
NSInvocationOperation *invocation = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(subThread3)
object:nil];
[invocation
start];
//第二种 NSBlockOperation,单独使用必须用手动开启线程。
__weak
typeof(self) temp =
self;
NSBlockOperation *block = [NSBlockOperation
blockOperationWithBlock:^{
[temp
subThread4];
}];
[block
start];
====================================
//第三种 和NSOperationQueue结合使用
NSInvocationOperation *invocation1 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(subThread3)
object:nil];
//一定要记得 block内部会持有
对象,需要在外面 用 __weak
修饰。
__weak
typeof(self) temp =
self;
NSBlockOperation *block1 = [NSBlockOperation
blockOperationWithBlock:^{
[temp
subThread4];
}];
//可以通过NSOperationQueue创建队列,向队列中添加操作。完成多任务同时执行。
NSOperationQueue *queue = [[NSOperationQueue
alloc]init];
#warning 注意:一定要提前设置好并发数--最大并发数(同时最多只有两个线程分享处理器)
[queue setMaxConcurrentOperationCount:2];
//添加进队列中
[queue
addOperation:invocation1];
[queue
addOperation:block1];
==========================================
#pragma mark 子线程
-(void)subThread3
{
@autoreleasepool {
NSLog(@"子线程3");
}
}
-(void)subThread4
{
@autoreleasepool {
NSLog(@"子线程4");
}
}
相关文章推荐
- UI 实现多线程方式之二 NSThread
- UIPickerView和UIDatePicker
- Ui 实现多线程的四种方式
- UI 多线程——种类
- UITableView的优化问题
- android UI界面设计(1)
- UVA 11995 I Can Guess the Data Structure!
- iOS编程--------UIImagePickerController
- iOS编程------自定义UITableViewCell / cell自适应高度
- Servlet的学习之Request请求对象(3)
- iOS编程-------UITableViewEdit_move -----> UITableView 编辑
- iOS编程------- UITableViewController
- iOS编程-------UITableView表视图 / UITableViewCell的重用机制
- 随波逐流之IOS UIslider详解
- iOS编程-------UINavigationController / 界面间通信(传值)
- hdu Beauty of Sequence (好题_集合问题)
- iOS编程-------UIScrollView / UIPageControl
- setuid,setgid,sticky
- UI常用控件
- iOS编程--------UIControl的作用及其子类UISegmentedControl的使用 / UISlider的使用 / UIImageView的使用