Dispatch

作者: 迷路的小小 | 来源:发表于2020-11-24 13:20 被阅读0次

1. 定时执行

let timer = DispatchSource.makeTimerSource()
timer.schedule(deadline: .now() + 1, repeating: 1)
timer.setEventHandler {
   print("1")
}
timer.resume()
timer.suspend()

2. DispatchSemaphore信号量

let timer = DispatchSource.makeTimerSource()

func semaphoreDemo() -> Void {
    let sema = DispatchSemaphore(value: 0)
    
    print("我要开始了啊")
    print("你快点……")
    
    timer.schedule(deadline: .now() + 1, repeating: 1)
    timer.setEventHandler {
        print("1")
    }
    timer.resume()
    
    getListData { (reslut) in
        if reslut {
            sema.signal()
        }
    }
    sema.wait()
    timer.suspend()
    print("我终于可以开始干活了")
}

func getListData(isFinish: @escaping (Bool) -> Void) {
    DispatchQueue.global().async {
        Thread.sleep(forTimeInterval: 2)
        print("好了,好了!")
        isFinish(true)
    }
}

semaphoreDemo()

3. concurrent并发

  • 并发的快速发起:
DispatchQueue.concurrentPerform(iterations: 10) { (num) in
    print(num)
}
/// 3 1 0 2 4 6 5 7 9 8
  • 并发队列:
let concurrentQueue = DispatchQueue(label: "concurrent", attributes: .concurrent)

删格化barrier

func barrier() -> Void {
    DispatchQueue.concurrentPerform(iterations: 5) { (count) in
        concurrentQueue.async(flags: .barrier) { [self] in
            Thread.sleep(forTimeInterval: 0.1)
            k += 1
            print("线程\(Thread.current)正在执行\(k)号任务")
            sema.signal()
        }
    }
    
    for _ in 0..<5 {
        sema.wait()
    }
    print("-----------------")
}

线程<NSThread: 0x600001702b40>{number = 6, name = (null)}正在执行1号任务
线程<NSThread: 0x600001702b40>{number = 6, name = (null)}正在执行2号任务
线程<NSThread: 0x600001702b40>{number = 6, name = (null)}正在执行3号任务
线程<NSThread: 0x600001702b40>{number = 6, name = (null)}正在执行4号任务
线程<NSThread: 0x600001702b40>{number = 6, name = (null)}正在执行5号任务


默认:

func noBarrier() -> Void {
    
    DispatchQueue.concurrentPerform(iterations: 5) { (count) in
        concurrentQueue.async { [self] in
            Thread.sleep(forTimeInterval: 0.1)
            k += 1
            print("线程\(Thread.current)正在执行\(k)号任务")
            
            sema.signal()
        }
    }
    
    for _ in 0..<5 {
        sema.wait()
    }
    print("----------")
}

结果为:

线程<NSThread: 0x600001702b40>{number = 6, name = (null)}正在执行8号任务
线程<NSThread: 0x600001705b40>{number = 3, name = (null)}正在执行8号任务
线程<NSThread: 0x600001715d00>{number = 9, name = (null)}正在执行10号任务
线程<NSThread: 0x600001715380>{number = 7, name = (null)}正在执行9号任务
线程<NSThread: 0x600001705840>{number = 8, name = (null)}正在执行10号任务


  • 并发同步
func sync() {
    for i in 0..<10 {
        DispatchQueue.global().sync {
            //全局并发同步
            Thread.sleep(forTimeInterval: 2)
            print("线程\(Thread.current)正在执行\(i)号任务")
        }
    }
}

线程<NSThread: 0x6000007b07c0>{number = 1, name = main}正在执行0号任务
线程<NSThread: 0x6000007b07c0>{number = 1, name = main}正在执行1号任务
线程<NSThread: 0x6000007b07c0>{number = 1, name = main}正在执行2号任务
线程<NSThread: 0x6000007b07c0>{number = 1, name = main}正在执行3号任务
线程<NSThread: 0x6000007b07c0>{number = 1, name = main}正在执行4号任务

  • 并发异步
for i in 0..<5 {
    DispatchQueue.global().async {
        //全局并发同步
        Thread.sleep(forTimeInterval: 0.2)
        print("线程\(Thread.current)正在执行\(i)号任务")
    }
}

线程<NSThread: 0x60000006a400>{number = 9, name = (null)}正在执行3号任务
线程<NSThread: 0x60000006c000>{number = 7, name = (null)}正在执行0号任务
线程<NSThread: 0x600000066880>{number = 10, name = (null)}正在执行2号任务
线程<NSThread: 0x600000069cc0>{number = 11, name = (null)}正在执行4号任务
线程<NSThread: 0x600000070000>{number = 12, name = (null)}正在执行1号任务

4. DispatchGroup

let group = DispatchGroup()
let queue = DispatchQueue(label: "com.concurrent.thread", qos:
DispatchQoS.default, attributes: .concurrent)
        
print("开始")
for i in 1...4 {
    queue.async(group: group) {
       //并发异步
       Thread.sleep(forTimeInterval: 2)
       print("线程\(Thread.current)正在执行\(i)号任务")
   }
}
queue.async(group: group) {
    //并发异步
    Thread.sleep(forTimeInterval: 2)
    print("线程\(Thread.current)正在执行\(11)号任务")
}
                
group.notify(queue: DispatchQueue.main) {
     // 通知主线程,子线程操作已完成
     print("所有任务都已经完成")
}

相关文章

网友评论

      本文标题:Dispatch

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