【Swift】实现多线程的三种方式

【Swift】实现多线程的三种方式,第1张

Swift实现多线程的三种方式 Thread实现快捷方式实现初始化器 Cocoa Operation(Operation和OperationQueue)实现BlockOperation实现自定义Operation实现加入Operation的回调completionBlock Grand Central Dispath(GCD)实现同步sync异步asyncasyncAfter

实现多线程的方式有三种:
Thread,
Cocoa Operation(Operation和OperationQueue),
Grand Central Dispath(GCD)

Thread实现 快捷方式实现
import UIKit
import PlaygroundSupport

//不希望主线程代码实现完成之后就退出了
PlaygroundPage.current.needsIndefiniteExecution = true;

//快捷方式实现
for i in 0..<10 {
    Thread.detachNewThread {
        print(i)
    }
}
初始化器
import UIKit
import PlaygroundSupport

//不希望主线程代码实现完成之后就退出了
PlaygroundPage.current.needsIndefiniteExecution = true;

//初始化器
class ObjectThread {
    func threadTest() {
        let thread = Thread(target: self, selector: #selector(threadExecute), object: nil)
        thread.start()
    }
    
    @objc func threadExecute() {
        print("threadExecuting1")
    }
}
let obj = ObjectThread();
obj.threadTest()
Cocoa Operation(Operation和OperationQueue)实现 BlockOperation实现
import UIKit
import PlaygroundSupport

//不希望主线程代码实现完成之后就退出了
PlaygroundPage.current.needsIndefiniteExecution = true;

//BlockOperation实现(代码可以多运行几次)
class ObjectOperation {
    func threadTest() {
        let operation = BlockOperation {[ weak self] in 
            self?.threadExecute()
        }
        let queue = OperationQueue()
        queue.addOperation(operation)
    }
    
    func threadExecute() {
        print("threadExecuting")
    }
}

let objOperation = ObjectOperation()
objOperation.threadTest()
print("after invoke test")
自定义Operation实现
import UIKit
import PlaygroundSupport

//不希望主线程代码实现完成之后就退出了
PlaygroundPage.current.needsIndefiniteExecution = true;

//自定义Operation实现
class ObjectOperation {
    func threadTest() {
        let operation = MyOperation()
        let queue = OperationQueue()
        queue.addOperation(operation)
    }
}

class MyOperation: Operation {
    override func main() {
        sleep(1)
        print("in MyOperation main")
    }
}

let objOperation = ObjectOperation()
objOperation.threadTest()
print("after invoke test")
加入Operation的回调completionBlock
import UIKit
import PlaygroundSupport

//不希望主线程代码实现完成之后就退出了
PlaygroundPage.current.needsIndefiniteExecution = true;

//加入Operation的回调completionBlock
class ObjectOperation {
    func threadTest() {
        let operation = MyOperation()
        operation.completionBlock{ ()-> Void in
            print("completionBlock")
        }
        let queue = OperationQueue()
        queue.addOperation(operation)
    }
}

class MyOperation: Operation {
    override func main() {
        sleep(1)
        print("in MyOperation main")
    }
}

let objOperation = ObjectOperation()
objOperation.threadTest()
print("after invoke test")

Grand Central Dispath(GCD)实现 同步sync
import UIKit
import PlaygroundSupport

//不希望主线程代码实现完成之后就退出了
PlaygroundPage.current.needsIndefiniteExecution = true;

let queue = DispatchQueue(label: "myQueue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)

queue.sync {
    sleep(1)
    print("in queue sync")
}

print("after invoke queue method")

异步async
import UIKit
import PlaygroundSupport

//不希望主线程代码实现完成之后就退出了
PlaygroundPage.current.needsIndefiniteExecution = true;

let queue = DispatchQueue(label: "myQueue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)

  queue.async {
      sleep(1)
      print("in queue async")
  }
  
print("after invoke queue method")
asyncAfter
import UIKit
import PlaygroundSupport

//不希望主线程代码实现完成之后就退出了
PlaygroundPage.current.needsIndefiniteExecution = true;

let queue = DispatchQueue(label: "myQueue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)

queue.asyncAfter(deadline: .now() + 1 ){
    print("in asyncAfter")
}

print("after invoke queue method")

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/989530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存