GCD

#import "ViewController.h"

@interface ViewController ()

@end
/*
 GCD 核心概念:将任务添加队列,指定任务执行的方法
 
 -任务
    - 使用block 封装
    - block 就是一个提前准备好的代码块,需要的时候执行

 -队列
    - 串行队列:一个接一个的执行任务
    - 并发队列:可以同时调度多个任务
 -任务执行函数(任务都需要在线程中执行)
    - 同步执行:不会到线程池里面去获取子线程
    - 异步执行:只要有任务,就会去线程池取子线程(主队列除外)
 
 
 小结:
  -- 开不开线程,取决于执行任务的函数,同步不开,异步就开
  -- 开几条线程,取决于队列,串行开一条,并发开多条(异步)
 
 */


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self gcdDemo7];
}
//全局队列
//Mark:全局队列(本质上是一个并发队列)
-(void)gcdDemo7
{
    //全局队列
    
    /*
     参数
     1:涉及系统适配
     iOS 8.0 服务质量
     QOS_CLASS_USER_INTERACTIVE  用户交互(希望线程快速执行,不要放一些耗时操作)
     QOS_CLASS_USER_INITIATED    用户需要的(不要放一些耗时操作)
     QOS_CLASS_DEFAULT           默认
     QOS_CLASS_UTILITY           使用工具(用来耗时操作)
     QOS_CLASS_BACKGROUND        后台
     QOS_CLASS_UNSPECIFIED       没有指定优先级
     
     iOS 7.0 调度优先级
     DISPATCH_QUEUE_PRIORITY_HIGH      高优先级
     DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默认优先级
     DISPATCH_QUEUE_PRIORITY_LOW (-2)  低优先级
     DISPATCH_QUEUE_PRIORITY_BACKGROUND 后台优先级
     
     
     参数2:为未来的保留参数
     
     提示:尤其不要选择BACKGROUND  ,线程执行会慢到令人发指!
     */
    
    dispatch_queue_t q = dispatch_get_global_queue(0, 0);//前者:优先级  后者:保留值
    
    for (int i = 0; i < 10; i++) {
        
        dispatch_async(q, ^{ // sync
            NSLog(@"%@ %d",[NSThread currentThread],i);
            
        });
       
    }
     NSLog(@"Come here");
    /*
     2018-05-07 18:11:01.401836+0800 003 GCD演示[86602:5136701] Come here
     2018-05-07 18:11:01.402103+0800 003 GCD演示[86602:5136760] <NSThread: 0x600000461b80>{number = 3, name = (null)} 0
     2018-05-07 18:11:01.402121+0800 003 GCD演示[86602:5136759] <NSThread: 0x604000272f40>{number = 4, name = (null)} 1
     2018-05-07 18:11:01.402156+0800 003 GCD演示[86602:5136761] <NSThread: 0x600000461f80>{number = 5, name = (null)} 2
     2018-05-07 18:11:01.402204+0800 003 GCD演示[86602:5136768] <NSThread: 0x604000273000>{number = 6, name = (null)} 3
     2018-05-07 18:11:01.402478+0800 003 GCD演示[86602:5136762] <NSThread: 0x604000273040>{number = 7, name = (null)} 4
     2018-05-07 18:11:01.402593+0800 003 GCD演示[86602:5136821] <NSThread: 0x604000272f80>{number = 9, name = (null)} 6
     2018-05-07 18:11:01.402610+0800 003 GCD演示[86602:5136820] <NSThread: 0x6000004620c0>{number = 8, name = (null)} 5
     2018-05-07 18:11:01.402614+0800 003 GCD演示[86602:5136822] <NSThread: 0x6040002730c0>{number = 10, name = (null)} 7
     2018-05-07 18:11:01.402693+0800 003 GCD演示[86602:5136823] <NSThread: 0x600000461bc0>{number = 11, name = (null)} 8
     2018-05-07 18:11:01.402744+0800 003 GCD演示[86602:5136824] <NSThread: 0x604000273140>{number = 12, name = (null)} 9
    
     
   ---->  总结:没什么特别的 开启一个 异步并发队列
     2018-05-07 18:12:05.299154+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 0
     2018-05-07 18:12:05.302814+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 1
     2018-05-07 18:12:05.303830+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 2
     2018-05-07 18:12:05.304140+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 3
     2018-05-07 18:12:05.304293+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 4
     2018-05-07 18:12:05.304410+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 5
     2018-05-07 18:12:05.304639+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 6
     2018-05-07 18:12:05.304751+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 7
     2018-05-07 18:12:05.304915+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 8
     2018-05-07 18:12:05.305030+0800 003 GCD演示[86627:5138620] <NSThread: 0x604000075f40>{number = 1, name = main} 9
     2018-05-07 18:12:05.399105+0800 003 GCD演示[86627:5138620] Come here
    ----> 总结:没什么特别的 开启一个 同步并发队列
     */
}


//Mark:增加班同步任务
/*
 队列调度多个任务之前,指定一个同步任务,让所有的异步任务,等待同步任务执行完成,就是依赖关系
 同步任务就相当于一个锁
 */
-(void)gcdDemo6
{
//    dispatch_queue_t loginQueue = dispatch_queue_create("CC_GCD", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t loginQueue = dispatch_queue_create("CC_GCD", NULL);
    
    void (^task)() = ^{
        //1.登录
        
            NSLog(@"用户登录了 %@",[NSThread currentThread]);
        
        //2.支付
            sleep(1.3);
            NSLog(@"用户支付了 %@",[NSThread currentThread]);
        
        //3.下载
    
            NSLog(@"用户下载了 %@",[NSThread currentThread]);
/*
 2018-05-07 17:50:52.713088+0800 003 GCD演示[86418:5118592] 用户登录了 <NSThread: 0x60400027b1c0>{number = 3, name = (null)}
 2018-05-07 17:50:53.720881+0800 003 GCD演示[86418:5118592] 用户支付了 <NSThread: 0x60400027b1c0>{number = 3, name = (null)}
 2018-05-07 17:50:53.721117+0800 003 GCD演示[86418:5118592] 用户下载了 <NSThread: 0x60400027b1c0>{number = 3, name = (null)}
 总结:串行队列/并行队列 效果是一样的
 */
    };
    
    
    dispatch_async(loginQueue, task);
    
    
    
    
}








//Mark 并发队列,同步执行
/*
 会不会开辟线程?  顺序执行吗?   comeHere
  不会            顺序          最后
 */
-(void)gcdDemo4
{
    //1.队列 DISPATCH_QUEUE_CONCURRENT
    dispatch_queue_t q = dispatch_queue_create("CC_GCD", DISPATCH_QUEUE_CONCURRENT);

    
    //2.同步执行任务
    for (int i = 0;  i < 10; i++) {
        NSLog(@"%d------------- ",i);
        dispatch_sync(q, ^{
            if(i==3||i==5){
                
                sleep(2.0);
            }
            NSLog(@"%@,%d",[NSThread currentThread],i);
        });
        
    }
    
    NSLog(@"come here");
    /*
     2018-05-07 16:51:46.511757+0800 003 GCD演示[85767:5060291] 0-------------
     2018-05-07 16:51:46.518181+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},0
     2018-05-07 16:51:46.518790+0800 003 GCD演示[85767:5060291] 1-------------
     2018-05-07 16:51:46.519115+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},1
     2018-05-07 16:51:46.519493+0800 003 GCD演示[85767:5060291] 2-------------
     2018-05-07 16:51:46.521978+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},2
     2018-05-07 16:51:46.522231+0800 003 GCD演示[85767:5060291] 3-------------
     2018-05-07 16:51:48.523538+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},3
     2018-05-07 16:51:48.523810+0800 003 GCD演示[85767:5060291] 4-------------
     2018-05-07 16:51:48.524136+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},4
     2018-05-07 16:51:48.524444+0800 003 GCD演示[85767:5060291] 5-------------
     2018-05-07 16:51:50.526239+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},5
     2018-05-07 16:51:50.526510+0800 003 GCD演示[85767:5060291] 6-------------
     2018-05-07 16:51:50.526741+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},6
     2018-05-07 16:51:50.526947+0800 003 GCD演示[85767:5060291] 7-------------
     2018-05-07 16:51:50.527393+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},7
     2018-05-07 16:51:50.528577+0800 003 GCD演示[85767:5060291] 8-------------
     2018-05-07 16:51:50.528809+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},8
     2018-05-07 16:51:50.528969+0800 003 GCD演示[85767:5060291] 9-------------
     2018-05-07 16:51:50.529213+0800 003 GCD演示[85767:5060291] <NSThread: 0x600000071e40>{number = 1, name = main},9
     2018-05-07 16:51:50.529396+0800 003 GCD演示[85767:5060291] come here
     
     总结:由于同步执行,即使为异步队列,也不会开辟新线程, 任务在主线程内执行.顺序执行()
     所以说开不开线程,看是否异步,和这个队列没毛关系
     */
}






//Mark:并发队列,异步执行
/*
 会不会开辟线程?  顺序执行吗?   comeHere
    会            不是         不确定
 */
-(void)gcdDemo3
{
    //1.队列 DISPATCH_QUEUE_CONCURRENT
    dispatch_queue_t q = dispatch_queue_create("CC_GCD", DISPATCH_QUEUE_CONCURRENT);
    
    
    //2.异步执行任务
    for (int i = 0;  i < 10; i++) {
        NSLog(@"%d------------- ",i);
        dispatch_async(q, ^{
            if(i== 3||i==4){
                
                sleep(1.5);
            }
            NSLog(@"%@,%d",[NSThread currentThread],i);
        });
        
    }
    
    NSLog(@"come here");
    
    /*
     2018-05-07 16:47:48.030652+0800 003 GCD演示[85712:5053945] 0-------------
     2018-05-07 16:47:48.030958+0800 003 GCD演示[85712:5053945] 1-------------
     2018-05-07 16:47:48.031097+0800 003 GCD演示[85712:5054078] <NSThread: 0x600000471a80>{number = 3, name = (null)},0
     2018-05-07 16:47:48.031267+0800 003 GCD演示[85712:5053945] 2-------------
     2018-05-07 16:47:48.031354+0800 003 GCD演示[85712:5054079] <NSThread: 0x600000472100>{number = 4, name = (null)},1
     2018-05-07 16:47:48.031461+0800 003 GCD演示[85712:5053945] 3-------------
     2018-05-07 16:47:48.031501+0800 003 GCD演示[85712:5054078] <NSThread: 0x600000471a80>{number = 3, name = (null)},2
     2018-05-07 16:47:48.031620+0800 003 GCD演示[85712:5053945] 4-------------
     2018-05-07 16:47:48.032760+0800 003 GCD演示[85712:5053945] 5-------------
     2018-05-07 16:47:48.033992+0800 003 GCD演示[85712:5053945] 6-------------
     2018-05-07 16:47:48.034114+0800 003 GCD演示[85712:5054077] <NSThread: 0x600000471ec0>{number = 5, name = (null)},5
     2018-05-07 16:47:48.035160+0800 003 GCD演示[85712:5053945] 7-------------
     2018-05-07 16:47:48.035318+0800 003 GCD演示[85712:5054075] <NSThread: 0x604000262d00>{number = 6, name = (null)},6
     2018-05-07 16:47:48.037172+0800 003 GCD演示[85712:5053945] 8-------------
     2018-05-07 16:47:48.037215+0800 003 GCD演示[85712:5054077] <NSThread: 0x600000471ec0>{number = 5, name = (null)},7
     2018-05-07 16:47:48.037922+0800 003 GCD演示[85712:5053945] 9-------------
     2018-05-07 16:47:48.037965+0800 003 GCD演示[85712:5054075] <NSThread: 0x604000262d00>{number = 6, name = (null)},8
     2018-05-07 16:47:48.038402+0800 003 GCD演示[85712:5053945] come here
     2018-05-07 16:47:48.038440+0800 003 GCD演示[85712:5054077] <NSThread: 0x600000471ec0>{number = 5, name = (null)},9
     2018-05-07 16:47:49.034294+0800 003 GCD演示[85712:5054079] <NSThread: 0x600000472100>{number = 4, name = (null)},3
     2018-05-07 16:47:49.034364+0800 003 GCD演示[85712:5054078] <NSThread: 0x600000471a80>{number = 3, name = (null)},4
     
     总结:并行队列/异步执行
     是否开辟线程:会
     开辟线程数:不一定/cpu调度
     执行顺序:不一定,看线程内任务是否耗时
     */
    
}






//Mark:串行队列,异步执行
/*
 会不会开辟线程?  顺序执行吗?
    会              会
 */
-(void)gcdDemo2
{
    
    //1.队列 -- 串行 DISPATCH_QUEUE_SERIAL 串行 等价于NULL
    dispatch_queue_t q = dispatch_queue_create("CC_GCD", NULL);
    
    
    //2.异步执行任务
    for (int i = 0;  i < 10; i++) {
        NSLog(@"%d------------- ",i);
        dispatch_async(q, ^{
            NSLog(@"%@,%d",[NSThread currentThread],i);
        });
        
    }
    
    NSLog(@"come here");
    /*
     2018-05-07 16:42:14.945286+0800 003 GCD演示[85624:5044961] 0-------------
     2018-05-07 16:42:14.945568+0800 003 GCD演示[85624:5044961] 1-------------
     2018-05-07 16:42:14.945730+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},0
     2018-05-07 16:42:14.946207+0800 003 GCD演示[85624:5044961] 2-------------
     2018-05-07 16:42:14.946371+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},1
     2018-05-07 16:42:14.946510+0800 003 GCD演示[85624:5044961] 3-------------
     2018-05-07 16:42:14.946642+0800 003 GCD演示[85624:5044961] 4-------------
     2018-05-07 16:42:14.946673+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},2
     2018-05-07 16:42:14.947064+0800 003 GCD演示[85624:5044961] 5-------------
     2018-05-07 16:42:14.948015+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},3
     2018-05-07 16:42:14.948690+0800 003 GCD演示[85624:5044961] 6-------------
     2018-05-07 16:42:14.949690+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},4
     2018-05-07 16:42:14.950768+0800 003 GCD演示[85624:5044961] 7-------------
     2018-05-07 16:42:14.951341+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},5
     2018-05-07 16:42:14.951730+0800 003 GCD演示[85624:5044961] 8-------------
     2018-05-07 16:42:14.952629+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},6
     2018-05-07 16:42:14.953559+0800 003 GCD演示[85624:5044961] 9-------------
     2018-05-07 16:42:14.954518+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},7
     2018-05-07 16:42:14.955275+0800 003 GCD演示[85624:5044961] come here
     2018-05-07 16:42:14.955690+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},8
     2018-05-07 16:42:14.956065+0800 003 GCD演示[85624:5045326] <NSThread: 0x60400026f300>{number = 3, name = (null)},9
     
     总结:异步串行队列,会开辟子线程,然后子线程内任务依次执行(废话 一个线程内执行的 能不顺序执行么)
     come here 虽然在主线程,但是不见得先打印,也许子线程中任务执行更快
     */
    
}






//Mark:串行队列,同步执行
/*
  会不会开辟线程?  顺序执行吗?
    不会            会
 */
-(void)gcdDemo1
{
    
    //1.队列 -- 串行 DISPATCH_QUEUE_SERIAL 串行 等价于NULL
    dispatch_queue_t q = dispatch_queue_create("CC_GCD", NULL);
    
    
    //2.同步执行任务
    for (int i = 0;  i < 10; i++) {
        dispatch_sync(q, ^{
            NSLog(@"%@,%d",[NSThread currentThread],i);
        });
   
    }
    
   /*
    2018-05-07 16:40:25.967160+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},0
    2018-05-07 16:40:25.969158+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},1
    2018-05-07 16:40:25.987708+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},2
    2018-05-07 16:40:25.988432+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},3
    2018-05-07 16:40:25.989809+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},4
    2018-05-07 16:40:25.990379+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},5
    2018-05-07 16:40:25.990513+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},6
    2018-05-07 16:40:25.990759+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},7
    2018-05-07 16:40:25.990905+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},8
    2018-05-07 16:40:25.991057+0800 003 GCD演示[85590:5041711] <NSThread: 0x6000002600c0>{number = 1, name = main},9
    
    总结:没啥好说的 同步串行队列 ,主线线程内依次执行
    */
    
}


//线程池???? !!!


//全局队列本质是一个 并发队列
/*gcd  执行子线程的执行顺序

 方案1:
 实现一个异步串行队列
 异步开启子线程,串行队列顺序执行 三个任务
 
 方案2:将三个任务封装成一个block整体
 开启异步队列/串行,并行队列都行,因为是个整体
 

*/


@end

 

posted @ 2018-05-07 18:17  看谷秀  阅读(512)  评论(0编辑  收藏  举报