iOS開發多線程-NSOperation,NSOperationQueue

1.簡介

NSOperation、NSOperationQueue是基于GCD的封裝,完全面向對象。比GCD更為簡單,代碼可讀性也高了不少。
NSOperation和NSOperationQueue有不少有點,如下

  • 可以添加完成代碼,在操作完成后執行
  • 可以添加操作之間的順序
  • 設定操作之間的優先級
  • 可以方便的取消操作
  • 可以觀察操作執行狀態:isCancelled, isAsynchronous, isExecuting, isFinished, isReady等等

既然是基于GCD封裝的,那么有些概念還是可以套用一下的。比如操作對應任務
隊列對應操作隊列

  • 操作

    • 執行操作就是在線程中執行代碼
    • 使用NSOperation的子類NSInvocationOperation,NSBlockOperation,或者自定義子類來封裝操作
  • 操作隊列

    • 操作隊列就是存放操作的隊列,但是和GCD的隊列不一樣,不遵循FIFO原則。首先通過依賴關系(dependencies)進入準備就緒狀態(ready)。依賴關系是絕對的,哪怕是多線程情況下,也會等待之前的依賴線程執行完才能執行當前操作。
    • 操作隊列通過設置最大并發操作數(maxConcurrentOperationCount)來控制并發,串行
    • NSOperationQueue提供了兩種類型的隊列,一種是主隊列和自定義隊列。主隊列運行在主線程上,自定義隊列在后臺執行。

2.使用步驟

NSOperation操作步驟

  1. 創建操作:將要執行的操作封裝到一個NSOperation對象中。
  2. 創建隊列:創建NSOperationQueue對象
  3. 將操作添加到隊列中:將NSOperation對象添加到NSOperationQueue中

2.1 創建操作

NSOperation是一個抽象類,需要我們使用它的子類。有以下三種選擇:

  1. NSInvocationOperation
  2. NSBlockOperation
  3. 自定義繼承自NSOperation的子類,實現相應方法來封裝操作

2.1.1 NSInvocationOperation

- (void)invocation{
    NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task) object:nil];
    [op start];
}

- (void)task1{
    for (int i = 0; i < 2; i++) {
        [NSThread sleepForTimeInterval:2];
        NSLog(@"1---%@", [NSThread currentThread]);
    }
}

2018-05-17 14:17:02.903373+0800 MultiThread[13966:1261901] 1---<NSThread: 0x604000074f80>{number = 1, name = main}
2018-05-17 14:17:04.905077+0800 MultiThread[13966:1261901] 1---<NSThread: 0x604000074f80>{number = 1, name = main}

    [NSThread detachNewThreadSelector:@selector(invocation) toTarget:self withObject:nil];

2018-05-17 14:17:44.535875+0800 MultiThread[13991:1263666] 1---<NSThread: 0x60000046de80>{number = 3, name = (null)}
2018-05-17 14:17:46.541399+0800 MultiThread[13991:1263666] 1---<NSThread: 0x60000046de80>{number = 3, name = (null)}

不使用NSOperationQueue,會直接在主線程中執行操作,不會創建新線程。如果在其他線程中執行,就會打印其他線程

2.1.2 NSBlockOperation

- (void)block{
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"1---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [op start];
}

2018-05-17 14:20:32.340855+0800 MultiThread[14056:1269157] 1---<NSThread: 0x60400006f140>{number = 1, name = main}
2018-05-17 14:20:34.342273+0800 MultiThread[14056:1269157] 1---<NSThread: 0x60400006f140>{number = 1, name = main}

NSBlockOperation提供了一個方法addExecutionBlock,通過這個方法,就能為NSBlockOperation添加額外的操作,只有在blockOperationWithBlockaddExecutionBlock中的操作全部完成后,才算完成。如果添加的操作過多,系統有可能會開啟新的線程來完成操作。

- (void)block{
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"1---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"2---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"3---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"4---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"5---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [op addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"6---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    
    [op start];
}

- (void)invocation{
    NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task1) object:nil];
    [op start];
}

2018-05-17 14:28:06.052693+0800 MultiThread[14158:1281194] 1---<NSThread: 0x60400007ff40>{number = 1, name = main}
2018-05-17 14:28:06.052693+0800 MultiThread[14158:1281347] 3---<NSThread: 0x600000467600>{number = 3, name = (null)}
2018-05-17 14:28:06.052698+0800 MultiThread[14158:1281349] 2---<NSThread: 0x6040004727c0>{number = 4, name = (null)}
2018-05-17 14:28:06.052795+0800 MultiThread[14158:1281348] 4---<NSThread: 0x600000467500>{number = 5, name = (null)}
2018-05-17 14:28:08.054454+0800 MultiThread[14158:1281194] 1---<NSThread: 0x60400007ff40>{number = 1, name = main}
2018-05-17 14:28:08.054454+0800 MultiThread[14158:1281347] 3---<NSThread: 0x600000467600>{number = 3, name = (null)}
2018-05-17 14:28:08.054454+0800 MultiThread[14158:1281349] 2---<NSThread: 0x6040004727c0>{number = 4, name = (null)}
2018-05-17 14:28:08.054525+0800 MultiThread[14158:1281348] 4---<NSThread: 0x600000467500>{number = 5, name = (null)}
2018-05-17 14:28:10.055459+0800 MultiThread[14158:1281194] 6---<NSThread: 0x60400007ff40>{number = 1, name = main}
2018-05-17 14:28:10.055531+0800 MultiThread[14158:1281347] 5---<NSThread: 0x600000467600>{number = 3, name = (null)}
2018-05-17 14:28:12.056989+0800 MultiThread[14158:1281194] 6---<NSThread: 0x60400007ff40>{number = 1, name = main}
2018-05-17 14:28:12.056989+0800 MultiThread[14158:1281347] 5---<NSThread: 0x600000467600>{number = 3, name = (null)}

打印結果可以看出:AddExecutionBlockblockOperationWithBlock兩者是在不同的線程中執行的。所以如果任務過多,是有可能會開啟新線程的。另外,blockOperationWithBlock的操作有可能還在開啟的新線程執行。

2.1.3 使用自定義的NSOperation子類

當系統提供的兩個子類不能滿足我們的要求時,我們可以通過自定義繼承自NSOperation的子類。重寫mainstart方法來定義自己的NSOperation對象。

//CusOperation.h
#import <Foundation/Foundation.h>

@interface CusOperation : NSOperation

@end

//CusOperation.m
#import "CusOperation.h"

@implementation CusOperation

- (void)main{
    if (!self.isCancelled) {
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@", [NSThread currentThread]);
        }
    }
}

@end

- (void)customOperation{
    CusOperation *op = [[CusOperation alloc]init];
    [op start];
}

2018-05-17 14:38:16.129219+0800 MultiThread[14331:1299438] 1---<NSThread: 0x600000079340>{number = 1, name = main}
2018-05-17 14:38:18.129706+0800 MultiThread[14331:1299438] 1---<NSThread: 0x600000079340>{number = 1, name = main}

以上示例可以看出:

在沒有使用 NSOperationQueue、在主線程單獨使用自定義繼承自 NSOperation 的子類的情況下,是在主線程執行操作,并沒有開啟新線程

2.2 創建隊列

NSOperationQueue有兩種隊列:主隊列和自定義隊列。自定義隊列包含了串行、并發功能。

  • 主隊列
    • 添加到主隊列的操作,都會方法哦主線程中執行。
NSOperationQueue *queue = [NSOperationQueue mainQueue];
  • 自定義隊列
    • 添加到該隊列的操作,會自動放到子線程中執行
    • 同時包含了串行,并發功能
// 自定義隊列創建方法
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

2.3 將操作添加到隊列中

將操作添加到操作隊列中區,有兩種方法:

  1. - (void)addOperation:(NSOperation *)op;
    先創建操作,再講創建好的操作添加到創建好的隊列中去
- (void)addOperationToQueue{
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task1) object:nil];
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task2) object:nil];
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"3---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    
    [op3 addExecutionBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"Finished --- %@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];

}
  1. - (void)addOperationWithBlock:(void (^)(void))block;
    不需要創建操作,在block中直接添加操作,直接將包含操作的block添加到隊列中。
- (void)addOperationByBlock{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 2.使用 addOperationWithBlock: 添加操作到隊列中
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"1---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"2---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"3---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
}

2018-05-17 15:09:17.220840+0800 MultiThread[14694:1339685] 1---<NSThread: 0x60400027ce40>{number = 3, name = (null)}
2018-05-17 15:09:17.220840+0800 MultiThread[14694:1338535] 3---<NSThread: 0x6000006652c0>{number = 4, name = (null)}
2018-05-17 15:09:17.220870+0800 MultiThread[14694:1339697] 2---<NSThread: 0x60400027e040>{number = 5, name = (null)}
2018-05-17 15:09:19.223380+0800 MultiThread[14694:1339685] 1---<NSThread: 0x60400027ce40>{number = 3, name = (null)}
2018-05-17 15:09:19.223399+0800 MultiThread[14694:1338535] 3---<NSThread: 0x6000006652c0>{number = 4, name = (null)}
2018-05-17 15:09:19.223464+0800 MultiThread[14694:1339697] 2---<NSThread: 0x60400027e040>{number = 5, name = (null)}

可以看出開啟了新線程,并發執行。

3. NSOperationQueue控制串行,并行

最大并發操作數maxConcurrentOperationCount,可以用來控制一個特定隊列中可以有多少個操作同時并發執行。

  • maxConcurrentOperationCount默認-1,表示不做限制,可以進行并發操作
  • maxConcurrentOperationCount等于1,隊列為串行隊列,只能串行執行
  • maxConcurrentOperationCount大于1時,隊列為并發隊列,可以進行并發執行
- (void)setMaxConcurrentOperationCount{
    // 1.創建隊列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 2.設置最大并發操作數
    queue.maxConcurrentOperationCount = 1; // 串行隊列
    // queue.maxConcurrentOperationCount = 2; // 并發隊列
    // queue.maxConcurrentOperationCount = 6; // 并發隊列
    
    // 3.添加操作
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"1---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"2---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    [queue addOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"3---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
}

設置maxConcurrentOperationCount等于1的時候。

2018-05-17 15:14:48.876623+0800 MultiThread[14777:1347945] 1---<NSThread: 0x600000461d40>{number = 3, name = (null)}
2018-05-17 15:14:50.879393+0800 MultiThread[14777:1347945] 1---<NSThread: 0x600000461d40>{number = 3, name = (null)}
2018-05-17 15:14:52.884111+0800 MultiThread[14777:1347942] 2---<NSThread: 0x60400007a980>{number = 4, name = (null)}
2018-05-17 15:14:54.888856+0800 MultiThread[14777:1347942] 2---<NSThread: 0x60400007a980>{number = 4, name = (null)}
2018-05-17 15:14:56.893411+0800 MultiThread[14777:1347945] 3---<NSThread: 0x600000461d40>{number = 3, name = (null)}
2018-05-17 15:14:58.895721+0800 MultiThread[14777:1347945] 3---<NSThread: 0x600000461d40>{number = 3, name = (null)}

設置maxConcurrentOperationCount等于2的時候。

2018-05-17 15:16:59.695592+0800 MultiThread[14819:1352085] 2---<NSThread: 0x600000663f40>{number = 3, name = (null)}
2018-05-17 15:16:59.695593+0800 MultiThread[14819:1352087] 1---<NSThread: 0x604000270f80>{number = 4, name = (null)}
2018-05-17 15:17:01.701006+0800 MultiThread[14819:1352087] 1---<NSThread: 0x604000270f80>{number = 4, name = (null)}
2018-05-17 15:17:01.701010+0800 MultiThread[14819:1352085] 2---<NSThread: 0x600000663f40>{number = 3, name = (null)}
2018-05-17 15:17:03.706541+0800 MultiThread[14819:1352088] 3---<NSThread: 0x60400027a640>{number = 5, name = (null)}
2018-05-17 15:17:05.711808+0800 MultiThread[14819:1352088] 3---<NSThread: 0x60400027a640>{number = 5, name = (null)}

設置maxConcurrentOperationCount等于6的時候。

2018-05-17 15:17:46.013273+0800 MultiThread[14843:1353840] 2---<NSThread: 0x600000462a00>{number = 5, name = (null)}
2018-05-17 15:17:46.013280+0800 MultiThread[14843:1353842] 1---<NSThread: 0x604000461780>{number = 4, name = (null)}
2018-05-17 15:17:46.013280+0800 MultiThread[14843:1353841] 3---<NSThread: 0x600000461c80>{number = 3, name = (null)}
2018-05-17 15:17:48.016936+0800 MultiThread[14843:1353842] 1---<NSThread: 0x604000461780>{number = 4, name = (null)}
2018-05-17 15:17:48.016936+0800 MultiThread[14843:1353840] 2---<NSThread: 0x600000462a00>{number = 5, name = (null)}
2018-05-17 15:17:48.016982+0800 MultiThread[14843:1353841] 3---<NSThread: 0x600000461c80>{number = 3, name = (null)}

4. NSOperation操作依賴

通過操作依賴,我們可以很輕松的控制操作執行順序,有以下三個方法屬性,幫助我們管理依賴關系。

  1. - (void)addDependency:(NSOperation *)op;添加依賴,使當前操作依賴于操作 op 的完成。
  2. - (void)removeDependency:(NSOperation *)op;移除依賴,取消當前操作對操作 op 的依賴。
  3. @property (readonly, copy) NSArray<NSOperation *> *dependencies;在當前操作開始執行之前完成執行的所有操作對象數組。
- (void)addDependency {
    
    // 1.創建隊列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 2.創建操作
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"1---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"2---%@", [NSThread currentThread]); // 打印當前線程
        }
    }];
    
    // 3.添加依賴
    [op2 addDependency:op1]; // 讓op2 依賴于 op1,則先執行op1,在執行op2
    
    // 4.添加操作到隊列中
    [queue addOperation:op1];
    [queue addOperation:op2];
}

2018-05-17 15:28:00.975331+0800 MultiThread[14961:1367884] 1---<NSThread: 0x60000027cac0>{number = 3, name = (null)}
2018-05-17 15:28:02.980260+0800 MultiThread[14961:1367884] 1---<NSThread: 0x60000027cac0>{number = 3, name = (null)}
2018-05-17 15:28:04.984993+0800 MultiThread[14961:1367885] 2---<NSThread: 0x60400027ccc0>{number = 4, name = (null)}
2018-05-17 15:28:06.990673+0800 MultiThread[14961:1367885] 2---<NSThread: 0x60400027ccc0>{number = 4, name = (null)}

添加依賴后,永遠是op1先執行。

5. NSOperation優先級

NSOperation 提供了queuePriority(優先級)屬性,queuePriority屬性適用于同一操作隊列中的操作,不適用于不同操作隊列中的操作。默認情況下,所有新創建的操作對象優先級都是NSOperationQueuePriorityNormal。但是我們可以通過setQueuePriority:方法來改變當前操作在同一隊列中的執行優先級。

// 優先級的取值
typedef NS_ENUM(NSInteger, NSOperationQueuePriority) {
    NSOperationQueuePriorityVeryLow = -8L,
    NSOperationQueuePriorityLow = -4L,
    NSOperationQueuePriorityNormal = 0,
    NSOperationQueuePriorityHigh = 4,
    NSOperationQueuePriorityVeryHigh = 8
};

在沒有依賴關系的操作之間,可以通過優先級來決定操作開始執行的順序。

6. NSOperation,NSOperationQueue線程間的通信

- (void)communication{
    // 1.創建隊列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    // 2.添加操作
    [queue addOperationWithBlock:^{
        // 異步進行耗時操作
        for (int i = 0; i < 2; i++) {
            [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
            NSLog(@"1---%@", [NSThread currentThread]); // 打印當前線程
        }
        
        // 回到主線程
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            // 進行一些 UI 刷新等操作
            for (int i = 0; i < 2; i++) {
                [NSThread sleepForTimeInterval:2]; // 模擬耗時操作
                NSLog(@"2---%@", [NSThread currentThread]); // 打印當前線程
            }
        }];
    }];
}

2018-05-17 15:37:56.670333+0800 MultiThread[15086:1383288] 1---<NSThread: 0x604000470f40>{number = 3, name = (null)}
2018-05-17 15:37:58.672749+0800 MultiThread[15086:1383288] 1---<NSThread: 0x604000470f40>{number = 3, name = (null)}
2018-05-17 15:38:00.674389+0800 MultiThread[15086:1382902] 2---<NSThread: 0x60000006f000>{number = 1, name = main}
2018-05-17 15:38:02.675024+0800 MultiThread[15086:1382902] 2---<NSThread: 0x60000006f000>{number = 1, name = main}

線程中的通信,是先執行其他子線程的操作,然后回到主線程執行相關操作。

7.線程安全

線程同步安全問題,可以通過NSLock來實現,以之前GCD中有關smemaphore的賣票代碼為例子,只需要在賣票操作之前加上[self.lock lock];在操作完成后解鎖[self.lock unlock];即可。

- (void)saleTicketSafe {
    while (1) {

        // 加鎖
        [self.lock lock];

        if (self.ticketSurplusCount > 0) {
            //如果還有票,繼續售賣
            self.ticketSurplusCount--;
            NSLog(@"%@", [NSString stringWithFormat:@"剩余票數:%d 窗口:%@", self.ticketSurplusCount, [NSThread currentThread]]);
            [NSThread sleepForTimeInterval:0.2];
        }

        // 解鎖
        [self.lock unlock];

        if (self.ticketSurplusCount <= 0) {
            NSLog(@"所有火車票均已售完");
            break;
        }
    }
}

8. 其他屬性,方法

8.1 NSOperation屬性和方法

1.取消操作方法

- (void)cancel; 可取消操作,實質是標記 isCancelled 狀態。

2.判斷操作狀態方法

- (BOOL)isFinished; 判斷操作是否已經結束。
- (BOOL)isCancelled; 判斷操作是否已經標記為取消。
- (BOOL)isExecuting; 判斷操作是否正在在運行。
- (BOOL)isReady; 判斷操作是否處于準備就緒狀態,這個值和操作的依賴關系相關。

3.操作同步

- (void)waitUntilFinished; 阻塞當前線程,直到該操作結束。可用于線程執行順序的同步。
- (void)setCompletionBlock:(void (^)(void))block; completionBlock 會在當前操作執行完畢時執行 completionBlock。
- (void)addDependency:(NSOperation *)op; 添加依賴,使當前操作依賴于操作 op 的完成。
- (void)removeDependency:(NSOperation *)op; 移除依賴,取消當前操作對操作 op 的依賴。
@property (readonly, copy) NSArray<NSOperation *> *dependencies; 在當前操作開始執行之前完成執行的所有操作對象數組。

8.2 NSOperationQueue屬性和方法

1.取消/暫停/恢復操作

- (void)cancelAllOperations; 可以取消隊列的所有操作。
- (BOOL)isSuspended; 判斷隊列是否處于暫停狀態。 YES 為暫停狀態,NO 為恢復狀態。
- (void)setSuspended:(BOOL)b; 可設置操作的暫停和恢復,YES 代表暫停隊列,NO 代表恢復隊列。

2.操作同步

- (void)waitUntilAllOperationsAreFinished; 阻塞當前線程,直到隊列中的操作全部執行完畢。

3.添加/獲取操作

- (void)addOperationWithBlock:(void (^)(void))block; 向隊列中添加一個 NSBlockOperation 類型操作對象。
- (void)addOperations:(NSArray *)ops waitUntilFinished:(BOOL)wait; 向隊列中添加操作數組,wait 標志是否阻塞當前線程直到所有操作結束
- (NSArray *)operations; 當前在隊列中的操作數組(某個操作執行結束后會自動從這個數組清除)。
- (NSUInteger)operationCount; 當前隊列中的操作數。

4.獲取隊列

+ (id)currentQueue; 獲取當前隊列,如果當前線程不是在 NSOperationQueue 上運行則返回 nil。
+ (id)mainQueue; 獲取主隊列。

參考
iOS多線程:『NSOperation、NSOperationQueue』詳盡總結
并發編程指南
NSOperation
并發編程:API 及挑戰

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容