- NSObject —- 不显示创建线程的方法
[self performSelectorInBackground:@selector(doSomething) withObject:nil];
实现doSomething方法:
- (void)doSomething
{
NSLog(@"%@",[NSThread currentThread]);
}
- NSThread
// 两种开启方式
//1. 实例方法: 先创建线程对象,然后再手动开启线程运行,在线程运行之前可以设置线程的优先级信息
NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething) object:nil];
// 设置名字
[thread setName:@"thread"];
// 设置优先级0-1
[thread setThreadPriority:0.1];
// NSThread需要手动开启线程
[thread start];
//打印信息:
//2015-11-03 16:13:06.985 多线程[8026:1165477] <NSThread: 0x7fc8f500eb00>{number = 2, name = thread}
//2.类方法: 直接创建线程并开始运行线程
[NSThread detachNewThreadSelector:@selector(doSomething) toTarget:self withObject:nil];
//打印信息:
//2015-11-03 16:13:06.985 多线程[8026:1165478] <NSThread: 0x7fc8f2e029d0>{number = 3, name = (null)}
3.NSOperation
(1)NSOperation在MVC里面属于M层,用来封装单个任务的数据和相关代码的抽象类,我们一般使用它的子类NSInvocationOperation 和 NSBlockOperation.
(2)NSOperation本身并无主线程,子线程之分,只是一个操作,通常与NSOperationQueue结合使用.
(3)NSOperationQueue是线程操作队列,用来管理一组NSOperationQueue,会根据需求创建出合适数量的子线程,完成任务的并发执行.
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
// 1.NSInvocationOperation
NSInvocationOperation * invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
// 添加到队列中
[queue addOperation:invocation];
NSLog(@"%@",[NSThread currentThread]);
//打印信息:
//2015-11-03 16:24:05.446 多线程[8109:1177674] <NSThread: 0x7f9d424022d0>{number = 1, name = main}
// 2.NSBlockOperation
NSBlockOperation * block = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"---%@",[NSThread currentThread]);
}];
// 添加到队列中
[queue addOperation:block];
//打印信息:
//2015-11-03 16:24:52.821 多线程[8123:1179037] <NSThread: 0x7fd6e34141f0>{number = 2, name = (null)}
// 队列里面可以加入多个NSOperation,可以把NSOperationQueue线程池,可往线程池中添加操作(NSOperation)到队列中,线程池中的线程也可看做是消费者,从队列中取走操作,并执行它们.
// 线程池中的线程数,也就是线程操作数,默认情况下是-1,-1表示没有限制,这样会同时运行队列中的全部操作.
//设置最大并发执行数,当数量为1的时候,线程编程同步执行(串行)
[queue setMaxConcurrentOperationCount:1];
// 添加依赖关系
[invocation addDependency:block];
// 取消操作
[invocation cancel];
[block cancel];
// 整个队列取消操作
[queue cancelAllOperations];
// 暂停
[queue setSuspended:YES];
// 开启
[queue setSuspended:NO];
在storyboard中拖一个imageView,并添加为IBOutlet属性.
#define kUrl @"http://img.hb.aicdn.com/1f2acbecdc6ac6187a9b02bffe4a5dedc9fe45ff15a44-D6LjC1_fw580"
// 下载图片操作
- (void)downloadImage
{
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kUrl]];
UIImage * img = [UIImage imageWithData:data];
if (img == nil) {
return;
}else{
// 线程下载完通知主线程更新界面
[self performSelectorOnMainThread:@selector(updateUI:) withObject:img waitUntilDone:YES];
}
}
- (void)updateUI:(UIImage *)image
{
self.myImgView.image = image;
}
4.GCD
(1)常用的方法 dispatch_async
// 去子线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 耗时操作
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kUrl]];
UIImage * img = [UIImage imageWithData:data];
NSLog(@"%@",[NSThread currentThread]);
if (data != nil) {
// 回主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 更新界面
self.myImgView.image = img;
NSLog(@"%@",[NSThread currentThread]);
});
}
});
//打印信息:
//2015-11-03 16:37:11.781 多线程[8215:1194416] <NSThread: 0x7ff5017a5e20>{number = 3, name = (null)}
//2015-11-03 16:37:11.782 多线程[8215:1194367] <NSThread: 0x7ff501700760>{number = 1, name = main}
(2)dispatch_group_async(异步加载的方法)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"group1");
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"group2");
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"group3");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"update");
});
//打印信息:
//2015-11-03 16:40:16.170 多线程[8244:1198075] group1
//2015-11-03 16:40:17.167 多线程[8244:1198074] group2
//2015-11-03 16:40:18.167 多线程[8244:1198076] group3
//2015-11-03 16:40:18.168 多线程[8244:1198076] update
(3)dispatch_barrier_async
// dispatch_barrier_async是在前面的任务执行结束后它才结束,而且它后面的任务等它执行完成之后才会执行
dispatch_queue_t queue1 = dispatch_queue_create("djcnsk", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue1, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"dispatch_async1");
});
dispatch_async(queue1, ^{
[NSThread sleepForTimeInterval:4];
NSLog(@"dispatch_async2");
});
dispatch_barrier_async(queue1, ^{
NSLog(@"dispatch_barrier_async");
[NSThread sleepForTimeInterval:2];
});
dispatch_async(queue1, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"dispatch_async3");
});
// 打印信息:
//2015-11-03 16:43:37.991 多线程[8304:1201942] dispatch_async1
//2015-11-03 16:43:39.994 多线程[8304:1201941] dispatch_async2
//2015-11-03 16:43:39.994 多线程[8304:1201941] dispatch_barrier_async
//2015-11-03 16:43:43.001 多线程[8304:1201941] dispatch_async3
(4)dispatch_apply
// 执行某个代码片段N次
dispatch_apply(5, queue1, ^(size_t index) {
NSLog(@"5555");
});