Operation是一个抽象类.
所以一般都是使用它的子类来实现多线程。
比如 BlockOperation的使用
let blockOperation = BlockOperation()
blockOperation.queuePriority = .veryHigh
blockOperation.addExecutionBlock {
print("block 1",Thread.current)
}
blockOperation.addExecutionBlock {
print("block 2",Thread.current)
}
blockOperation.start()
再来看一下 OperationQueue 的使用
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.addOperation {
print("block 2",Thread.current)
}
//队列取消
queue.cancelAllOperations()
//队列暂停
queue.isSuspended = true
//队列恢复
queue.isSuspended = false
BlockOperation可以添加到OperationQueue配合使用,
其实只需要
let queue = OperationQueue()
queue.addOperation(blockOperation)
addOperation这个方法自动触发
blockOperation.start()
这段代码