GCD 的使用

 可以查看Demo

//  ViewController.m

//  UsedGCD

//

//  Created by jinxin on 16/8/1.

//  Copyright © 2016年 zhaixingzhi. All rights reserved.

//



#import "ViewController.h"



@interface ViewController ()



@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    

    [self mainSync];;

}

/**

 *  主队列 + 异步任务 : 没有开启新线程,任务还是逐个完成的

 */

-(void)mainAsync

{

    dispatch_queue_t queue = dispatch_get_main_queue();

    dispatch_async(queue, ^{

        for (int

             i = 0; i< 6; i++) {

            NSLog(@"1----%@",[NSThread currentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i = 0; i< 6; i++) {

            NSLog(@"2----%@",[NSThread currentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i = 0; i< 6; i++) {

            NSLog(@"3----%@",[NSThread currentThread]);

        }

    });

    



}

/**

 *  主队列 + 同步任务:会造成死锁的进程 【切记】:不能字啊主队列中添加同步任务

 */

-(void)mainSync

{

    NSLog(@"---1");

    dispatch_queue_t queue = dispatch_get_main_queue();

    dispatch_sync(queue, ^{

        NSLog(@"不会打印");

    });

    NSLog(@"---2");

    

    

}



/**

 *  串行队列 +异步任务,开启了新的线程,任务是逐个完成的



 */

-(void)serialAsync

{

    dispatch_queue_t queue = dispatch_queue_create("queue", NULL);

    

    dispatch_async(queue, ^{

        for (int

             i = 0; i< 6; i++) {

            NSLog(@"1----%@",[NSThread currentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i = 0; i< 6; i++) {

            NSLog(@"2----%@",[NSThread currentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i = 0; i< 6; i++) {

            NSLog(@"3----%@",[NSThread currentThread]);

        }

    });



}

/**

 *  串行队列 + 同步任务:没有开启新的线程,任务是逐个完成

 */

-(void)serialSync

{

//创建串行队列

    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);//这里可以用DISPATCH_QUEUE_SERIAL或者NULL都是串行队列

    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThread currentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThread currentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThread currentThread]);

        }

    });



}

/**

 *  全局队列 + 异步任务 : 开启新的线程,任务是并发的

 */

-(void)globalAsync

{

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    dispatch_async(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThread currentThread]);

        }

   

    });



    dispatch_async(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThread currentThread]);

        }

        

    });

    dispatch_async(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThread currentThread]);

        }

        

    });

}

/**

 *  全局队列 + 同步任务 : 没有开启新线程 ,任务是逐个完成的

 */

-(void)globalSync

{

    //获得全局队列

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThread currentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThread currentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThread currentThread]);

        }

    });



}



/**

 *  并发队列 + 异步任务 : 开启了新线程,任务是并发的

 *

 */

-(void)concurrentAsunc

{

    //创建并发队列
    dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);

    //异步任务

    dispatch_async(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThread currentThread]);

        }

  

    });

    //异步任务

    dispatch_async(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThread currentThread]);

        }

        

    });

    //异步任务

    dispatch_async(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThread currentThread]);

        }

        

    });



}



/**

 *  并发队列 + 同步任务 : 没有开启新的线程,任务逐个进行

 */

-(void)concurrentSync

{

    //创建并行队列
    dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);

    //在队列中创建任务

    //同步任务
    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-1--%@",[NSThread currentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-2--%@",[NSThread currentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"-3--%@",[NSThread currentThread]);

        }

    });



}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值