NSThread 子线程 Cocoa NSOperation GCD(Grand Central Dispatch) 多线程

单词:thread 英 θred:n 线、思路、vt 穿过、vi 穿透过

一、    进程、线程

进程:正在进行中的程序被称为进程,负责程序运行的内存分配,每一个进程都有自己独立的虚拟内存空间

线程:线程是进程中一个独立的执行路径(控制单元),一个进程中至少包含一条线程,即主线程(UI操作),可以将耗时的执行路径(如网络请求)放在其他线程中执行,线程不能被杀掉,但是可以暂停/休眠一条线程。

创建线程的目的:开启一条新的执行路径,运行指定的代码,与主线程中的代码实现同时运行

多线程的优势:充分发挥多核处理器优势,将不同线程任务分配给不同的处理器,真正进入"并行运算"状态;将耗时的任务分配到其他线程执行,由主线程负责统一更新界面会使应用程序更加流畅,用户体验更好;当硬件处理器的数量增加,程序会运行更快,而程序无需做任何调整.。

弊端:新建线程会消耗内存空间和CPU时间,线程太多会降低系统的运行性能

二、    NSThread

优点:NSThread 比其他两个轻量级

缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销

1、获取当前线程
NSLog(@"%@",[NSThread currentThread]);//获取当前线程,当打印的number等于1时,代表当前是主线程,大于1代表子线程
2、创建子线程方式(两种方法)
1)显式创建方式,需要手动开启线程。 最后的object代表线程触发方法时传递的参数,注意,有且只有一个参数
thread = [[NSThreadalloc] initWithTarget:selfselector:@selector(run1:) object:@"123"];
    thread.name = @"123";//给线程起个名字
    [thread start];//开启线程
2)隐式创建方式,不需要手动开启
[NSThread detachNewThreadSelector:@selector(run2) toTarget:self withObject:nil];

[self performSelectorInBackground:@selector(test:) withObject:@"456"];

3、线程间通讯

回到主线程:    [self performSelectorOnMainThread:@selector(testMain:) withObject:nil waitUntilDone:YES];// waitUntilDone是指是否等到主线程把方法执行完了,这个performSelector方法才返回。

在指定线程上执行操作:[self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];

在本线程执行:    [self performSelector:@selector(run) withObject:nil];

4、实现方法
1) [NSThread sleepForTimeInterval:3];//让调用该方法的线程睡一会,参数代表秒数
2)  NSRunLoop *r = [NSRunLoop currentRunLoop];//获取当前线程的runloop
3)  [self performSelectorOnMainThread:@selector(test) withObject:nil waitUntilDone:NO];//跳入主线程的方法,第一个参数代表要调用的方法,第二个参数代表传的值,第三个参数代表是否等待此操作完成后再继续往下执行
4) [self performSelector:@selector(test) withObject:nil];//在当前线程执行另外一个方法
5) [self performSelector:@selector(test) withObject:nil afterDelay:3];//延迟调用
6) NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]];

     [NSThread sleepUntilDate:date];//以当前时间为标准,延迟到某个时间再执行该线程操作

7) 获取主线程:    NSThread *main = [NSThread mainThread];

8) 获取当前线程:    NSThread *current = [NSThread currentThread];

9) 结束线程:[NSThread exit];//当前线程方法中使用

     [thread cancel];//主线程中使用

5、具体代码如下:
#import "ViewController.h"
@interface ViewController (){
    NSThread *thread;
    BOOL    _isEnd;//线程终止的标识
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _isEnd = NO;
    NSLog(@"%@",[NSThread currentThread]);//获取当前线程,当打印的number等于1时,代表当前是主线程,大于1代表子线程
    //*子线程创建方式1: 最后的object代表线程触发方法时传递的参数,注意,有且只有一个参数
    thread = [[NSThread alloc] initWithTarget:self selector:@selector(run1:) object:@"123"];
    thread.name = @"123";//给线程起个名字
    [thread start];//开启线程,只有显式的创建方式,需要手动开启、
    //*子线程创建方式2:
    [NSThread detachNewThreadSelector:@selector(run2) toTarget:self withObject:nil];//隐式创建,不需要手动开启
}
-(void)run1:(NSString *)str{
   // [NSThread sleepForTimeInterval:3];//让调用该方法的线程睡一会,参数代表秒数
    NSLog(@"子线程:%@",[NSThread currentThread]);
    NSRunLoop *r = [NSRunLoop currentRunLoop];//获取当前线程的runloop
    //判断当前线程是否需要循环(线程继续存活)
    while (!_isEnd) {
        [r runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];//runMode 第一个参数代表消息方式,默认写NSDefaultRunLoopMode,第二个参数代表结束时间,一般给distantFuture,代表不可达到的未来某个时间
    }
    NSLog(@"销毁");
}
-(void)run2{
    NSLog(@"子线程2:%@",[NSThread currentThread]);
//*    [self performSelectorOnMainThread:@selector(test) withObject:nil waitUntilDone:NO];//跳入主线程的方法,第一个参数代表要调用的方法,第二个参数代表传的值,第三个参数代表是否等待此操作完成后再继续往下执行
   
 //*   [self performSelector:@selector(test) withObject:nil];//在当前线程执行另外一个方法
 //*延迟调用    [self performSelector:<#(nonnull SEL)#> withObject:<#(nullable id)#> afterDelay:<#(NSTimeInterval)#>] 
    [self performSelector:@selector(run3) onThread:thread withObject:nil waitUntilDone:NO];//在某个线程中执行某个方法 
    NSLog(@"其他");
}
-(void)run3{
    _isEnd = YES;//方法执行结束后将线程结束标识置为YES
    NSLog(@"-----");
}
-(void)test{
    NSLog(@"主线程方法%@",[NSThread currentThread]);
}
 

三、    Cocoa NSOperation

优点:不需要关心线程管理,数据同步的事情,可以把精力放在自己需要执行的操作上。

NSOperation实例封装了需要执行的操作和执行操作所需的数据,并且能够以并发或非并发的方式执行这个操作。

NSOperation本身是抽象基类,因此必须使用它的子类,使用NSOperation子类的方式有2种:一种是用定义好的两个子类:NSInvocationOperation 和 NSBlockOperation,另一种是自定义子类继承NSOperation,实现内部相应的方法

1、       NSOperation的作用:配合使用NSOperation和NSOperationQueue也能实现多线程编程取消操作

2、       NSOperation和NSOperationQueue实现多线程的具体步骤:

先将需要执行的操作封装到一个NSOperation对象中

然后将NSOperation对象添加到NSOperationQueue中

系统会⾃动将NSOperationQueue中的NSOperation取出来

将取出的NSOperation封装的操作放到⼀条新线程中执⾏

2、创建操作对象,封装要执行的任务
//NSInvocationOperation   封装操作
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run1) object:nil];
 
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run1) object:nil];
3、执行操作
    [operation start];
    [operation1 start];
注意:操作对象默认在主线程中执行,只有添加到队列中才会开启新的线程。即默认情况下,如果操作没有放到队列queue中,都是同步执行。只有将NSOperation放到一个
4、NSBlockOperation创建操作对象
    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"block操作:%@",[NSThread currentThread]);
    }];
    //在NSBlockOperation对象中添加一个操作,如果NSBlockOperation对象包含了多个操作,有一个是在主线中一行,其他均在子线程中执行
5、添加操作(注意添加要放在start之前)
    [block addExecutionBlock:^{
        NSLog(@"block操作2:%@",[NSThread currentThread]);
    }];
    [block addExecutionBlock:^{
       NSLog(@"block操作3:%@",[NSThread currentThread]);
    }];
    [block addExecutionBlock:^{
        NSLog(@"block操作4:%@",[NSThread currentThread]);
    }];
    6、执行操作
  [block start ];//注意添加要放在start之前
   
    7、NSOperationQueue:创建队列:将操作放入队列(主队列除外),默认在子线程中执行,且不需要手动start
1)NSOperationQueue的作⽤:NSOperation可以调⽤start⽅法来执⾏任务,但默认是同步执行的
如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作
添加操作到NSOperationQueue中,自动执行操作,自动开启线程
2)创建NSOperationQueue 
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    NSInvocationOperation *ioperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run2) object:nil];
8、把操作添加到队列中
   1)第一种:添加之前创建的
  [queue addOperation:ioperation];
   2)第二种:添加的时候创建
注意:使用NSOperationQueue时,无需start
  [queue addOperationWithBlock:^{
        NSLog(@"block操作队列:%@",[NSThread currentThread]);
    }];
9、事件的实现
-(void)run1{
    NSLog(@"执行操作:%@",[NSThread currentThread]);
}
-(void)run2{
    NSLog(@"队列中执行:%@",[NSThread currentThread]);
}
 
 
四、GCD 全称:Grand Central Dispatch
1、Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法。在iOS4.0开始之后才能使用。GCD是一个替代诸如NSThread, NSOperationQueue, NSInvocationOperation等技术的很高效和强大的技术。
2、GCD的工作原理是:让程序平行排队的特定任务,根据可用的处理资源,安排他们在任何可用的处理器核心上执行任务
3、 GCD是基于C语言的。如果使用GCD,完全由系统管理线程,我们不需要编写线程代码。只需定义想要执行的任务,然后添加到适当的调度队列(dispatch queue)。GCD会负责创建线程和调度你的任务,系统直接提供线程管理
4、常用的方法dispatch_async
为了避免界面在处理耗时的操作时卡死,比如读取网络数据,IO,数据库读写等,我们会在另外一个线程中处理这些操作,然后通知主线程更新界面。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 耗时的操作
        dispatch_async(dispatch_get_main_queue(), ^{
            // 更新界面
        });
});
dispatch_async开启一个异步操作,第一个参数是指定一个gcd队列,第二个参数是分配一个处理事物的程序块到该队列。
dispatch_get_global_queue(0, 0),指用了全局队列。
一般来说系统本身会有3个队列:global_queue、current_queue(废弃)和main_queue
获取一个全局队列是接受两个参数,第一个是我分配的事物处理程序块队列优先级。分高低和默认,0为默认2为高,-2为低
5、dispatch_group_async的使用
dispatch_group_async可以实现监听一组任务是否完成,完成后得到通知执行其他的操作。这个方法很有用,比如你执行三个下载任务,当三个任务都下载完成后你才通知界面说完成了。
1)创建一个组
    dispatch_group_t group = dispatch_group_create();
2)在一个组内提交一个代码块来执行。必须明确这个代码块属于哪个组,必须     在哪个派送队列上执行。
dispatch_group_async(group, queue, ^{
       NSLog(@"group1--%@",[NSThread currentThread]);
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"group2--%@",[NSThread currentThread]);    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"group3--%@",[NSThread currentThread]);    });
3)等待组中的任务执行完毕,回到主线程执行block回调
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"updateUi");
    });
6、dispatch_barrier_async
dispatch_barrier_async是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行
     //创建队列,第一个参数代表队列名,第二个代表串行还是并行:DISPATCH_QUEUE_SERIAL串行   DISPATCH_QUEUE_CONCURRENT 并行
  dispatch_queue_t queue = dispatch_queue_create("123", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        NSLog(@"dispatch_async1");
});
//上一步执行结束才执行这一步
    dispatch_barrier_async(queue, ^{
        NSLog(@"dispatch_barrier_async");
});
//执行完上一步才执行下一步
    dispatch_async(queue, ^{
        NSLog(@"dispatch_async3");
    });
7、dispatch_apply 
     它一般都是放在dispatch_async里面(异步)。
执行某个代码片段N次
  dispatch_async(dispatch_get_global_queue(0, 0), ^{
        dispatch_apply(5, dispatch_get_global_queue(0, 0), ^(size_t index) {
            NSLog(@"%ld",index);
        }); 
    });
 
 
8、具体代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 30, 100, 100)];
    [self.view addSubview:imageview];
    UIImageView *imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 150, 100, 100)];
    [self.view addSubview:imageview1];
   UIImageView *imageview2 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 270, 100, 100)];
    [self.view addSubview:imageview2];
    UIImageView *imageview3 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 390, 100, 100)];
    [self.view addSubview:imageview3];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //NSLog(@"耗时操作:%@",[NSThread currentThread]);   
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.yooyoo360.com/photo/2009-1-1/20090112132752467.jpg"]];
        UIImage *image = [UIImage imageWithData:data];
        //NSLog(@"---%@",image);
        //返回主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            //NSLog(@"更新UI:%@",[NSThread currentThread]);
            imageview.image = image;
        });   
    });
    //组
    //创建组,用于存放耗时操作
    dispatch_group_t group = dispatch_group_create();  
    __block UIImage *image,*image1,*image2,*image3;
    //将操作封装进组,第一个参数代表要存放操作的组名,第二个参数代表操作队列,block执行耗时操作
//在一个组内提交一个代码块来执行。必须明确这个代码块属于哪个组,必须在哪个派送队列上执行。
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic2.ooopic.com/10/81/58/62bOOOPICce.jpg"]];
        image1 = [UIImage imageWithData:data];
        //NSLog(@"==%@",image1);
    });
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.downsc.com/vector_pic/shiliang_iecool/5/3/b_img/14430.jpg"]];
        image2 = [UIImage imageWithData:data];
    });
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.downsc.com/vector_pic/shiliang_iecool/5/2/b_img/13788.jpg"]]; 
        image3 = [UIImage imageWithData:data];
        //NSLog(@"----%@",image); 
    });
    //监听一组操作,第一个参数代表要监听的组名,第二个参数代表一组操作完全结束后跳转到哪个队列,一般跳到主线程(dispatch_get_main_queue),block执行要进行的操作(一般用来更新UI),注意:此方法在组中的所有操作执行完毕后调用
//等待组中的任务执行完毕,回到主线程执行block回调
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        imageview.image = image;
        imageview1.image = image1;
        imageview2.image = image2;
        imageview3.image = image3;
    });
}
-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
 
原文地址:https://www.cnblogs.com/wxzboke/p/5034914.html