标签:let 实现 queue 三种 print import Operation 多线程 swift
swift实现多线程的三种方式
实现多线程的方式有三种:
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")
标签:let,实现,queue,三种,print,import,Operation,多线程,swift 来源: https://blog.csdn.net/weixin_45279502/article/details/121969747
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。