美文网首页
dispatch_barrier_async和dispatch_

dispatch_barrier_async和dispatch_

作者: 雪丹妮_66865 | 来源:发表于2019-09-25 15:28 被阅读0次

    dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
    NSLog(@"download1----- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
    NSLog(@"download2----- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
    NSLog(@"download3----- %@",[NSThread currentThread]);
    });
    dispatch_barrier_async(queue, ^{
    NSLog(@"barrier函数l开始");
    NSLog(@"barrier函数----- %@",[NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"download4----- %@",[NSThread currentThread]);
    });
    NSLog(@"执行结束");
    

    执行结束
    download1----- <NSThread: 0x1d047b400>{number = 3, name = (null)}
    download3----- <NSThread: 0x1d047af40>{number = 5, name = (null)}
    download2----- <NSThread: 0x1d407dcc0>{number = 4, name = (null)}
    barrier函数l开始
    barrier函数----- <NSThread: 0x1d407dcc0>{number = 4, name = (null)}
    download4----- <NSThread: 0x1d407dcc0>{number = 4, name = (null)}

    dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
        NSLog(@"download1----- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download2----- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download3----- %@",[NSThread currentThread]);
    });
    
    dispatch_barrier_sync(queue, ^{
        NSLog(@"barrier函数l开始");
        NSLog(@"barrier函数----- %@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"download4----- %@",[NSThread currentThread]);
    });
    NSLog(@"执行结束");
    

    download3----- <NSThread: 0x1d407d400>{number = 5, name = (null)}
    download1----- <NSThread: 0x1d026a400>{number = 3, name = (null)}
    download2----- <NSThread: 0x1d46605c0>{number = 4, name = (null)}
    barrier函数l开始
    barrier函数----- <NSThread: 0x1d407d200>{number = 1, name = main}
    执行结束
    download4----- <NSThread: 0x1d46605c0>{number = 4, name = (null)}

    dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
        NSLog(@"download1----- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"延时函数----- %@",[NSThread currentThread]);
        });
        NSLog(@"download2----- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download3----- %@",[NSThread currentThread]);
    });
    
    dispatch_barrier_sync(queue, ^{
        NSLog(@"barrier函数l开始");
        NSLog(@"barrier函数----- %@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"download4----- %@",[NSThread currentThread]);
    });
    NSLog(@"执行结束");
    

    download1----- <NSThread: 0x1d4478000>{number = 3, name = (null)}
    download2----- <NSThread: 0x1d006e1c0>{number = 4, name = (null)}
    download3----- <NSThread: 0x1d0071640>{number = 5, name = (null)}
    barrier函数l开始
    barrier函数----- <NSThread: 0x1d406e880>{number = 1, name = main}
    执行结束
    download4----- <NSThread: 0x1d0071640>{number = 5, name = (null)}
    延时函数----- <NSThread: 0x1d406e880>{number = 1, name = main}

    dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
        NSLog(@"download1----- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"延时函数----- %@",[NSThread currentThread]);
        });
        NSLog(@"download2----- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"download3----- %@",[NSThread currentThread]);
    });
    
    dispatch_barrier_async(queue, ^{
        NSLog(@"barrier函数l开始");
        NSLog(@"barrier函数----- %@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"download4----- %@",[NSThread currentThread]);
    });
    NSLog(@"执行结束");
    

    执行结束
    download1----- <NSThread: 0x1d0079e40>{number = 3, name = (null)}
    download2----- <NSThread: 0x1d0079e40>{number = 3, name = (null)}
    download3----- <NSThread: 0x1d447e500>{number = 4, name = (null)}
    barrier函数l开始
    barrier函数----- <NSThread: 0x1d447e500>{number = 4, name = (null)}
    download4----- <NSThread: 0x1d447e500>{number = 4, name = (null)}
    延时函数----- <NSThread: 0x1d4074440>{number = 1, name = main}

    1. dispatch_barrier_async 的作用是等待队列的前面的任务执行完毕后,才执行dispatch_barrier_async的block里面的任务,不会阻塞主线程

    2. dispatch_barrier_sync 的作用是等待队列的前面的任务执行完毕后,才执行dispatch_barrier_async的block里面的任务,阻塞主线程

    3. 当dispatch_barrier_async和dispatch_barrier_sync的队列里面有异步的任务的时候,dispatch_barrier不会等到异步的任务执行完成才执行。

    相关文章

      网友评论

          本文标题:dispatch_barrier_async和dispatch_

          本文链接:https://www.haomeiwen.com/subject/nysructx.html