iOS NSThread和NSOperation的基本使用详解
NSThread适合简单的耗时任务的执行,它有两种执行方法
-(void)oneClick{
[NSThreaddetachNewThreadSelector:@selector(doSomething:)toTarget:selfwithObject:@"oneClick"];
}
-(void)doSomething:(NSString*)str{
NSLog(@"%@",str);
}
-(void)twoClick{
NSThread*myThread=[[NSThreadalloc]initWithTarget:self
selector:@selector(doSomething:)
object:@"twoClick"];
[myThreadstart];
}
NSOperation适合需要复杂的线程调度的方法,然后它默认是使用主线程不会创建子线程
-(void)threeClick{
//1.创建NSInvocationOperation对象
NSInvocationOperation*op=[[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(run)object:nil];
//2.调用start方法开始执行操作
[opstart];
}
-(void)run
{
NSLog(@"------%@",[NSThreadcurrentThread]);
}
-(void)fourClick{
NSBlockOperation*op=[NSBlockOperationblockOperationWithBlock:^{
//在主线程
NSLog(@"1------%@",[NSThreadcurrentThread]);
}];
//添加额外的任务(在子线程执行)
[opaddExecutionBlock:^{
NSLog(@"2------%@",[NSThreadcurrentThread]);
}];
[opaddExecutionBlock:^{
NSLog(@"3------%@",[NSThreadcurrentThread]);
}];
[opaddExecutionBlock:^{
NSLog(@"4------%@",[NSThreadcurrentThread]);
}];
[opstart];
}
以上这篇iOSNSThread和NSOperation的基本使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
4 狗狗请人训练
5 训练狗狗迎宾
6 训练狗狗解放
7 抬头训练狗狗
10 训练狗狗的基本动作
11 两个月狗狗怎么训练大小便
12 教狗狗训练
13 记叙文训练狗狗
14 潮州训练狗狗
15 训练狗狗叫的方法
16 猫训练狗狗
17 狗狗训练如厕
18 狗狗来训练