多線程pthread、NSThread和GCD的實(shí)現(xiàn)

一、pthread

首先,導(dǎo)入#import <pthread.h>框架,然后再創(chuàng)建線程

pthread_t myRestrict;
pthread_create(&myRestrict, NULL, run, NULL);

函數(shù)方法實(shí)現(xiàn)

void *run(void *data) {
    
    for (NSInteger i = 0; i < 10000; i++) {
        NSLog(@"touchesBegan----%ld-----%@", i, [NSThread currentThread]);
    }
    return NULL;
}

二、NSThread

創(chuàng)建線程的方式一

/// 創(chuàng)建線程的方式1
- (void)createThread1 {
    
    // 創(chuàng)建線程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadWithUrlString:) object:@"http://b.png"];
    thread.name = @"下載線程";
    
    // 啟動(dòng)線程(調(diào)用self的downloadWithUrlString方法)
    [thread start];
}

創(chuàng)建線程的方式二

/// 創(chuàng)建線程的方式2
- (void)createThread2 {
    
//    [NSThread detachNewThreadWithBlock:^{
//
//        NSLog(@"---%@---", [NSThread currentThread]);
//    }];
    [NSThread detachNewThreadSelector:@selector(downloadWithUrlString:) toTarget:self withObject:@"http://b.png"];
}

創(chuàng)建線程的方式三

/// 創(chuàng)建線程的方式3
- (void)createThread3 {
    
    // 這兩個(gè)不會(huì)創(chuàng)建線程,只在當(dāng)前線程中執(zhí)行
//    [self performSelector:@selector(downloadWithUrlString:) withObject:@"http://c.gif"];
//    [self downloadWithUrlString:@"http://c.gif"];

    [self performSelectorInBackground:@selector(downloadWithUrlString:) withObject:@"http://c.gif"];
}

函數(shù)實(shí)現(xiàn)

- (void)downloadWithUrlString:(NSString *)urlString {
    
    NSLog(@"download---%@---%@", urlString, [NSThread currentThread]);
}

三、GCD

dispatch_sync : 同步,不具備開啟線程的能力
dispatch_async : 異步,具備開啟線程的能力

并發(fā)隊(duì)列 :多個(gè)任務(wù)可以同時(shí)執(zhí)行
串行隊(duì)列 :一個(gè)任務(wù)執(zhí)行完后,再執(zhí)行下一個(gè)任務(wù)

Foundation : OC
Core Foundation : C語(yǔ)言
Foundation和Core Foundation框架的數(shù)據(jù)類型可以互相轉(zhuǎn)換的

 NSString *str = @"123"; // Foundation
 CFStringRef str2 = (__bridge CFStringRef)str; // Core Foundation
 NSString *str3 = (__bridge NSString *)str2;
     CFArrayRef ---- NSArray
     CFDictionaryRef ---- NSDictionary
     CFNumberRef ---- NSNumber

Core Foundation中手動(dòng)創(chuàng)建的數(shù)據(jù)類型,都需要手動(dòng)釋放

     CFArrayRef array = CFArrayCreate(NULL, NULL, 10, NULL);
     CFRelease(array);
     CGPathRef path = CGPathCreateMutable();
     CGPathRetain(path);

     CGPathRelease(path);
     CGPathRelease(path);

凡是函數(shù)名中帶有create\copy\new\retain等字眼, 都應(yīng)該在不需要使用這個(gè)數(shù)據(jù)的時(shí)候進(jìn)行release
GCD的數(shù)據(jù)類型在ARC環(huán)境下不需要再做release
CF(Core Foundation)的數(shù)據(jù)類型在ARC\MRC環(huán)境下都需要再做release

1、async -- 并發(fā)隊(duì)列(最常用)
/**
 *  async -- 并發(fā)隊(duì)列(最常用)
 *  會(huì)不會(huì)創(chuàng)建線程:會(huì),一般同時(shí)開多條
 *  任務(wù)的執(zhí)行方式:并發(fā)執(zhí)行
 */
- (void)asyncGlobalQueue {
    
    // 獲得全局的并發(fā)隊(duì)列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 將 任務(wù) 添加 全局隊(duì)列 中去 異步 執(zhí)行
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
    });
}
2、async -- 串行隊(duì)列(有時(shí)候用)
/**
 *  async -- 串行隊(duì)列(有時(shí)候用)
 *  會(huì)不會(huì)創(chuàng)建線程:會(huì),一般只開1條線程
 *  任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))
 */
- (void)asyncSerialQueue {
    
    // 1.創(chuàng)建一個(gè)串行隊(duì)列
    dispatch_queue_t queue = dispatch_queue_create("com.asliving.queue", NULL);
    
    // 2.將任務(wù)添加到串行隊(duì)列中 異步 執(zhí)行
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
    });
    
    // 3.非ARC,需要釋放創(chuàng)建的隊(duì)列
    //    dispatch_release(queue);
}
3、async -- 主隊(duì)列(很常用)
/**
 *  async -- 主隊(duì)列(很常用)
 */
- (void)asyncMainQueue {
    
    // 1.主隊(duì)列(添加到主隊(duì)列中的任務(wù),都會(huì)自動(dòng)放到主線程中去執(zhí)行)
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 2.添加 任務(wù) 到主隊(duì)列中 異步 執(zhí)行
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
    });
}
4、sync -- 主隊(duì)列(不能用---會(huì)卡死)
/**
 *  sync -- 主隊(duì)列(不能用---會(huì)卡死)
 */
- (void)syncMainQueue {
    
    NSLog(@"syncMainQueue----begin--");
    
    // 1.主隊(duì)列(添加到主隊(duì)列中的任務(wù),都會(huì)自動(dòng)放到主線程中去執(zhí)行)
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 2.添加 任務(wù) 到主隊(duì)列中 異步 執(zhí)行
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
    });
    
    NSLog(@"syncMainQueue----end--");
}

-----------------------------華麗的分割線------------------------------

5、sync -- 并發(fā)隊(duì)列
/**
 *  sync -- 并發(fā)隊(duì)列
 *  會(huì)不會(huì)創(chuàng)建線程:不會(huì)
 *  任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))
 *  并發(fā)隊(duì)列失去了并發(fā)的功能
 */
- (void)syncGlobalQueue {
    
    // 獲得全局的并發(fā)隊(duì)列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 將 任務(wù) 添加到 全局并發(fā)隊(duì)列 中 同步 執(zhí)行
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
    });
}
6、sync -- 串行隊(duì)列
/**
 *  sync -- 串行隊(duì)列
 *  會(huì)不會(huì)創(chuàng)建線程:不會(huì)
 *  任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))
 */
- (void)syncSerialQueue {
    
    // 創(chuàng)建一個(gè)串行隊(duì)列
    dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);
    
    // 將 任務(wù) 添加到 串行隊(duì)列 中 同步 執(zhí)行
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);
    });
}

Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容