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")

上一篇:APIJSON 博客13 Operation 第十三篇


下一篇:1