iOS 進(jìn)程&線(xiàn)程&多線(xiàn)程

2018年下半年學(xué)習(xí).png

寫(xiě)在前面吧:
上圖是下半年的學(xué)習(xí)計(jì)劃進(jìn)度腦圖,默默堅(jiān)持~~
這篇文 只能算是一個(gè)觀看筆記,感謝不死鳥(niǎo)大佬的教學(xué),看完受益良多

進(jìn)程

五態(tài)模型

線(xiàn)程

  • 線(xiàn)程是進(jìn)程的基本執(zhí)行單元
  • 進(jìn)程的所有任務(wù)都是在線(xiàn)程中執(zhí)行


多線(xiàn)程

  • 網(wǎng)絡(luò)請(qǐng)求
  • 圖片加載
  • 文件處理
  • 數(shù)據(jù)存儲(chǔ)
  • 任務(wù)執(zhí)行等

串行
串行耗時(shí)
并行

并行


多線(xiàn)程實(shí)現(xiàn)原理

單核

多核

多線(xiàn)程優(yōu)缺點(diǎn)

優(yōu)點(diǎn):
  • 簡(jiǎn)化了編程模型
  • 更加輕量級(jí)
  • 提高執(zhí)行效率
  • 提高資源利用率
缺點(diǎn):
  • 增加了程序設(shè)計(jì)的復(fù)雜性
  • 占用內(nèi)存空間
  • 增加了CPU的調(diào)度開(kāi)銷(xiāo)

多線(xiàn)程實(shí)現(xiàn)技術(shù)方案

pThread[基于C,移植性強(qiáng)]
  • 代碼:
///pThread
- (void)setupThreadWithpThread
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 30);
    [btn setTitle:@"pThread" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickPThread) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
- (void)clickPThread
{
    NSLog(@"我在主線(xiàn)程中執(zhí)行");
    pthread_t pthread;
    pthread_create(&pthread, NULL, run, NULL);
}
void *run(void *data)
{
    NSLog(@"我在子線(xiàn)程中執(zhí)行");
    for (int i=1; i<10; i++) {
        NSLog(@"%d",i);
        sleep(1);
    }
    return NULL;
}
  • 控制臺(tái):
2018-07-05 16:12:14.838406+0800 TestThread[4503:324104] 我在主線(xiàn)程中執(zhí)行
2018-07-05 16:12:14.839701+0800 TestThread[4503:324183] 我在子線(xiàn)程中執(zhí)行
2018-07-05 16:12:14.841281+0800 TestThread[4503:324183] 1
2018-07-05 16:12:15.844024+0800 TestThread[4503:324183] 2
2018-07-05 16:12:16.845006+0800 TestThread[4503:324183] 3
2018-07-05 16:12:17.845463+0800 TestThread[4503:324183] 4
2018-07-05 16:12:18.848984+0800 TestThread[4503:324183] 5
2018-07-05 16:12:19.853491+0800 TestThread[4503:324183] 6
2018-07-05 16:12:20.857345+0800 TestThread[4503:324183] 7
2018-07-05 16:12:21.859202+0800 TestThread[4503:324183] 8
2018-07-05 16:12:22.860372+0800 TestThread[4503:324183] 9


NSThread
  • 通過(guò)alloc init的方式創(chuàng)建并執(zhí)行線(xiàn)程,可控制線(xiàn)程name屬性
NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(runThread1) object:nil];
[thread1 start];
  • 通過(guò)detachNewThreadSelector 方式創(chuàng)建并執(zhí)行線(xiàn)程
[NSThread detachNewThreadSelector:@selector(runThread1) toTarget:self withObject:nil];
  • 通過(guò)detachNewThreadSelector 方式創(chuàng)建并執(zhí)行線(xiàn)程
[NSThread detachNewThreadSelector:@selector(runThread1) toTarget:self withObject:nil];
  • 代碼:
- (void)setupThreadWithNSThread
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 150, 100, 30);
    [btn setTitle:@"NSThread" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickNSThread) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)clickNSThread
{
    NSLog(@"我在主線(xiàn)程中執(zhí)行");
    [self performSelectorInBackground:@selector(runThread1) withObject:nil];
}

- (void)runThread1
{
    NSLog(@"我在子線(xiàn)程中執(zhí)行");
    for (int i=1; i<10; i++) {
        NSLog(@"%d",i);
        sleep(1);
        if (i == 9) {
            [self performSelectorOnMainThread:@selector(runMainThread) withObject:nil waitUntilDone:YES];
        }
    }
}
- (void)runMainThread
{
    NSLog(@"回到線(xiàn)程中執(zhí)行");
}
  • 控制臺(tái):
2018-07-05 16:34:08.496514+0800 TestThread[4718:338296] 我在主線(xiàn)程中執(zhí)行
2018-07-05 16:34:08.499321+0800 TestThread[4718:338376] 我在子線(xiàn)程中執(zhí)行
2018-07-05 16:34:08.500084+0800 TestThread[4718:338376] 1
2018-07-05 16:34:09.575477+0800 TestThread[4718:338376] 2
2018-07-05 16:34:10.576032+0800 TestThread[4718:338376] 3
2018-07-05 16:34:11.648309+0800 TestThread[4718:338376] 4
2018-07-05 16:34:12.721992+0800 TestThread[4718:338376] 5
2018-07-05 16:34:13.794289+0800 TestThread[4718:338376] 6
2018-07-05 16:34:14.869460+0800 TestThread[4718:338376] 7
2018-07-05 16:34:15.944381+0800 TestThread[4718:338376] 8
2018-07-05 16:34:17.018836+0800 TestThread[4718:338376] 9
2018-07-05 16:34:18.094780+0800 TestThread[4718:338296] 回到線(xiàn)程中執(zhí)行


GCD

注意的思考點(diǎn):
1->同步&異步
2->串行&并行
3->dispatch_get_main_queue() & dispatch_get_global_queue()
4->dispatch_group_enter & dispatch_group_leave

<-簡(jiǎn)單應(yīng)用->

  • 代碼:
- (void)setupThreadWithGCD
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 200, 100, 30);
    [btn setTitle:@"GCD" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickGCD) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)clickGCD
{
    NSLog(@"執(zhí)行GCD");
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       //執(zhí)行耗時(shí)任務(wù)
        NSLog(@"執(zhí)行耗時(shí)任務(wù) task1");
        [NSThread sleepForTimeInterval:3];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //回到主線(xiàn)程,刷新UI
            
            NSLog(@"回到主線(xiàn)程刷新UI");
        });
    });
}
  • 控制臺(tái):
2018-07-05 17:47:25.353341+0800 TestThread[5365:385948] 執(zhí)行GCD
2018-07-05 17:47:25.353779+0800 TestThread[5365:386006] 執(zhí)行耗時(shí)任務(wù) task1
2018-07-05 17:47:28.359846+0800 TestThread[5365:385948] 回到主線(xiàn)程刷新UI

<-按順序執(zhí)行->

  • 代碼:
/*
     * 執(zhí)行--結(jié)束的順序
     * DISPATCH_QUEUE_PRIORITY_LOW
     * DISPATCH_QUEUE_PRIORITY_HIGH
     * DISPATCH_QUEUE_PRIORITY_DEFAULT
*/
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSLog(@"執(zhí)行耗時(shí)任務(wù) task1");
        [NSThread sleepForTimeInterval:2];
        NSLog(@"結(jié)束耗時(shí)任務(wù) task1");
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"執(zhí)行耗時(shí)任務(wù) task2");
        [NSThread sleepForTimeInterval:2];
        NSLog(@"結(jié)束耗時(shí)任務(wù) task2");
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"執(zhí)行耗時(shí)任務(wù) task3");
        [NSThread sleepForTimeInterval:2];
        NSLog(@"結(jié)束耗時(shí)任務(wù) task3");
    });
  • 控制臺(tái):
2018-07-06 10:15:37.850583+0800 TestThread[951:33839] 執(zhí)行GCD
2018-07-06 10:15:37.850954+0800 TestThread[951:33906] 執(zhí)行耗時(shí)任務(wù) task2
2018-07-06 10:15:37.850963+0800 TestThread[951:33905] 執(zhí)行耗時(shí)任務(wù) task3
2018-07-06 10:15:37.850989+0800 TestThread[951:33908] 執(zhí)行耗時(shí)任務(wù) task1
2018-07-06 10:15:39.853563+0800 TestThread[951:33906] 結(jié)束耗時(shí)任務(wù) task2
2018-07-06 10:15:39.853581+0800 TestThread[951:33905] 結(jié)束耗時(shí)任務(wù) task3
2018-07-06 10:15:39.895970+0800 TestThread[951:33908] 結(jié)束耗時(shí)任務(wù) task1
NSOperation[基類(lèi)]``[線(xiàn)程池]``[最大并發(fā)數(shù)]

使用的兩種方式:
1-->NSInvocationOperation
2-->NSBlockOperation

  • 相關(guān)概念:

1、NSOperationQueue:

a. addOperation
b. setMaxConcurrentOperationCount
2、狀態(tài):
ready、cancelled、executingfinished、asynchronous
3、依賴(lài):
addDependency

  • 代碼:
- (void)setupThreadWithNSOperation
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 350, 150, 30);
    [btn setTitle:@"NSOperation" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickNSOperation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)clickNSOperation
{
    NSLog(@"我是主線(xiàn)程");
    /*
    NSBlockOperation *blockOper = [NSBlockOperation blockOperationWithBlock:^{
        for (int i=0; i < 3; i++) {
            NSLog(@"invocation--%d",i);
            [NSThread sleepForTimeInterval:1];
        }
    }];
    [blockOper start];
    */

    /*
    NSInvocationOperation * invocationOper = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(invocationAction) object:nil];
    
    //start 方式啟用,會(huì)是在當(dāng)前程執(zhí)行`[同步-非并發(fā)]`
    [invocationOper start];
    */
    
    //開(kāi)啟子線(xiàn)程,則會(huì)阻塞子線(xiàn)程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSInvocationOperation * invocationOper = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(invocationAction) object:nil];
        
        [invocationOper start];
    });
}
- (void)invocationAction
{
    for (int i=0; i < 3; i++) {
        NSLog(@"invocation--%d",i);
        [NSThread sleepForTimeInterval:1];
    }
}
  • 控制臺(tái):
2018-07-06 14:20:10.971880+0800 TestThread[2760:150878] 我是主線(xiàn)程
2018-07-06 14:20:10.973186+0800 TestThread[2760:150932] invocation--0
2018-07-06 14:20:11.977305+0800 TestThread[2760:150932] invocation--1
2018-07-06 14:20:12.981491+0800 TestThread[2760:150932] invocation--2 

結(jié)束語(yǔ)

再次說(shuō)明,這只是一篇筆記,要想真正用好多線(xiàn)程,短期內(nèi)估計(jì)是不行,需要多練習(xí)了,共勉之~

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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