• NSThread
1、两种方式:
。手动开启方式
/*
* 创建手动开启方式
*第三个参数:就是方法选择器选择方法的参数
*/
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread) object:@"thread"];
// 开启线程
[thread start];
。自动开启方式
[NSThread detachNewThreadSelector:@selector(thread1:) toTarget:self withObject:@"thread1"];
2、加载一张图片的步骤:
1、创建一个UIImageView,并放在父视图上
2、创建一个子线程
3、通过url获取网络图片
4、回到主线程
5、在主线程更新UI
图片地址的宏定义:
#define kurl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
[NSThread detachNewThreadSelector:@selector(thread1:) toTarget:self withObject:@"thread1"];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
[self.view addSubview:imageView];
}
//在子线程执行的方法
- (void)thread1:(NSString *)sender{
[NSThread currentThread];//获取到当前所在的信息
NSThread *thread = [NSThread currentThread];
thread.name = @"我是子线程 ";
NSLog(@"%@",thread);
[NSThread isMainThread] // 判断当前线程是否是主线程
BOOL isMainThread = [NSThread isMainThread];
[NSThread isMultiThreaded] //判断是否是多线程
BOOL isMUltiThread = [NSThread isMultiThreaded];
NSLog(@"%d,%d",isMainThread,isMUltiThread);
// 设置线程的优先级(0-1) setThreadPriority:
[NSThread setThreadPriority:1.0];
// sleepForTimeInterval:让线程休眠
[NSThread sleepForTimeInterval:2];
// 从网络加载图片并将它转化为data类型的数据
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kUrl]];
image = [UIImage imageWithData:data];
// waiUntilDone设为YES,意味着UI更新完才会做其它操作
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}
- (void)updateUI:(UIImage *)kimage{
https://github.com/duiyueliu/iOS-Images-Extractor-master.git
imageView.image = kimage;
NSLog(@"updateUI方法所在的线程%@",[NSThread currentThread]);
}
3、加载多张图片跟加载一张图片的步骤一样,就直接上代码了:
- (void)viewDidLoad {
[super viewDidLoad];
imageIndex = 100;
threadArrays = [NSMutableArray array];
// 创建多个UIImageView
for (int row = 0; row<3; row++) {
for (int list = 0; list<2; list++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5+list*205, 5+row*205, 200, 200)];
imageView.backgroundColor = [UIColor grayColor];
imageView.tag = imageIndex++;
[self.view addSubview:imageView];
}
}
// 创建多个子线程
for (int index = 0; index<6; index++) {
// [NSThread detachNewThreadSelector:@selector(thread:) toTarget:self withObject:@(index)];
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread:) object:@(index)];
[thread start];
[threadArrays addObject:thread];
}
}
//通过url加载网络图片
- (void)thread:(NSNumber *)index{
// 通过线程休眠 实现 实现图片的顺序加载
[NSThread sleepForTimeInterval:[index intValue]];
NSThread *thread = [NSThread currentThread];
if (thread.isCancelled == YES) {
[NSThread exit];
}
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kUrl]];
image = [UIImage imageWithData:data];
// 回到主线程
[self performSelectorOnMainThread:@selector(updateUI:) withObject:index waitUntilDone:YES];
}
//
- (void)updateUI:(NSNumber *)index{
UIImageView *imageView = [self.view viewWithTag:[index intValue] + 100];
imageView.image = image;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for (int index = 0; index<6; index++) {
NSThread *thread = threadArrays[index];
if (thread.isFinished == NO) {
// 点击屏幕 取消未完成的线程
[thread cancel];
}
}
[NSThread currentThread];
NSLog(@"%@",threadArrays);
}
• NSOperation
介绍:采用NSOperation(线程操作,通常用它的子类)和NSOperationQueue(线程队列)搭配来做多线程开发,采用NSOperation指定一个操作,把这个操作放到线程队列(线程池)中,让线程队列安排它的周期。
1、加载一张图片的步骤:
1、创建视图
2、创建线程操作
3、创建线程队列
4、把线程操作放在线程队列中
5、在子线程加载网络资源
6、回到主线程
7、在主线程更新UI
2、三种方式:
方式一:NSInvocationOperation和NSOperationQueue搭配进行多线程开发
图片地址的宏定义:
#define kurl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
// 1、创建视图
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
[self.view addSubview:imageView];
// 2、创建线程操作
NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadResource) object:nil];
// 3、创建线程队列
NSOperationQueue *operationQueue = [NSOperationQueue new];
// 4、把线程操作放在线程队列中
[operationQueue addOperation:invocationOperation];
}
//5、在子线程加载网络资源
- (void)loadResource{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
UIImage *image = [UIImage imageWithData:data];
// 6、回到主线程
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
// 7、在主线程更新UI
imageView.image = image;
}];
}
方式二:NSBlockOperation和NSOperationQueue搭配
- (void)viewDidLoad{
[super viewDidLoad];
// 1、创建视图
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
[self.view addSubview:imageView];
// 2、创建一个线程操作
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
// 5、加载网络资源
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
UIImage *image = [UIImage imageWithData:data];
// 6、回到主线程
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
// 7、更新UI
imageView.image = image;
}];
}];
// 3、创建一个线程队列
NSOperationQueue *operationQueue = [NSOperationQueue new];
// 4、把线程操作放到线程队列中
[operationQueue addOperation:blockOperation];
}
方式三:用自定义于NSOperation的类与NSOperationQueue搭配
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
// 1、创建视图
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
[self.view addSubview:imageView];
// 2、创建一个线程操作,在类中重写main方法,在main指定要进行的操作
CusTomOperation *customOperation = [[CusTomOperation alloc]initWithImageView:imageView];
// 3、创建一个线程队列
NSOperationQueue *operationQueue = [NSOperationQueue new];
// 4、将线程操作放到线程队列中
[operationQueue addOperation:customOperation];
}
自定义的NSOperation类中.m文件中的代码如下:
- (instancetype)initWithImageView:(UIImageView *)imageView
{
self = [super init];
if (self) {
_imageView = imageView;
}
return self;
}
- (void)main{
// 自动创建一个自动释放池,因为在这里无法访问到主线程的自动释放池
@autoreleasepool {
// 5、在子线程加载网络资源
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
UIImage *image = [UIImage imageWithData:data];
// 6、回到主线程
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
// 7、更新UI
_imageView.image = image;
}];
}
}
@end
第三种方式使用时需要注意的两点是:
(1)该子类需重写main方法,在main方法内做线程操作,该线程被执行就会自动调用main方法
(2)在main方法内切记要新建一个自动释放池,因为如果是同步操作,该方法能够自动访问到主线程的自动释放池,如果是异步执行操作,那么将无法访问到主线程的自动释放池。
下面用第二种方式加载多张图片,步骤跟加载一张图片的步骤一样,代码如下:
#define kurl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
@interface MoreImageViewViewController ()
{
int imageIndex;
NSOperationQueue *operationQueue;
}
@end
@implementation MoreImageViewViewController
- (void)viewDidLoad{
[super viewDidLoad];
imageIndex = 100;
// 1、创建多个视图
for (int row = 0; row<3; row++) {
for (int list = 0; list <2; list++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5+list*205, 5+row*205, 200, 200)];
imageView.backgroundColor = [UIColor yellowColor];
imageView.tag = imageIndex++;
[self.view addSubview:imageView];
}
}
// 3、创建线程队列
operationQueue = [NSOperationQueue new];
// 2、创建多个线程
for (int index = 0; index<6; index++) {
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
// 5、在子线程中加载网络资源
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
UIImage *image = [UIImage imageWithData:data];
[NSThread sleepForTimeInterval:0.5];
// 6、回到主线程
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
// 7、更新UI
UIImageView *imageView = [self.view viewWithTag:index+100];
imageView.image = image;
}];
}];
// 让第一个线程谁都不依赖
if (index != 0) {
[blockOperation addDependency:operationQueue.operations[index-1]];
}
// 4、将线程操作放到线程队列中
[operationQueue addOperation:blockOperation];
}
• GCD
1、介绍:全称是Grand Central Dispath ,纯C语言编写,提供非常多强大的函数,是目前苹果官网推荐的多线程开发方法,NSOperation便是基于GCD的封装。
2、优势:
(1)为多核的并行运算提出了解决方案
(2)GCD会自动利用更多的CPU内核,比如双核,四核
(3)GCD自动管理线程的生命周期(创建线程,调度任务,销毁线程)
(4)程序员只需告诉GCD想要执行什么任务,不需要编写任何线程管理代码
3、GCD中有两个核心概念
(1)任务:执行什么操作
(2)队列:用来存放任务
4、队列可以分为两大类型
(1)串行队列(serial Dispatch Queue):只有一个线程,加入到队列中的操作按添加顺序依次执行一个任务助兴完毕后,才能执行下一个任务
(2)并发队列(Concurrent Dispatch Queue:可以有多个线程,操作进来以后它会将这些线程安排在可用的处理器上,同时保证先进来的任务优先处理
(3)还有一个特殊的队列就是主队列,主队列中永远只有一个线程——主线程,用来执行主线程的操作任务。
5、采用GCD做多线程,可以抽象为两步
(1)找到队列(主队列或串行队列或并行队列)
(2)在队列中用同步或者异步的方式执行任务
6、执行队列中任务的两种方式
(1)同步:只能在当前线程执行任务,不具备开启新线程的能力
(2)异步:可以在新的线程中执行任务,具备开启新线程的能力
7、GCD创建的线程的四种执行方式
(1)串行同步
1、找到队列
/*
*第一个参数:该队列的名字
*第二个参数:指定队列的类型
*/
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
给队列指定任务
/*
*asyn是异步 syn是同步
*第一个参数:任务在哪个队列中执行
*第二个参数:想要执行的而操作
*/
dispatch_sync(serialQueue, ^{
NSLog(@"1===%@",[NSThread currentThread]);
});
(2)串行异步
1、找到队列
dispatch_queue_t serialQueue1 = dispatch_queue_create("serialQueue1", DISPATCH_QUEUE_SERIAL);
2、给队列指定异步任务
dispatch_async(serialQueue1, ^{
NSLog(@"1 = %@",[NSThread currentThread]);
});
(3)并行同步
1、找到一个队列
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
2、给队列指定任务
dispatch_sync(concurrentQueue, ^{
NSLog(@"%@",[NSThread currentThread]);
});
(4)并行异步
// 1、创建队列
dispatch_queue_t concurrentQueue1 = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
// 2、给队列指定任务
dispatch_async(concurrentQueue1, ^{
NSLog(@"%@",[NSThread currentThread]);
});
8、加载一张图片的步骤:
1、创建视图
2、创建一个串行队列
3、用异步方式执行串行队列中的任务
4、加载网络资源
5、回到主线程
6、更新UI
以异步串行方式为例加载一张图片:
#import "OneImageViewController.h"
#define kurl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
@interface OneImageViewController ()
{
UIImageView *imageView;
}
@end
@implementation OneImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
[self.view addSubview:imageView];
// 2、串行队列
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
// 3、用异步方式执行串行队列中的任务
dispatch_async(serialQueue, ^{
// 4、加载网络资源
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
UIImage *image = [UIImage imageWithData:data];
// 5、回到主线程
// dispatch_get_main_queue()这个函数找到主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_sync(mainQueue, ^{
// 6、更新UI
imageView.image = image;
});
});
}
9、用并行方式加载多张图片:
#define kurl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
@interface MoreImageViewViewController ()
{
int imageIndex;
dispatch_queue_t concurrentQueue;
}
@end
@implementation MoreImageViewViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1、创建多个视图
imageIndex = 100;
for (int row = 0; row<3; row++) {
for (int list = 0; list<2; list++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5+list*205, 5+row*205, 200, 200)];
imageView.backgroundColor = [UIColor grayColor];
imageView.tag = imageIndex++;
[self.view addSubview:imageView];
}
}
// 2、找到并行队列
/*
*dispatch_get_global_queue 获取到系统的全局并行队列
*第一个参数:是优先级
*第二个参数:保留参数,没用
*/
// dispatch_queue_t concurrentQueue = dispatch_get_global_queue(0, 0);
concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_SERIAL);
// 3、给这个并行队列指定多个任务
for (int index = 0; index<6; index++) {
dispatch_async(concurrentQueue, ^{
// 在子线程加载网络资源
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
UIImage *image = [UIImage imageWithData:data];
// 5、回到主线程
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_sync(mainQueue, ^{
// 6、更新UI
UIImageView *imageView = [self.view viewWithTag:100+index];
imageView.image = image;
});
});
}
}