Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift-->GCD,NSThread,NSBlockOperation多线程使用(主线程回调)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

应用程序开发,少不了的多线程,与多线程相关的就是线程同步. 本文介绍Swift最简单的多线程使用. 推荐阅读: http://www.jianshu.com/p/0b0d9b1f1f19 看例子: 1:获取线程基本的信息 func getThreadInfo() { let thread = NSThread.currentThread() let threadInfo = "线程

应用程序开发,少不了的多线程,与多线程相关的就是线程同步.
本文介绍Swift最简单的多线程使用.

推荐阅读: http://www.jianshu.com/p/0b0d9b1f1f19

看例子:

1:获取线程基本的信息

func getThreadInfo() {
    let thread = NSThread.currentThread()
    let threadInfo = "线程名:\(thread.Name)\n" +
        "堆栈大小:\(thread.stackSizE)\n" +
        "优先级:\(thread.threadPriority)\n" +
        "是否是主线程:\(thread.ismainThread)\n" +
        "地址:\(NSThread.callStackReturnAddresses().first)-\(NSThread.callStackReturnAddresses().last!)"
}

2:子线程的创建方法

// MARK: 手动创建线程,并且手动开始线程
func createSingleThread() {
    NSThread(target: self,SELEctor: #SELEctor(onThreadRun),object: nil).start()
}

// MARK: 创建线程并自动开始线程
func createAndStartThread() {
    NSThread.detachNewThreadSELEctor(#SELEctor(onThreadRun),@R_911_10586@rget: self,withObject: nil)
}

3:GCD(Grand Central Dispatch)队列的使用

// MARK:添加一个block到主线程队列,注意会阻塞当前线程,等待当前队列任务执行完,才会执行block
func gcdMainQueueSync() {
    dispatch_sync(dispatch_get_main_queue()) {
        print("main_sync")
        self.getThreadInfo()
    }
}

// MARK: 添加一个block到主队列,异步执行,不会阻塞当前线程
func gcdMainQueueAsync() {
    dispatch_async(dispatch_get_main_queue()) {
        print("main_async")
        self.getThreadInfo()
    }
}

4:自定义queue

// MARK: 自定义一个同步队列
func gcdCustomserialQueue() {
    let queue = dispatch_queue_create("serial_queue",DISPATCH_QUEUE_seriaL)
    // 同步调用,当前线程执行
// dispatch_sync(queuE) {
// print("custom_queue")
// self.getThreadInfo()
// }
    // 异步调用,子线程执行
    dispatch_async(queue) {
        print("custom_queue")
        self.getThreadInfo()
    }
}

// MARK: 自定义一个异步队列
func gcdCustomcatoncurrentQueue() {
    let queue = dispatch_queue_create("serial_queue",DISPATCH_QUEUE_CONCURRENT)
// dispatch_sync(queuE) {
// print("custom_queue")
// self.getThreadInfo()
// }
    dispatch_async(queue) {
        print("custom_queue")
        self.getThreadInfo()
    }
}

5:NSBlockOperation和NSOperationQueue的使用

// MARK: ns block 会在当前线程执行,并且是一个 并行队列
func blockOperation() {
    // 并行队列,多任务会同时进行
// NSBlockOperation {
// print("blockOperation")
// self.getThreadInfo()
// }.start()

    let block = NSBlockOperation {
        print("blockOperation1")
        self.getThreadInfo()
    }
    block.addExecutionBlock {
        print("blockOperation2")
        self.getThreadInfo()
    }
    block.addExecutionBlock {
        print("blockOperation3")
        self.getThreadInfo()
    }
    block.start() // 并行执行1,2,3个任务,执行顺序不保证
}

// MARK: 添加到队列的block会立即执行,
func blockOperationQueue() {
    let queue = NSOperationQueue()
// queue.addoperationWithBlock {
// print("blockOperationQueue")
// self.getThreadInfo()
// }

    let operation1 = NSBlockOperation {
        print("operation1")
        self.getThreadInfo()
    }
    let operation2 = NSBlockOperation {
        print("operation2")
        self.getThreadInfo()
    }
    let operation3 = NSBlockOperation {
        print("operation3")
        self.getThreadInfo()
    }
    operation2.addDependency(operation1) // 2会等待1执行完后,再执行
    operation3.addDependency(operation2) // 3会等待2执行完后,再执行
    queue.addoperations([operation1,operation2,operation3],waitUntilFinished: true) //
}

6:子线程调用,主线程执行(必备技能,更新UI的首选)

// MARK: 一定在主线程回调
func @H_877_68@mainThread() {
    // 1->在主线程的队列执行
    dispatch_async(dispatch_get_main_queue()) {
        print("dispatch_async")
        self.getThreadInfo()
    }

    // 2->在主线程执行
    NSOperationQueue.mainQueue().addoperationWithBlock {
        print("mainQueue")
        self.getThreadInfo()
    }
}

通常,我都是这样使用子线程的:

NSOperationQueue().addoperationWithBlock {
    print("runOnThread")
    self.getThreadInfo()
}

源码: https://github.com/angcyo/ThreadDemo

至此: 文章就结束了,如有疑问: QQ群 Android:274306954 Swift:399799363 欢迎您的加入.

@H_673_281@

大佬总结

以上是大佬教程为你收集整理的Swift-->GCD,NSThread,NSBlockOperation多线程使用(主线程回调)全部内容,希望文章能够帮你解决Swift-->GCD,NSThread,NSBlockOperation多线程使用(主线程回调)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。