iOS 開發多線程 —— NSOperation

来源:http://www.cnblogs.com/lfyDragon/archive/2017/08/15/7364852.html
-Advertisement-
Play Games

本文是根據文頂頂老師的博客學習而來,轉載地址:http://www.cnblogs.com/wendingding/p/3809042.html 一、NSOperation簡介 1.簡單說明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能實現多線程 ...


本文是根據文頂頂老師的博客學習而來,轉載地址:http://www.cnblogs.com/wendingding/p/3809042.html

一、NSOperation簡介

1.簡單說明

NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能實現多線程編程

NSOperation和NSOperationQueue實現多線程的具體步驟:

(1)先將需要執行的操作封裝到一個NSOperation對象中

(2)然後將NSOperation對象添加到NSOperationQueue中

(3)系統會⾃動將NSOperationQueue中的NSOperation取出來

(4)將取出的NSOperation封裝的操作放到⼀條新線程中執⾏

 2.NSOperation的子類

NSOperation是個抽象類,並不具備封裝操作的能力,必須使⽤它的子類

使用NSOperation⼦類的方式有3種:

(1)NSInvocationOperation

(2)NSBlockOperation

(3)自定義子類繼承NSOperation,實現內部相應的⽅法

 

NSInvocationOperation:

示例代碼:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil];
    [operation start];
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil];
    [operation2 start];
    
}

-(void)thread1{
    NSLog(@"currentThread = %@",[NSThread currentThread]);
}

-(void)thread2{
   NSLog(@"currentThread2 = %@",[NSThread currentThread]);
}
 currentThread = <NSThread: 0x17406d780>{number = 1, name = main}

currentThread2 = <NSThread: 0x17406d780>{number = 1, name = main}

可以看見,並沒有開啟新的線程.

註意:操作對象預設在主線程中執行,只有添加到隊列中才會開啟新的線程。即預設情況下,如果操作沒有放到隊列中queue中,都是同步執行。只有將NSOperation放到一個NSOperationQueue中,才會非同步執行操作 

 

NSBlockOperation:

 NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"operation = %@",[NSThread currentThread]);
    }];
    [operation3 start];



看列印結果:
operation = <NSThread: 0x1700728c0>{number = 1, name = main}

註意:只要NSBlockOperation封裝的操作數 > 1,就會非同步執行操作

例如:

NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"operation = %@",[NSThread currentThread]);
    }];
    [operation3 addExecutionBlock:^{
        NSLog(@"operation2 = %@",[NSThread currentThread]);
    }];
    [operation3 start];


然後看列印結果:

operation = <NSThread: 0x1700706c0>{number = 1, name = main}
operation2 = <NSThread: 0x170075a40>{number = 4, name = (null)}

 

NSOperationQueue:

NSOperationQueue的作⽤:NSOperation可以調⽤start⽅法來執⾏任務,但預設是同步執行的

如果將NSOperation添加到NSOperationQueue(操作隊列)中,系統會自動非同步執行NSOperation中的操作

添加操作到NSOperationQueue中,自動執行操作,自動開啟線程

 

示例:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil];

    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil];
   
    
    
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"currentThread3 = %@",[NSThread currentThread]);
    }];
    
    [operation3 addExecutionBlock:^{
        NSLog(@"currentThread3_1 = %@",[NSThread currentThread]);
    }];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //把操作添加到隊列中
    //第一種方式
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    //第二種方式
    [queue addOperationWithBlock:^{
        NSLog(@"currentThread4 = %@",[NSThread currentThread]);
    }];
    
}

-(void)thread1{
    NSLog(@"currentThread1 = %@",[NSThread currentThread]);
}

-(void)thread2{
   NSLog(@"currentThread2 = %@",[NSThread currentThread]);
}

查看結果:

currentThread1 = <NSThread: 0x17006f440>{number = 4, name = (null)}
currentThread2 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}
currentThread3_1 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}
currentThread3 = <NSThread: 0x17006f440>{number = 4, name = (null)}
currentThread4 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...