多线程开发

异步

public typealias Task = () -> Void

func _async(_ task: @escaping Task,_ mainTask: Task? = nil) {
    let item = DispatchWorkItem(block: task)
    DispatchQueue.global().async(execute: item)
    if let main = mainTask {
        item.notify(queue: DispatchQueue.main, execute: main)
    }
    
}
//子线程做任务
public func async(_ task: @escaping Task){
    _async(task)
}
//子线程执行完任务回到主线程做任务
public func async(_ task: @escaping Task, _ mainTask: Task? = nil) {
    _async(task, mainTask)
}
class HomeViewController: UIViewController {

    override func viewDidLoad() {
        //子线程执行任务
        async {
            for _ in 0 ... 9 {
                print(Thread.current)
            }
        }
        //子线程执行完任务回到主线程做任务
        _async({//子线程
            print(11111)
            for _ in 0 ... 999999 {

            }
            print(22222)
            print(Thread.current)
        }) {//主线程
            print(33333)
            print(Thread.current)
        }
  
        print(4444)
    } 
}
//x子线程做任务.主线程任务可以传空
        _async({
            print(Thread.current)
        }, nil)

延迟

public typealias Task = () -> Void

@discardableResult
public func delay(_ seconds: Double, _ block: @escaping Task) -> DispatchWorkItem {
    let item = DispatchWorkItem(block: block)
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + seconds, execute: item)
    return item
}

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        print(Date())
        delay(3) {
        print(Date())
        }
    }
}
//打印
2019-08-08 08:15:35 +0000
2019-08-08 08:15:38 +0000

异步延迟

public typealias Task = () -> Void
func _asyncDelay(_ seconds: Double,_ task: @escaping Task, _ mainTask : Task? = nil) -> DispatchWorkItem {
    let item = DispatchWorkItem(block: task)
    DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + seconds, execute: item)
    if let main = mainTask {
        item.notify(queue: DispatchQueue.main, execute: main)
    }
    return item
}
@discardableResult
public func asyncDelay(_ seconds: Double,_ task: @escaping Task) -> DispatchWorkItem {
    return _asyncDelay(seconds, task)
}

@discardableResult
public func asyncDelay(_ seconds: Double,_ task: @escaping Task,_ mainTask: Task? = nil) -> DispatchWorkItem {
    return _asyncDelay(seconds, task, mainTask)
}

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        print(Date(),00000)//1
        asyncDelay(3) {
            print(Date(),1111,Thread.current)//4
        }
        print(Date(),222222)//2
        asyncDelay(3, {
            print(Date(),333333,Thread.current)//4
        }) {
            print(Date(),44444,Thread.current)//5
        }
        print(Date(),5555555)//3

    }
}
//打印
2019-08-08 08:56:41 +0000 0
2019-08-08 08:56:41 +0000 222222
2019-08-08 08:56:41 +0000 5555555
2019-08-08 08:56:44 +0000 1111 {number = 4, name = (null)}
2019-08-08 08:56:44 +0000 333333 {number = 6, name = (null)}
2019-08-08 08:56:44 +0000 44444 {number = 1, name = main}

once

  • dispatch_onceSwift中已经被废弃,可以使用类型属性或者全局变量\常量默认自带lazy + dispatch_once效果
let age1: Int = {
    print(1111)
    return 10
}()


class HomeViewController: UIViewController {
    static let age2: Int = {
       print(2222)
        return 20
    }()
    
    override func viewDidLoad() {
       super.viewDidLoad()
        print(age1)
        print(age1)
        // 1111 10 10
        print(HomeViewController.self.age2)
        print(HomeViewController.self.age2)
        // 2222 20 20
    }
}

你可能感兴趣的:(多线程开发)