iOS开发之多线程

来源:互联网 发布:360的免费dns解析域名 编辑:程序博客网 时间:2024/05/09 04:28

1.基本概念

  • 1.1 进程

    进程是指在系统中正在运行的一个应用程序。每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内。

  • 1.2 线程

(1)基本概念

1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程),线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行

(2)线程的串行

1个线程中任务的执行是串行的,如果要在1个线程中执行多个任务,那么只能一个一个地按顺序执行这些任务。也就是说,在同一时间内,1个线程只能执行1个任务。
  • 1.3 多线程

(1)基本概念

即1个进程中可以开启多条线程,每条线程可以并行(同时)执行不同的任务。

(2)线程的并行

并行即同时执行。比如同时开启3条线程分别下载3个文件(分别是文件A、文件B、文件C。

(3)多线程并发执行的原理

在同一时间里,CPU只能处理1条线程,只有1条线程在工作(执行)。多线程并发(同时)执行,其实是CPU快速地在多条线程之间调度(切换),如果CPU调度线程的时间足够快,就造成了多线程并发执行的假象

(4)多线程优缺点

优点     1)能适当提高程序的执行效率。     2)能适当提高资源利用率(CPU、内存利用率) 缺点     1)开启线程需要占用一定的内存空间(默认情况下,主线程占用1M,子线程占用512KB),如果开启大量的线程,会占用大量的内存空间,降低程序的性能。     2)线程越多,CPU在调度线程上的开销就越大。     3)程序设计更加复杂:比如线程之间的通信、多线程的数据共享
  • 1.4 多线程在iOS开发中的应用

(1)主线程

1)一个iOS程序运行后,默认会开启1条线程,称为“主线程”或“UI线程”。 2)作用。刷新显示UI,处理UI事件。 

(2)使用注意

1)不要将耗时操作放到主线程中去处理,会卡住线程。
  • 1.5 iOS中多线程的实现方案

(1)pthread

01 特点:     (1)一套通用的多线程API     (2)适用于Unix\Linux\Windows等系统     (3)跨平台\可移植     (4)使用难度大 02 使用语言:c语言 03 使用频率:几乎不用 04 线程生命周期:由程序员进行管理 

(2) NSThread

01 特点:     (1)使用更加面向对象     (2)简单易用,可直接操作线程对象 02 使用语言:OC语言 03 使用频率:偶尔使用 04 线程生命周期:由程序员进行管理 

(3)GCD

01 特点:     (1)旨在替代NSThread等线程技术     (2)充分利用设备的多核(自动) 02 使用语言:OC语言 03 使用频率:经常使用 04 线程生命周期:自动管理 

(4) NSOperation

01 特点:     (1)基于GCD(底层是GCD)     (2)比GCD多了一些更简单实用的功能     (3)使用更加面向对象 02 使用语言:OC语言 03 使用频率:经常使用 04 线程生命周期:自动管理 

2.pthread

(1)pthread的基本使用(需要包含头文件)

//使用pthread创建线程 pthread_t thread; NSString *name = @"wendingding"; //使用pthread创建线程 //第一个参数:线程对象地址 //第二个参数:线程属性 //第三个参数:指向函数的执行 //第四个参数:传递给该函数的参数 pthread_create(&thread, NULL, run, (__bridge void *)(name)); 

3.NSThread

(1)NSThread的基本使用

//第一种创建线程的方式:alloc init. //特点:需要手动开启线程,可以拿到线程对象进行详细设置 //创建线程 /*     第一个参数:目标对象     第二个参数:选择器,线程启动要调用哪个方法     第三个参数:前面方法要接收的参数(最多只能接收一个参数,没有则传nil) */ NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"wendingding"]; //启动线程 [thread start]; //第二种创建线程的方式:分离出一条子线程 //特点:自动启动线程,无法对线程进行更详细的设置 /*     第一个参数:线程启动调用的方法     第二个参数:目标对象     第三个参数:传递给调用方法的参数 */ [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是分离出来的子线程"]; //第三种创建线程的方式:后台线程 //特点:自动启动县城,无法进行更详细设置 [self performSelectorInBackground:@selector(run:) withObject:@"我是后台线程"];

(2)设置线程的属性

//设置线程的属性 //设置线程的名称 thread.name = @"线程A"; //设置线程的优先级,注意线程优先级的取值范围为0.0~1.0之间,1.0表示线程的优先级最高,如果不设置该值,那么理想状态下默认为0.5 thread.threadPriority = 1.0;

(3)线程的状态(了解)

//线程的各种状态:新建-就绪-运行-阻塞-死亡 //常用的控制线程状态的方法 [NSThread exit];//退出当前线程 [NSThread sleepForTimeInterval:2.0];//阻塞线程 [NSThread sleepUntilDate:[NSDatedateWithTimeIntervalSinceNow:2.0]];//阻塞线程 //注意:线程死了不能复生

(4)线程安全

01 前提:多个线程访问同一块资源会发生数据安全问题    02 解决方案:加互斥锁    03 相关代码:@synchronized(self){} //       @synchronized(锁对象) { // 需要锁定的代码  }    04 专业术语-线程同步- 线程同步的意思是:多条线程在同一条线上执行(按顺序地执行任务)- 互斥锁,就是使用了线程同步技术    05 原子和非原子属性(是否对setter方法加锁)- 互斥锁的优缺点- 优点:能有效防止因多线程抢夺资源造成的数据安全问题- 缺点:需要消耗大量的CPU资源
#import "ViewController.h"@interface ViewController ()/** 售票员01 */@property (nonatomic, strong) NSThread *thread01;/** 售票员02 */@property (nonatomic, strong) NSThread *thread02;/** 售票员03 */@property (nonatomic, strong) NSThread *thread03;/** 剩余票的张数 */@property (nonatomic, assign) NSInteger totalTicket;/** 锁对象 *///@property (nonatomic, strong) NSObject *obj;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //self.obj = [[NSObject alloc]init];    // 剩余票的总张数    self.totalTicket = 100;    self.thread01 = [[NSThread alloc]initWithTarget:self selector:@selector(sellTicket) object:nil];    self.thread01.name = @"售票员01";    self.thread02 = [[NSThread alloc]initWithTarget:self selector:@selector(sellTicket) object:nil];    self.thread02.name = @"售票员02";    self.thread03 = [[NSThread alloc]initWithTarget:self selector:@selector(sellTicket) object:nil];    self.thread03.name = @"售票员03";}- (void)sellTicket{    while (1) {        // 加互斥锁        @synchronized(self) {            [NSThread sleepForTimeInterval:0.1];            NSInteger count = self.totalTicket;            if (count > 0) {                self.totalTicket = count - 1;                NSLog(@"%@卖了一张票,还剩余%zd",[NSThread currentThread].name,self.totalTicket);            }            else{                NSLog(@"%@发现票已经买完了",[NSThread currentThread].name);                break;            }        }    }}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [self.thread01 start];    [self.thread02 start];    [self.thread03 start];}@end

(5)线程间通信

-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // [self download2];  //开启一条子线程来下载图片   [NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];   }   -(void)downloadImage {   //1.确定要下载网络图片的url地址,一个url唯一对应着网络上的一个资源 NSURL *url = [NSURL URLWithString:@"http://p6.qhimg.com/t01d2954e2799c461ab.jpg"];   //2.根据url地址下载图片数据到本地(二进制数据   NSData *data = [NSData dataWithContentsOfURL:url];   //3.把下载到本地的二进制数据转换成图片   UIImage *image = [UIImage imageWithData:data];   //4.回到主线程刷新UI   //4.1 第一种方式   // [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];    //4.2 第二种方式     // [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];    //4.3 第三种方式    [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES]; }

(6)如何计算代码段的执行时间

//第一种方法 NSDate *start = [NSDate date]; //2.根据url地址下载图片数据到本地(二进制数据)  NSData *data = [NSData dataWithContentsOfURL:url];  NSDate *end = [NSDate date];  NSLog(@"第二步操作花费的时间为%f",[end timeIntervalSinceDate:start]);  //第二种方法  CFTimeInterval start = CFAbsoluteTimeGetCurrent();  NSData *data = [NSData dataWithContentsOfURL:url]; CFTimeInterval end = CFAbsoluteTimeGetCurrent();  NSLog(@"第二步操作花费的时间为%f",end - start);

4.GCD

(1)GCD基本知识

01 两个核心概念-队列和任务 02 同步函数和异步函数 

(2)GCD基本使用【重点】

01 异步函数+并发队列:开启多条线程,并发执行任务02 异步函数+串行队列:开启一条线程,串行执行任务03 同步函数+并发队列:不开线程,串行执行任务04 同步函数+串行队列:不开线程,串行执行任务05 异步函数+主队列:不开线程,在主线程中串行执行任务06 同步函数+主队列:不开线程,串行执行任务(注意死锁发生)07 注意同步函数和异步函数在执行顺序上面的差异
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [self syncMain];}// 同步函数+主队列:不会开启线程,任务串行执行- (void)syncMain{    // 获取主队列    dispatch_queue_t queue = dispatch_get_main_queue();    // 异步函数    dispatch_sync(queue, ^{        NSLog(@"---download01----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download02----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download03----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download04----%@",[NSThread currentThread]);    });}// 异步函数+主队列:不会开启线程,任务串行执行- (void)asyncMain{    // 获取主队列    dispatch_queue_t queue = dispatch_get_main_queue();    // 异步函数    dispatch_async(queue, ^{        NSLog(@"---download01----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download02----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download03----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download04----%@",[NSThread currentThread]);    });}/** 同步函数+串行队列:不会开启线程,任务串行执行 */- (void)syncSerial{    // 创建串行队列    dispatch_queue_t queue = dispatch_queue_create("com.baidu.download", DISPATCH_QUEUE_SERIAL);    // 异步函数    dispatch_sync(queue, ^{        NSLog(@"---download01----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download02----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download03----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download04----%@",[NSThread currentThread]);    });}/** 同步函数+并发队列:不会开启线程,任务串行执行 */- (void)syncConcurrent{    // 创建 全局 并发队列    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);    // 异步函数    dispatch_sync(queue, ^{        NSLog(@"---download01----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download02----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download03----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download04----%@",[NSThread currentThread]);    });}// 异步函数+串行队列:开启一个线程,任务串行执行- (void)asyncSerial{    // 创建串行队列    dispatch_queue_t queue = dispatch_queue_create("com.baidu.download", DISPATCH_QUEUE_SERIAL);    // 异步函数    dispatch_async(queue, ^{        NSLog(@"---download01----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download02----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download03----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download04----%@",[NSThread currentThread]);    });}// 异步函数+并发队列:开启多个线程,任务并发执行- (void)asyncConcurrent{    // 创建并发队列    /**     *  创建并发队列     *     *  @param label#> C语言字符串,标签 description#>     *  @param attr#>  DISPATCH_QUEUE_CONCURRENT:并发队列 description#>     *     *  @return 队列     */    //dispatch_queue_t queue = dispatch_queue_create("com.baidu.download", DISPATCH_QUEUE_CONCURRENT);    // 获取并发队列    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);    // 异步函数    dispatch_async(queue, ^{        NSLog(@"---download01----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download02----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download03----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download04----%@",[NSThread currentThread]);    });}@end

(3)GCD线程间通信

//0.获取一个全局的队列  dispatch_queue_t queue = dispatch_get_global_queue(0, 0); //1.先开启一个线程,把下载图片的操作放在子线程中处理 dispatch_async(queue, ^{  //2.下载图片  NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/6a63f6246b600c3320b14bb3184c510fd8f9a185.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url];  UIImage *image = [UIImage imageWithData:data];  NSLog(@"下载操作所在的线程--%@",[NSThread currentThread]);  //3.回到主线程刷新UI dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.image = image;  //打印查看当前线程  NSLog(@"刷新UI---%@",[NSThread currentThread]);     });  });

(4)GCD其它常用函数

01 栅栏函数(控制任务的执行顺序)    dispatch_barrier_async(queue, ^{        NSLog(@"--dispatch_barrier_async-");    });    02 延迟执行(延迟·控制在哪个线程执行)      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        NSLog(@"---%@",[NSThread currentThread]);    });    03 一次性代码(注意不能放到懒加载)    -(void)once    {        //整个程序运行过程中只会执行一次        //onceToken用来记录该部分的代码是否被执行过        static dispatch_once_t onceToken;        dispatch_once(&onceToken, ^{            NSLog(@"-----");        });    }    04 快速迭代(开多个线程并发完成迭代操作)       dispatch_apply(subpaths.count, queue, ^(size_t index) {    });    05 队列组(同栅栏函数)    //创建队列组    dispatch_group_t group = dispatch_group_create();    //队列组中的任务执行完毕之后,执行该函数    dispatch_group_notify(dispatch_group_t group,    dispatch_queue_t queue,    dispatch_block_t block);
#import "ViewController.h"#import "XMGPerson.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (nonatomic, strong) UIImage  *image1; /**< 图片1 */@property (nonatomic, strong) UIImage  *image2; /**< 图片2 */@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//    [self once];//    XMGPerson *p1 = [[XMGPerson alloc]init];////    NSLog(@"%@",p1.books);////    XMGPerson *p2 = [[XMGPerson alloc]init];////    NSLog(@"%@",p2.books);//    [self applay];//    [self moveFile1];    [self barrier];}-(void)group{    //下载图片1    //创建队列组    dispatch_group_t group =  dispatch_group_create();    //1.开子线程下载图片    //创建队列(并发)    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);    dispatch_group_async(group, queue, ^{        //1.获取url地址        NSURL *url = [NSURL URLWithString:@"http://www.huabian.com/uploadfile/2015/0914/20150914014032274.jpg"];        //2.下载图片        NSData *data = [NSData dataWithContentsOfURL:url];        //3.把二进制数据转换成图片        self.image1 = [UIImage imageWithData:data];        NSLog(@"1---%@",self.image1);    });    //下载图片2    dispatch_group_async(group, queue, ^{        //1.获取url地址        NSURL *url = [NSURL URLWithString:@"http://img1.3lian.com/img2011/w12/1202/19/d/88.jpg"];        //2.下载图片        NSData *data = [NSData dataWithContentsOfURL:url];        //3.把二进制数据转换成图片        self.image2 = [UIImage imageWithData:data];        NSLog(@"2---%@",self.image2);    });    //合成    dispatch_group_notify(group, queue, ^{        //开启图形上下文        UIGraphicsBeginImageContext(CGSizeMake(200, 200));        //画1        [self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];        //画2        [self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];        //根据图形上下文拿到图片        UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();        //关闭上下文        UIGraphicsEndImageContext();        dispatch_async(dispatch_get_main_queue(), ^{            self.imageView.image = image;            NSLog(@"%@--刷新UI",[NSThread currentThread]);        });    });}-(void)applay{//    for (NSInteger i=0; i<10; i++) {//        NSLog(@"%zd--%@",i,[NSThread currentThread]);//    }    //创建队列(并发队列)    dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT);    /*     第一个参数:迭代的次数     第二个参数:在哪个队列中执行     第三个参数:block要执行的任务     */    dispatch_apply(10, queue, ^(size_t index) {        NSLog(@"%zd--%@",index,[NSThread currentThread]);    });}/** 快速迭代函数 */- (void)apply1{    dispatch_queue_t queue = dispatch_queue_create("com.down", DISPATCH_QUEUE_CONCURRENT);    NSString *from = @"/Users/weishine/Desktop/from";    NSString *to   = @"/Users/weishine/Desktop/to";    NSFileManager *maneger = [NSFileManager defaultManager];    NSArray *subpaths = [maneger subpathsAtPath:from];    NSLog(@"%@",subpaths);    NSInteger count = [subpaths count];    dispatch_apply(count, queue, ^(size_t i) {        NSString *subpath = subpaths[i];        NSString *fullPath = [from stringByAppendingPathComponent:subpath];        NSString *toFullPath = [to stringByAppendingPathComponent:subpath];        [maneger moveItemAtPath:fullPath toPath:toFullPath error:nil];    });}-(void)moveFile1{    //文件在哪个地方(文件夹)    NSString *form = @"/Users/xiaomage/Desktop/form";    //要剪切到什么地方    NSString *to = @"/Users/xiaomage/Desktop/to";    NSFileManager *manager = [NSFileManager defaultManager];//    NSArray *subpaths =  [manager subpathsAtPath:form];//    NSDirectoryEnumerator *enumer = [manager enumeratorAtPath:to];    NSDirectoryEnumerator *enumer = [manager directoryContentsAtPath:form];//    for (NSDirectoryEnumerator *en in enumer) {//        NSLog(@"%@",en);//    }////    //创建队列(并发队列)//    dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT);////    NSInteger count = [subpaths count];//    dispatch_apply(count, queue, ^(size_t index) {////        NSString *subpath = subpaths[index];////        NSString *fullPath = [form stringByAppendingPathComponent:subpath];////        //拼接目标文件全路径//        NSString *fileName = [to stringByAppendingPathComponent:subpath];////        //剪切操作//        [manager moveItemAtPath:fullPath toPath:fileName error:nil];////        NSLog(@"%@",[NSThread currentThread]);//    });}-(void)moveFile{    //文件在哪个地方(文件夹)    NSString *form = @"/Users/xiaomage/Desktop/form";    //要剪切到什么地方    NSString *to = @"/Users/xiaomage/Desktop/to";    NSFileManager *manager = [NSFileManager defaultManager];    NSArray *subpaths =  [manager subpathsAtPath:form];//    NSLog(@"%@",subpaths);    NSInteger count = [subpaths count];    for (NSInteger i = 0; i<count; i++) {        //拼接文件全路径//        NSString *fullPath = [form stringByAppendingString:<#(nonnull NSString *)#>]        NSString *subpath = subpaths[i];        NSString *fullPath = [form stringByAppendingPathComponent:subpath];        //拼接目标文件全路径        NSString *fileName = [to stringByAppendingPathComponent:subpath];        //剪切操作        [manager moveItemAtPath:fullPath toPath:fileName error:nil];        NSLog(@"%@--%@",fullPath,fileName);    }}-(void)once{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        NSLog(@"+++++++++");    });}-(void)delay{    NSLog(@"----");    //表名2秒钟之后调用run//    [self performSelector:@selector(run) withObject:nil afterDelay:2.0];//    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];    /*     第一个参数:延迟时间     第二个参数:要执行的代码     */    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{        NSLog(@"---%@",[NSThread currentThread]);    });}-(void)run{    NSLog(@"++++");}-(void)barrier{    //1.创建队列(并发队列)    dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT);    dispatch_async(queue, ^{        for (NSInteger i = 0; i<10; i++) {            NSLog(@"%zd-download1--%@",i,[NSThread currentThread]);        }    });    dispatch_async(queue, ^{        for (NSInteger i = 0; i<10; i++) {            NSLog(@"%zd-download2--%@",i,[NSThread currentThread]);        }    });    //栅栏函数    dispatch_barrier_async(queue, ^{        NSLog(@"我是一个栅栏函数");    });    dispatch_async(queue, ^{        for (NSInteger i = 0; i<10; i++) {            NSLog(@"%zd-download3--%@",i,[NSThread currentThread]);        }    });    dispatch_async(queue, ^{        for (NSInteger i = 0; i<10; i++) {            NSLog(@"%zd-download4--%@",i,[NSThread currentThread]);        }    });}@end

5.NSOperation

  • 5.1 NSOperation基本使用

(1)相关概念

01 NSOperation是对GCD的包装 02 两个核心概念【队列+操作】

(2)基本使用

01 NSOperation本身是抽象类,只能只有它的子类  02 三个子类分别是:NSBlockOperation、NSInvocationOperation以及自定义继承自NSOperation的类  03 NSOperation和NSOperationQueue结合使用实现多线程并发  

(3)相关代码

// 01 NSInvocationOperation   //1.封装操作  /*     第一个参数:目标对象     第二个参数:该操作要调用的方法,最多接受一个参数     第三个参数:调用方法传递的参数,如果方法不接受参数,那么该值传nil */ NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; //2.启动操作 [operation start];  -------------------------------------------------  // 02 NSBlockOperation  //1.封装操作  /*  NSBlockOperation提供了一个类方法,在该类方法中封装操作  */  NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{  //在主线程中执行      NSLog(@"---download1--%@",[NSThread currentThread]);      }];     //2.追加操作,追加的操作在子线程中执行 [operation addExecutionBlock:^{     NSLog(@"---download2--%@",[NSThread currentThread]);     }]; [operation addExecutionBlock:^{     NSLog(@"---download3--%@",[NSThread currentThread]); }]; //3.启动执行操作 [operation start]; ---------------------------------------------- // 03 自定义NSOperation //如何封装操作? //自定义的NSOperation,通过重写内部的main方法实现封装操作 -(void)main {     NSLog(@"--main--%@",[NSThread currentThread]);     }     //如何使用?     //1.实例化一个自定义操作对象     XMGOperation *op = [[XMGOperation alloc]init];     //2.执行操作 [op start];
  • 5.2 NSOperationQueue基本使用
    (1)NSOperation中的两种队列

    01 主队列 通过mainQueue获得,凡是放到主队列中的任务都将在主线程执行 02 非主队列 直接alloc init出来的队列。非主队列同时具备了并发和串行的功能,通过设置最大并发数属性来控制任务是并发执行还是串行执行 

(2)相关代码

//自定义NSOperation-(void)customOperation {    //1.创建队列    NSOperationQueue *queue = [[NSOperationQueue alloc]init];    //2.封装操作    //好处:1.信息隐蔽    //2.代码复用    XMGOperation *op1 = [[XMGOperation alloc]init]; XMGOperation    *op2 = [[XMGOperation alloc]init];    //3.添加操作到队列中    [queue addOperation:op1]; [queue addOperation:op2];}//NSBlockOperation- (void)block {    //1.创建队列 NSOperationQueue    *queue = [[NSOperationQueue alloc]init];    //2.封装操作    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"1----%@",[NSThread currentThread]); }];    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"2----%@",[NSThread currentThread]); }];    [op2 addExecutionBlock:^{ NSLog(@"3----%@",[NSThread currentThread]); }];    [op2 addExecutionBlock:^{ NSLog(@"4----%@",[NSThread currentThread]); }];    //3.添加操作到队列中    [queue addOperation:op1]; [queue addOperation:op2];    //补充:简便方法    [queue addOperationWithBlock:^{ NSLog(@"5----%@",[NSThread currentThread]);    }];}//NSInvocationOperation- (void)invocation {    /*      GCD中的队列:      串行队列:自己创建的,主队列      并发队列:自己创建的,全局并发队列      NSOperationQueue      主队列:[NSOperationQueue mainqueue];     凡事放在主队列中的操作都在主线程中执行 非主队列:[[NSOperationQueue alloc]init],并发和串行,默认是并发执行的     */    //1.创建队列    NSOperationQueue *queue = [[NSOperationQueue alloc]init];    //2.封装操作    NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil]; NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil];    //3.把封装好的操作添加到队列中    [queue addOperation:op1];//[op1 start]    [queue addOperation:op2];    [queue addOperation:op3];}
  • 5.3 NSOperation其它用法

(1)设置最大并发数【控制任务并发和串行】

//1.创建队列  NSOperationQueue *queue = [[NSOperationQueue alloc]init];  //2.设置最大并发数  //注意点:该属性需要在任务添加到队列中之前进行设置  //该属性控制队列是串行执行还是并发执行  //如果最大并发数等于1,那么该队列是串行的,如果大于1那么是并行的  //系统的最大并发数有个默认的值,为-1,如果该属性设置为0,那么不会执行任何任务  queue.maxConcurrentOperationCount = 2;  

(2)暂停和恢复以及取消

//设置暂停和恢复//suspended设置为YES表示暂停,suspended设置为NO表示恢复//暂停表示不继续执行队列中的下一个任务,暂停操作是可以恢复的if (self.queue.isSuspended){    self.queue.suspended = NO;}else {    self.queue.suspended = YES;}//取消队列里面的所有操作//取消之后,当前正在执行的操作的下一个操作将不再执行,而且永远都不在执行,就像后面的所有任务都从队列里面移除了一样//取消操作是不可以恢复的[self.queue cancelAllOperations];//---------自定义NSOperation取消操作---------------------------(void)main {    //耗时操作1    for (int i = 0; i<1000; i++) {        NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);    }    NSLog(@"+++++++++++++++++++++++++++++++++");    //苹果官方建议,每当执行完一次耗时操作之后,就查看一下当前队列是否为取消状态,如果是,那么就直接退出    //好处是可以提高程序的性能    if (self.isCancelled) { return; }    //耗时操作2    for (int i = 0; i<1000; i++) {        NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);    }    NSLog(@"+++++++++++++++++++++++++++++++++");}
  • 5.4 NSOperation实现线程间通信

(1)开子线程下载图片

//1.创建队列NSOperationQueue *queue = [[NSOperationQueue alloc]init];//2.使用简便方法封装操作并添加到队列中[queue addOperationWithBlock:^{    //3.在该block中下载图片    NSURL *url = [NSURL URLWithString:@"http://news.51sheyuan.com/uploads/allimg/111001/133442IB-2.jpg"];    NSData *data = [NSData dataWithContentsOfURL:url];    UIImage *image = [UIImage imageWithData:data];    NSLog(@"下载图片操作--%@",[NSThread currentThread]);    //4.回到主线程刷新UI    [[NSOperationQueue mainQueue] addOperationWithBlock:^{        self.imageView.image = image;        NSLog(@"刷新UI操作---%@",[NSThread currentThread]);    }];}];

(2)下载多张图片合成综合案例(设置操作依赖)

//02 综合案例- (void)download2 {    NSLog(@"----");    //1.创建队列    NSOperationQueue *queue = [[NSOperationQueue alloc]init];    //2.封装操作下载图片1    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{        NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/6a63f6246b600c3320b14bb3184c510fd8f9a185.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url];        //拿到图片数据        self.image1 = [UIImage imageWithData:data]; }];    //3.封装操作下载图片2    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{        NSURL *url = [NSURL URLWithString:@"http://pic.58pic.com/58pic/13/87/82/27Q58PICYje_1024.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url];        //拿到图片数据        self.image2 = [UIImage imageWithData:data];    }];    //4.合成图片    NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{        //4.1 开启图形上下文        UIGraphicsBeginImageContext(CGSizeMake(200, 200));        //4.2 画image1        [self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];        //4.3 画image2        [self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];        //4.4 根据图形上下文拿到图片数据        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();        // NSLog(@"%@",image);        //4.5 关闭图形上下文        UIGraphicsEndImageContext();        //7.回到主线程刷新UI        [[NSOperationQueue mainQueue]addOperationWithBlock:^{            self.imageView.image = image;            NSLog(@"刷新UI---%@",[NSThread currentThread]);        }];    }];    //5.设置操作依赖    [combine addDependency:op1]; [combine addDependency:op2];    //6.添加操作到队列中执行    [queue addOperation:op1];    [queue addOperation:op2];    [queue addOperation:combine];}
1 0