gcd小结

作者: alex_zn | 来源:发表于2018-07-19 14:35 被阅读0次
    //: Playground - noun: a place where people can play
    
    import UIKit
    import Dispatch
    
    //MARK: - DispatchQueue
    
    // 初始化一个默认队列
    let lab = "com.dispatch"
    // 线程的优先级 quality of service
    let qos = DispatchQoS.default
    // attributes 队列的属性
    let attr = DispatchQueue.Attributes.concurrent
    // 自动释放频率
    let autorelease = DispatchQueue.AutoreleaseFrequency.never
    // 队列
    let queue = DispatchQueue.init(label: lab, qos: qos, attributes: attr, autoreleaseFrequency: autorelease, target: nil)
    
    // MARK: - 队列分类
    
    // 主队列(对应主线程)
    let mainQueue = DispatchQueue.main
    // 全局队列
    let globalQueue = DispatchQueue.global()
    // 优先级队列
    let globalQos = DispatchQueue.global(qos: DispatchQoS.QoSClass.background)
    
    // MARK: - 任务分类
    // 串行
    let serialQueue = DispatchQueue(label: "com.leo.serialQueue")
    // 异步
    serialQueue.async {
    
    }
    // 同步
    serialQueue.sync {
    
    }
    // 并行
    let concurrentQueue = DispatchQueue(label: "com.leo.concurrentQueue",attributes:.concurrent)
    // 异步
    concurrentQueue.async {
    
    }
    // MARK: - 任务对象
    let item = DispatchWorkItem {
    
    }
    let item2 = DispatchWorkItem.init(qos: qos, flags: DispatchWorkItemFlags.enforceQoS) {
    
    }
    DispatchQueue.global().async(execute: item)
    
    // MARK: - after(延迟执行)
    let deadline = DispatchTime.now() + 2.0
    DispatchQueue.global().asyncAfter(deadline: deadline) {
    
    }
    let timer = DispatchSource.makeTimerSource()
    timer.setEventHandler {
    
    }
    timer.setCancelHandler {
    
    }
    // 1秒后执行,然后10秒后自动取消,允许10毫秒的误差
    timer.schedule(deadline: .now(), repeating: 2, leeway: .microseconds(10))
    //timer.schedule(deadline: .now() + .seconds(1), interval: 2.0, leeway: .microseconds(10))
    timer.resume()
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 10) {
        timer.cancel()
    }
    
    // MARK: - DispatchGroup
    
    func test(id:Int,ok:@escaping ()->()) {
    
        print("id",id)
        ok()
    }
    let group = DispatchGroup()
    group.enter()
    test(id: 0) {
        group.leave()
    }
    group.enter()
    test(id: 1) {
        group.leave()
    }
    group.wait(timeout:.now() + 3)
    
    item.notify(queue: queue) {
        print("item end")
    }
    queue.async(group: group, execute: item)
    queue.async(group: group, execute: item2)
    
    
    group.notify(queue: DispatchQueue.main, work: DispatchWorkItem.init(block: {
        print("All network is done")
    }))
    
    // MARK: - 信号量 Semaphore
    let semaphore = DispatchSemaphore.init(value: 2)
    let queue3 = DispatchQueue.init(label: "com.semaphore", qos: .default, attributes: .concurrent, autoreleaseFrequency: .never, target: nil)
    queue3.async {
        semaphore.wait()
        // doSomething
        test(id: 1, ok: {
            semaphore.signal()
        })
    }
    queue3.async {
        semaphore.wait()
        // doSomething
        test(id: 2, ok: {
            semaphore.signal()
        })
    }
    queue3.async {
        semaphore.wait()
        // doSomething
        test(id: 3, ok: {
            semaphore.signal()
        })
    }
    // Barrier 屏障 线程锁
    let concurrentQueueBarr = DispatchQueue.init(label: "com.lab.barr")
    concurrentQueueBarr.async {
        // doSomething
    }
    concurrentQueueBarr.async {
        // doSomething
    }
    concurrentQueueBarr.async(group: nil, qos: DispatchQoS.background, flags: .barrier) {
    
    }
    
    
    // Operation
    let oquque = OperationQueue.init()
    let op1 = BlockOperation.init {
    
        print("op1")
    }
    let op2 = BlockOperation.init {
        print("op1")
    }
    op2.addDependency(op1)
    oquque.addOperation(op1)
    oquque.addOperation(op2)
    

    DCD 其他用法

        #pragma 信号量
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
        dispatch_async(queue, ^{
            
            // 相当于加锁
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            NSLog(@"执行任务1");
            // 相当于解锁
            dispatch_semaphore_signal(semaphore);
        });
        
        dispatch_async(queue, ^{
            
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            NSLog(@"执行任务2");
            dispatch_semaphore_signal(semaphore);
        });
        
        dispatch_async(queue, ^{
            
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            NSLog(@"执行任务3");
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                [NSThread sleepForTimeInterval:3];
                NSLog(@"异步执行任务3");
                dispatch_semaphore_signal(semaphore);
            });
            
        });
        
        dispatch_async(queue, ^{
            
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            NSLog(@"执行任务4");
            dispatch_semaphore_signal(semaphore);
        });
        
        #pragma barrier
        dispatch_queue_t queue2 = dispatch_queue_create("zzn.com.01", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(queue2, ^{
            // doSomething()
            [NSThread sleepForTimeInterval:2];
            NSLog(@"barrier:执行任务1");
        });
        dispatch_barrier_async(queue2, ^{
            NSLog(@"barrier:执行任务2");
        });
        
        #pragma group
        dispatch_group_t group = dispatch_group_create();
        dispatch_queue_t queue3 = dispatch_queue_create("zzn.com.03", DISPATCH_QUEUE_CONCURRENT);
        
        //
        dispatch_group_async(group, queue3, ^{
            
            [NSThread sleepForTimeInterval:2];
            NSLog(@"group:执行任务1");
        });
        
        // 等同 dispatch_group_async
        dispatch_async(queue3, ^{
            
            dispatch_group_enter(group);
            [NSThread sleepForTimeInterval:2];
            dispatch_group_leave(group);
            NSLog(@"group:执行任务2");
        });
        
        
        dispatch_group_notify(group, queue3, ^{
            NSLog(@"group:执行任务3");
        });
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:gcd小结

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