1.NSthead使用:
创建方式1:
NSThread *thead = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"第一种"];
thead.name = @"one";//线程名称
[thead start];//开启线程
创建方式2:
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"第二种"];
创建方式3:
[self performSelectorInBackground:@selector(run:) withObject:@"第三种"];
run方法:
-(void)run:(NSThread *)sender{
NSLog(@"--%@---%@",sender.observationInfo,[NSThread currentThread]);
for (int i=0; i<100000; i++) {
if(self.thead.isCancelled){//调用cancel方法设置为yes
NSLog(@"NShread exit:%d",i);
[NSThread exit];//类方法 只能在线程里面运行在主线程运行会阻挡主线程代码的运行
}else
NSLog(@"NShread:%d",i);
}
}
关闭NSThead线程:
[thead cancel];//此方法设置thead.isCancelled为yes,所以可在线程里面执行thead.isCancelled来做相对应处理方法
2.NSOperationQueue使用
-(void)queueOperation{
NSOperationQueue *queue = [[NSOperationQueue alloc]init];//创建线程
NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];//任务1
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"downLoad2--%@",[NSThread currentThread]);
}];//任务2
[op2 addExecutionBlock:^{
NSLog(@"downLoad3--%@",[NSThread currentThread]);
}];
CustomNSOperation *op3 = [[CustomNSOperation alloc]init];//任务三
[queue addOperation:op1];//自动掉用start
[queue addOperation:op2];
[queue addOperation:op3];
}
CustomNSOperation类
CustomNSOperation类继承NSOperation,在.m文件里面调用main方法。如下例子:
#import
@interface CustomNSOperation : NSOperation //.h
@end
#import "CustomNSOperation.h"//.m
@implementation CustomNSOperation
-(void)main{
NSLog(@"自定义---%@",[NSThread currentThread]);
}
@end
3. CGD使用 最常用的有6中方法:(如下)
// !!!: 同步+主队列
-(void)syncMain{
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"sync ---begin");
//将任务加入队列
dispatch_sync(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
NSLog(@"sync ---end");
}
// TODO:异步+主队列
-(void)asynMain{
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"async( ---begin");
dispatch_async(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
NSLog(@"async( ---end");
}
// FIXME:同步+串行队列:不会开启新的线程,在当前线程执行任务
-(void)syncSerial{
dispatch_queue_t queue = dispatch_queue_create("com.520.queue", DISPATCH_QUEUE_SERIAL);
//创建串行队列
dispatch_sync(queue, ^{
NSLog(@"sync Serial---begin");
//将任务加入队列
dispatch_sync(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
NSLog(@"sync ---end");
NSLog(@"sync Serial---end");
});
}
// ???:异步+串行队列:会开启新的线程,但是任务是串行的,执行一个任务,在执行下一个任务
-(void)asyncSerial{
dispatch_queue_t queue = dispatch_queue_create("com.520.queue", DISPATCH_QUEUE_SERIAL);
NSLog(@"async(Serial ---begin");
dispatch_async(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
NSLog(@"async( Serial---end");
}
// !!!:同步+并发队列 不会开启线程
-(void)syncConcurrent{
//获取全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"async(Concurrent ---begin");
dispatch_async(queue, ^{
NSLog(@"1 Concurrent---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2 Concurrent---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3 Concurrent---%@",[NSThread currentThread]);
});
NSLog(@"async( Concurrent---end");
}
// !!!:异步+并发队列 可以同时开启多条线程
-(void)asycConcurrent{
//创建一个队列
//第一个参数是标签等同于名字
//第二个参数传串行还是并行
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//将任务添加到队列
dispatch_async(queue, ^{
for (int i=0; i<10000; i++) {
NSLog(@"1 Concurrent---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i=0; i<100000; i++) {
NSLog(@"2 Concurrent---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i=0; i<100000; i++) {
NSLog(@"3 Concurrent---%@",[NSThread currentThread]);
}
});
}
以上是ios三种线程使用方式,当线程运行后如何调用刷新主线程UI。调用此方法:[self performSelectorOnMainThread: withObject: waitUntilDone:];//刷新主线程
例子:
以NSthead为列:
-(void)createThead1{
self.thead = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"第一种"];
self.thead.name = @"one";
[self.thead start];
}
-(void)run:(NSThread *)sender{
NSLog(@"--%@---%@",sender.observationInfo,[NSThread currentThread]);
for (int i=0; i<100000; i++) {
if(self.thead.isCancelled){
NSLog(@"NShread exit:%d",i);
[self performSelectorOnMainThread:@selector(updateUI:) withObject:nil waitUntilDone:YES];//刷新主线程
[NSThread exit];//类方法
}else
NSLog(@"NShread:%d",i);
}
}
-(void)updateUI:(NSThread *)sender{
NSLog(@"刷新主线程UI");
}
- (IBAction)btnThead1:(id)sender {
[self.thead cancel];
}
当btnThead1:这个方法时,设置了self.thead.isCancelled=YES,在线程里面就运行self.thead.isCancelled里面的方法。
注意:只有当线程里面的任务完成才能运行dealloc()方法。
4. 三种线程的优缺点
NSThread:
优点:NSThread 比其他两个轻量级
缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销。
NSOperation
优点:不需要关心线程管理,数据同步的事情,可以把精力放在自己需要执行的操作上。
NSOperationQueue
operation 相关的类是 NSOperation ,
NSOperation是个抽象类,使用它必须用它的子类,可以实现它或者使用它定义好的两个子类:NSInvocationOperation 和 NSBlockOperation。
创建NSOperation子类的对象,把对象添加到NSOperationQueue队列里执行。
GCD
Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法。在iOS4.0开始之后才能使用。GCD是一个替代诸如NSThread, NSOperationQueue, NSInvocationOperation等技术的很高效和强大的技术。现在的iOS系统都升级到7了,所以不用担心该技术不能使用