多執行緒的初級使用,新手福利

weixin_34075551發表於2017-08-08

多執行緒的初級使用,新手福利

多執行緒的使用可以明顯的提高程式碼的執行效率,尤其在耗時操作時候,比如獲取網路請求資料、視訊解碼,圖片載入等等方面,下面我就把多執行緒的常用方法給簡單介紹一下

多執行緒分類:

同步(sy) 非同步(asy)
序列 同一執行緒挨個執行 其他執行緒挨個執行
並行 同一執行緒挨個執行 多個執行緒同時執行

NSThread的使用方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //Nsthread 使用方法
    //使用方法一
    [NSThread detachNewThreadWithBlock:^{
        [self test_01];
    }];
    //使用方法二
    NSThread *thread =[[NSThread alloc]initWithTarget:self selector:@selector(test_02) object:nil];
    [thread start];
}


-(void)test_01{
    for (int i = 0; i<1000; i++) {
        NSLog(@"**********");
    }
}
-(void)test_02{
    for (int i = 0; i<1000; i++) {
        NSLog(@"##########");
    }
}

下面是控制檯輸出:

2017-08-08 09:33:21.239 Thread_GCDDeom[2467:102054] **********
2017-08-08 09:33:21.240 Thread_GCDDeom[2467:102055] ##########
2017-08-08 09:33:21.240 Thread_GCDDeom[2467:102054] **********
2017-08-08 09:33:21.240 Thread_GCDDeom[2467:102055] ##########
2017-08-08 09:33:21.240 Thread_GCDDeom[2467:102054] **********
2017-08-08 09:33:21.240 Thread_GCDDeom[2467:102055] ##########
2017-08-08 09:33:21.241 Thread_GCDDeom[2467:102054] **********
2017-08-08 09:33:21.241 Thread_GCDDeom[2467:102055] ##########

NSThread的其他用法還有:(這裡不做解釋)

+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
+ (void)exit;
+ (double)threadPriority;   //獲取執行緒的優先順序
+ (BOOL)setThreadPriority:(double)p; //這個是設定執行緒的優先順序

從上面的例子可以看得出,NSThread是蘋果封裝好的多執行緒,他是非同步操作的

GCD的使用方法:

//測試一:建立一個同步執行緒---序列佇列(兩種方法建立序列佇列)
//    dispatch_queue_t queue_01 =dispatch_queue_create("queue01", NULL);
    dispatch_queue_t queue_01 =dispatch_queue_create("queue01", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue_01, ^{
        [self test_01];
    });
    dispatch_sync(queue_01, ^{
        [self test_02];
    });

執行結果:

2017-08-08 10:14:46.331 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.331 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.334 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.334 Thread_GCDDeom[2787:147269] ##########

//    //測試二:建立一個同步執行緒----並行佇列
    dispatch_queue_t queue_02 =dispatch_queue_create("queue02", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue_02, ^{
        [self test_01];
    });
    dispatch_sync(queue_02, ^{
        [self test_02];
    });

執行結果:

2017-08-08 10:14:46.331 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.331 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.334 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.334 Thread_GCDDeom[2787:147269] ##########

    //測試三:建立一個非同步執行緒---序列佇列
    dispatch_queue_t queue_03 =dispatch_queue_create("queue03", NULL);
    dispatch_async(queue_03, ^{
        [self test_01];
    });
    dispatch_async(queue_03, ^{
        [self test_02];
    });

執行結果:

2017-08-08 10:14:46.331 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.331 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.332 Thread_GCDDeom[2787:147269] **********
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.333 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.334 Thread_GCDDeom[2787:147269] ##########
2017-08-08 10:14:46.334 Thread_GCDDeom[2787:147269] ##########

    dispatch_queue_t queue_04 =dispatch_queue_create("queue04", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue_04, ^{
        [self test_01];
    });
    dispatch_async(queue_04, ^{
        [self test_02];
    });

執行結果:

2017-08-08 10:23:18.687 Thread_GCDDeom[2911:154462] ##########
2017-08-08 10:23:18.687 Thread_GCDDeom[2911:154508] **********
2017-08-08 10:23:18.687 Thread_GCDDeom[2911:154462] ##########
2017-08-08 10:23:18.687 Thread_GCDDeom[2911:154508] **********
2017-08-08 10:23:18.688 Thread_GCDDeom[2911:154462] ##########
2017-08-08 10:23:18.688 Thread_GCDDeom[2911:154508] **********
2017-08-08 10:23:18.688 Thread_GCDDeom[2911:154462] ##########
2017-08-08 10:23:18.688 Thread_GCDDeom[2911:154508] **********
2017-08-08 10:23:18.688 Thread_GCDDeom[2911:154462] ##########
2017-08-08 10:23:18.688 Thread_GCDDeom[2911:154508] **********

教程到此結束,如果您需要Demo請點選此處

如有疑問Email : grammermao@gmail.com

最後還是歡迎您的star(☆☆☆☆☆),您的支援是我繼續寫demo的無線動力

相關文章