网络多线程-GCD常用函数

#import "ViewController.h"

#import "XMGPerson.h"

@interface ViewController ()


@end


@implementation ViewController


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

{

    /*

    XMGPerson *p1 = [[XMGPerson alloc]init];

    NSLog(@"%@",p1.books);

    

    XMGPerson *p2 = [[XMGPerson alloc]init];

    NSLog(@"%@",p2.books);

     */

    [self barrier];

}


//一次性代码:整个程序运行过程中只会执行一次

/*不能放在懒加载里面的*/

-(void)once

{

    NSLog(@"-----");

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        NSLog(@"----once----");

    });

}


//延迟执行

-(void)afterDelay

{

    NSLog(@"-----start----");

    

    //    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];

    

    //    [self performSelector:@selector(run) withObject:nil afterDelay:3.0];

    

    dispatch_queue_t queue = dispatch_get_main_queue();

    //    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    /*

     GCD里面的时间是以纳秒为单位的 1=10的九次方纳秒

     */

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)),queue , ^{

        

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

    });

}


//栅栏函数:控制并发队列里面任务的执行顺序

/*不能使用全局并发队列*/

-(void)barrier

{

    //1.创建并发队列

    dispatch_queue_t queue = dispatch_queue_create("xmg", DISPATCH_QUEUE_CONCURRENT);

    

    //2.异步函数

    dispatch_async(queue, ^{

        for (NSInteger i = 0; i<10; i++) {

            NSLog(@"download1--%zd--%@",i,[NSThread currentThread]);

        }

    });

    

    dispatch_async(queue, ^{

        for (NSInteger i = 0; i<10; i++) {

            NSLog(@"download2--%zd--%@",i,[NSThread currentThread]);

        }

    });

    

    //3.使用栅栏函数

    dispatch_barrier_async(queue, ^{

        NSLog(@"+++++++++++++++++barrier+++++++++++");

    });

    

    dispatch_async(queue, ^{

        for (NSInteger i = 0; i<10; i++) {

            NSLog(@"download3--%zd--%@",i,[NSThread currentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (NSInteger i = 0; i<10; i++) {

            NSLog(@"download4--%zd--%@",i,[NSThread currentThread]);

        }

    });

    

    dispatch_async(queue, ^{

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

    });

    

}


-(void)run

{

    NSLog(@"---run---");

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值