欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中多線程的入門使用教程(Swift)

 更新時間:2021年11月28日 11:12:25   作者:閩江小張  
這篇文章主要給大家介紹了關于iOS中多線程入門使用的相關資料,一個進程中可以開啟多條線程,每條線程可以并行執(zhí)行不同的任務,本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下

一、iOS的三種多線程技術

1、NSThread

–優(yōu)點:NSThread 比其他兩個輕量級,使用簡單

–缺點:需要自己管理線程的生命周期、線程同步、加鎖、睡眠以及喚醒等。線程同步對數(shù)據(jù)的加鎖會有一定的系統(tǒng)開銷

2、NSOperation

不需要關心線程管理,數(shù)據(jù)同步的事情,可以把精力放在自己需要執(zhí)行的操作上

3、GCD

基于C語言的

二、基本使用

NSThread的基本使用

方式一:

//        block
        let thread =  Thread.init {
                print("1、----this is thread \(Thread.current) ")
        }
        thread.start()

方式二:

//        SEL
        let thread2 = Thread.init(target: self, selector: #selector(text), object: nil)
        thread2.start()
        
    @objc func text(){
        print("2、----this is thread \(Thread.current) ")
    }

方式三:\

 self.performSelector(inBackground: #selector(text2), with: nil)

輸出

請?zhí)砑訄D片描述

tips:可以給任意一個線程增加延遲看看

   print(thread.isCancelled)//是否取消
   print(thread.isExecuting)//是否在執(zhí)行
   print(thread.isFinished)//是否完成
   print(thread.isMainThread)//是否是主線程

NSOperation的基本使用

方式一:

      let queue = OperationQueue.init()
//     最大線程數(shù)
        queue.maxConcurrentOperationCount = 2
        queue.addOperation {
            sleep(1)
            print("1111")
        }
        queue.addOperation {
            print("2222")
        }

輸出結果如下:

方式二:可設置優(yōu)先級

        let queue = OperationQueue.init()
      	let op =  BlockOperation.init {
            print("op")
        }
        op.queuePriority = .normal //設置優(yōu)先級
        queue.addOperation(op)
        let op2 = BlockOperation.init {
            print("op2")
        }
        op2.queuePriority = .normal
        queue.addOperation(op2)
        
        queue.addOperation {
            print("op3")
        }

略作修改

        let queue = OperationQueue.init()
        let op =  BlockOperation.init {
            sleep(1)
            print("op")
        }
        op.queuePriority = .normal //設置優(yōu)先級
        queue.addOperation(op)
        let op2 = BlockOperation.init {
            sleep(1)
            print("op2")
        }
        op2.queuePriority = .veryHigh
        queue.addOperation(op2)
        
        queue.addOperation {
            print("op3")
        }

輸出如下:

優(yōu)先級如下:

GCD的基本使用

方式一:queue.async 異步

   let queue =   DispatchQueue.init(label: "com.zjb.concurrent",attributes: .concurrent)
        for i in 0...15 {
            queue.async {
                sleep(1)
                print("this is \(Thread.current) \(i)")
            }
        }

方式二:queue.sync 同步

  let queue =   DispatchQueue.init(label: "com.zjb.concurrent",attributes: .concurrent)
        for i in 0...15 {
            queue.sync {
                sleep(1)
                print("this is \(Thread.current) \(i)")
            }
        }
        

附加網(wǎng)絡上一段代碼

        for i in 1...10 {
               DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
                   NSLog("DispatchQoS.QoSClass.default, %d", i)
               }
                    
               DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
                   NSLog("DispatchQoS.QoSClass.background, %d", i)
               }
                    
               DispatchQueue.global(qos: DispatchQoS.QoSClass.unspecified).async {
                   NSLog("DispatchQoS.QoSClass.unspecified, %d", i)
               }
                    
               DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {
                   NSLog("DispatchQoS.QoSClass.userInitiated, %d", i)
               }
                    
               DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive).async {
                   NSLog("DispatchQoS.QoSClass.userInteractive, %d", i)
               }
                    
               DispatchQueue.global(qos: DispatchQoS.QoSClass.utility).async {
                   NSLog("DispatchQoS.QoSClass.utility, %d", i)
               }
        }

優(yōu)先級userInteractive最高、background最低

總結

到此這篇關于iOS中多線程入門使用的文章就介紹到這了,更多相關iOS多線程使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • iOS 11 UINavigationItem 去除左右間隙的方法

    iOS 11 UINavigationItem 去除左右間隙的方法

    本篇文章主要介紹了iOS 11 UINavigationItem 去除左右間隙的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • iOS實現(xiàn)從通訊錄中選擇聯(lián)系人

    iOS實現(xiàn)從通訊錄中選擇聯(lián)系人

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)從通訊錄中選擇聯(lián)系人,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • iOS開發(fā)實現(xiàn)UIImageView的分類

    iOS開發(fā)實現(xiàn)UIImageView的分類

    這篇文章主要為大家詳細介紹了iOS開發(fā)實現(xiàn)UIImageView的分類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • iOS自定義雷達掃描擴散動畫

    iOS自定義雷達掃描擴散動畫

    這篇文章主要為大家詳細介紹了iOS自定義雷達掃描擴散動畫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • iOS功能實現(xiàn)之列表的橫向刷新加載

    iOS功能實現(xiàn)之列表的橫向刷新加載

    現(xiàn)今已有越來越多的APP需要橫向刷新的需求,而橫向刷新加載的控件卻寥寥無幾,即使有也是集成起來非常的麻煩,恰巧最近項目中又用到了這個功能,所以干脆自己來造個輪子,方便大家使用。
    2016-08-08
  • iOS下一鍵調試Push的方法詳解

    iOS下一鍵調試Push的方法詳解

    這篇文章主要給大家介紹了關于iOS下如何一鍵調試Push的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-03-03
  • iOS頁面跳轉及數(shù)據(jù)傳遞(三種)

    iOS頁面跳轉及數(shù)據(jù)傳遞(三種)

    本文主要介紹了iOS頁面跳轉的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • ios設備使用iframe寬度超出屏幕的解決方法

    ios設備使用iframe寬度超出屏幕的解決方法

    這篇文章主要給大家介紹了關于ios設備使用iframe寬度超出屏幕的解決方法,文中通過示例代碼介紹的非常詳細,對各位ios開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • iOS應用中UITableView左滑自定義選項及批量刪除的實現(xiàn)

    iOS應用中UITableView左滑自定義選項及批量刪除的實現(xiàn)

    這篇文章主要介紹了iOS應用中UITableView左滑自定義選項及批量刪除的實現(xiàn),UITableView列表中即通訊錄左滑呼出選項的那種效果在刪除時能夠實現(xiàn)多行刪除將更加方便,需要的朋友可以參考下
    2016-03-03
  • iOS實現(xiàn)循環(huán)滾動公告欄

    iOS實現(xiàn)循環(huán)滾動公告欄

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)循環(huán)滾動公告欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03

最新評論