GCD各种队列

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

//    [self asyncGlobalQueue];
//    [self asyncSerialQueue];
//    [self syncGlobalQueue];
//    [self syncSerialQueue];
    [self asyncMainQueue];
    
}

// 异步函数 执行 并行队列 会 开辟新线程 并行执行任务
- (void)asyncGlobalQueue {
    
    // 全局并发队列
    dispatch_queue_t queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    Returns a system-defined global concurrent queue with the specified quality of service class.
//    返还一个 系统定义的 指定的标准  全局并行队列
    
    // 异步执行 并发队列中的任务
    dispatch_async(queue, ^{
        
//        Submits a block for asynchronous execution on a dispatch queue and returns immediately.
//        提交一个异步的线程队列 的block块代码 并且会立即返回 (回调)
        NSLog(@"1 -- %@",[NSThread currentThread]);
    });
    
//    Calls to this function always return immediately after the block has been submitted and never wait for the block to be invoked.
//    调用这个函数会立即返回,等到block已经被提交而不会等到block执行完(唤醒)
    
    dispatch_async(queue, ^{
        NSLog(@"2 -- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"3 -- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"4 -- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"5 -- %@",[NSThread currentThread]);
    });
    
    
}

// 异步函数 执行 串行队列 会 开辟新线程 串行执行任务
- (void)asyncSerialQueue {
    
    // 创建一个串行队列
//    Creates a new dispatch queue to which blocks can be submitted.
//    创建一个新的 block代码块可以被提交的 调度队列
//    Blocks submitted to a serial queue are executed one at a time in FIFO order.
//    block提交一个连续的执行队列,执行顺序是按照FIFO 先进先出
    /**
     *
     使用dispatch_queue_create创建连续的(串行)的队列 (serial Dispatch Queue)
     该queue虽然每次只执行一个任务,但是通过dispatch_queue_create可以创建多个
     Serial Dispatch Queue,将处理追加到多个queue,每次就会执行多个任务
     */
    dispatch_queue_t queue = dispatch_queue_create("Charles", NULL);
    
    dispatch_async(queue, ^{
        NSLog(@"1 -- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"2 -- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"3 -- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"4 -- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"5 -- %@",[NSThread currentThread]);
    });
    
    
    
}

// 同步函数 执行 并行队列 不会 开辟新线程 串行执行任务
- (void)syncGlobalQueue {
    
    // 创建一个全局的并行队列
    dispatch_queue_t queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    

    
    dispatch_sync(queue, ^{
        NSLog(@"1 -- %@",[NSThread currentThread]);
    });
    
    //    Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.
    
    /**
     *  提交一个block代码块 到一个 串行执行的调度队列中  不像dispatch_async, 这个函数不会返回直到 block已经完成,
     调用这个函数 并把这个当前队列作为参数 导致死锁
     */
   
    dispatch_sync(queue, ^{
        NSLog(@"2 -- %@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"3 -- %@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"4 -- %@",[NSThread currentThread]);
    });
}

// 同步函数 执行 串行队列 不会 开辟新线程 串行执行任务
- (void)syncSerialQueue {
    
    // 创建一个串行队列
   dispatch_queue_t queue =  dispatch_queue_create("Charles", 0);
    
    dispatch_sync(queue, ^{
        NSLog(@" 1 -- %@",[NSThread currentThread]);
    });

    dispatch_sync(queue, ^{
        NSLog(@" 2 -- %@",[NSThread currentThread]);
    });

    dispatch_sync(queue, ^{
        NSLog(@" 3 -- %@",[NSThread currentThread]);
    });

    
    dispatch_sync(queue, ^{
        NSLog(@" 4 -- %@",[NSThread currentThread]);
    });

    
}

// 同、异步函数 执行 主队列 不会 开辟新线程 串行执行任务
- (void)asyncMainQueue {
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        NSLog(@" 1 -- %@",[NSThread currentThread]);
        
//        The main queue is automatically created by the system and associated with your application’s main thread. Your application uses one (and only one) of the following three approaches to invoke blocks submitted to the main queue:
        
//        这个主队列会被系统立即创建 和 你的应用程序主线程有关联
//        你的应用程序用下面三个方法中的一个 (只能用一个)来唤醒block代码块提交给主线程
    });
    
    
    dispatch_async(queue, ^{
        NSLog(@" 2 -- %@",[NSThread currentThread]);
    });
    
    
    dispatch_async(queue, ^{
        NSLog(@" 3 -- %@",[NSThread currentThread]);
    });
    
    
    dispatch_async(queue, ^{
        NSLog(@" 4 -- %@",[NSThread currentThread]);
    });
    
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值