您的位置:首页 > 产品设计 > UI/UE

iOS 中的线程(1)---①performSelectorInBackground②NSThread③NSOperation 和 NSOperationQueue 的组合

2015-11-23 20:21 597 查看
iOS里面共有四种实现多线程的方式

一、第一种实现多线程的方式:

-----------------------------------------NSObject 自带了 performSelectorInBackground

二、第二种实现多线程的方式:

-----------------------------------------NSThread

       其中,NSThread 有2种方式

①第一种创建 NSThread 的方式

NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(jisuanNumber) object:nil];
//必须手动开启线程
[thread start];
这里有2个方法:

//获取当前线程
[NSThread currentThread];
//判断一个线程是否是主线程
[NSThread isMainThread];

NSLog(@" viewDidLoad  %@  %d",[NSThread currentThread], [NSThread isMainThread]);


②第二种创建 NSThread 的方式

[NSThread detachNewThreadSelector:@selector(jisuanNumber) toTarget:self withObject:nil];

#pragma mark 计算按钮的点击事件
- (IBAction)calculteButtonDidClicked:(id)sender {

[self performSelectorInBackground:@selector(jisuanNumber) withObject:nil];

}

-(void)jisuanNumber {

@autoreleasepool {
NSLog(@"%@  %d",[NSThread currentThread], [NSThread isMainThread]);

NSInteger sum = 0;
for (NSInteger i = 0; i < 100000000; i++) {
sum += i;
}
//从子线程回到主线程
[self performSelectorOnMainThread:@selector(result:) withObject:@(sum) waitUntilDone:YES];
}

}

-(void)result:(NSNumber *)sum{

NSLog(@"%@",sum);
}
注意:在子线程里面一定要自己写 自动释放池 autoreleasepool(最简单最轻量级的)(他是隐式的创建)

三、第三种实现多线程的方式:

-----------------------------------------NSOperation 和 NSOperationQueue 的组合

1.注意:NSOperation 和 NSOperationQueue 本省并不是多线程。

2.NSOperation---任务块

NSOperationQueue --- 任务队列

3.实现多线程的方式是:把若干个任务块添加到任务队列,这个时候操作系统会自己创建合适数量的 Thread(线程) 来完成队列里的任务块

4.队列里面的任务块会按照队列的顺序一一开始,但是每个任务块结束的时间并不一定(可能最后开始的任务最先完成)

5.NSOperation 是一个抽象类,一般使用它的子类,iOS 里面自带了2个它的子类,一种是 target 思想的,一种是 block 思想的

6.操作系统创建线程的数量取决于:①队列里面任务的数量   ②CPU的使用情况  ③内存的使用情况  ④其他的资源

NSOperation 第一个子类--NSInvocationOperation

-(void)viewDidLoad{
[super viewDidLoad];

NSInvocationOperation *ope1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(ope1Action) object:nil];
NSInvocationOperation *ope2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(ope2Action) object:nil];
NSInvocationOperation *ope3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(ope3Action) object:nil];
NSInvocationOperation *ope4 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(ope4Action) object:nil];
NSInvocationOperation *ope5 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(ope5Action) object:nil];
创建任务队列

//方式①
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//设置任务块的最大并发数(串行队列)
queue.maxConcurrentOperationCount = 1;

//方式②
//这种方式特点:队列里面的任务会在主线程 串行完成
NSOperationQueue *queue = [NSOperationQueue mainQueue];

//把任务块添加到任务队列
[queue addOperation:ope1];
[queue addOperation:ope2];
[queue addOperation:ope3];
[queue addOperation:ope4];
[queue addOperation:ope5];


NSOperation 第二个子类--NSBlockOperation

NSBlockOperation *blockope4 = [NSBlockOperation blockOperationWithBlock:^{
//写具体完成的任务
NSLog(@"任务四 %@  %d",[NSThread currentThread],[NSThread isMainThread]);

}];
NSBlockOperation *blockope5 = [NSBlockOperation blockOperationWithBlock:^{
//写具体完成的任务
NSLog(@"任务五 %@  %d",[NSThread currentThread],[NSThread isMainThread]);

}];

//任务列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperation:blockope1];
[queue addOperation:blockope2];
[queue addOperation:blockope3];
[queue addOperation:blockope4];
[queue addOperation:blockope5];

}

-(void)ope1Action{
//任务计算
NSLog(@"任务1 %@  %d",[NSThread currentThread],[NSThread isMainThread]);

}
-(void)ope2Action{
//任务计算
NSLog(@"任务2 %@  %d",[NSThread currentThread],[NSThread isMainThread]);

}
-(void)ope3Action{
//任务计算
NSLog(@"任务3 %@  %d",[NSThread currentThread],[NSThread isMainThread]);

}
-(void)ope4Action{
//任务计算
NSLog(@"任务4 %@  %d",[NSThread currentThread],[NSThread isMainThread]);

}
-(void)ope5Action{
//任务计算
NSLog(@"任务5 %@  %d",[NSThread currentThread],[NSThread isMainThread]);

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