iOS学习笔记-106.多线程05——CGD同步、异步函数和并行、串行、主队列示例

来源:互联网 发布:智能优化算法的优缺点 编辑:程序博客网 时间:2024/05/17 21:44

  • 多线程05CGD同步异步函数和并行串行主队列示例
    • 一说明与图示
    • 二异步函数 并发队列
      • 1 代码
      • 2 结果
    • 三异步函数 串行队列
      • 1 代码示例
      • 2 结果
    • 四同步函数 并发队列
      • 1 代码示例
      • 2 结果
    • 五同步函数 串行队列
      • 1 代码示例
      • 2 结果
    • 六异步函数 主队列
      • 1 代码示例
      • 2 结果
    • 七同步函数 主队列 死锁除非该方法在子线程中执行
      • 1 代码示例
      • 2 结果
    • 八同步函数子线程 主队列
      • 1 代码示例
      • 2 结果

多线程05——CGD同步、异步函数和并行、串行、主队列示例

一、说明与图示

我们主要完成以下的主要示例

1.异步函数 + 并发队列 —— 会开启多条线程,队列中的任务是并发执行

2.异步函数 + 串行队列 —— 会开线程,开一条线程,队列中的任务是串行执行的

3.同步函数 + 并发队列 —— 不会开线程,任务是串行执行的

4.同步函数 + 串行队列 —— 不会开线程,任务是串行执行的

5.异步函数 + 主队列 —— 所有任务都在主线程中执行,不会开线程

6.同步函数 + 主队列 —— 死锁。除非该方法在子线程中执行

7.同步函数(子线程) + 主队列

特别说明:

使用sync函数往当前串行队列中添加任务,会卡住当前的串行队列(死锁

图示:

这里写图片描述


二、异步函数 + 并发队列

我们点击对应按钮的时候去执行这个操作

2.1 代码

- (IBAction)acClick:(id)sender {    NSLog(@"--------acClick---------");    [self asyncConcurrent];}/* 1.异步函数 + 并发队列*/-(void)asyncConcurrent{    //1. 获取队列    //第一个参数:c语言的字符串,标签    //第二个参数:队列的类型    // DISPATCH_QUEUE_CONCURRENT 并发    // DISPATCH_QUEUE_SERIAL 串行    dispatch_queue_t queue = dispatch_queue_create("com.wiming.asyncConcurrent",DISPATCH_QUEUE_CONCURRENT);    //2.封装任务>添加任务到队列中    dispatch_async(queue, ^{        NSLog(@"asyncConcurrent---download1-----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"asyncConcurrent---download2-----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"asyncConcurrent---download3-----%@",[NSThread currentThread]);    });}

2.2 结果

[57261:318823] --------acClick---------[57261:326249] asyncConcurrent---download1-----<NSThread: 0x608000278840>{number = 6, name = (null)}[57261:326263] asyncConcurrent---download2-----<NSThread: 0x60000026ae00>{number = 7, name = (null)}[57261:326264] asyncConcurrent---download3-----<NSThread: 0x60000026a540>{number = 8, name = (null)}

三、异步函数 + 串行队列

3.1 代码示例

- (IBAction)asClick:(id)sender {    NSLog(@"--------asClick---------");    [self asyncSerial];}/* 2.异步函数 + 串行队列 */-(void)asyncSerial{    dispatch_queue_t queue = dispatch_queue_create("com.wiming.asyncSerial", DISPATCH_QUEUE_SERIAL);    dispatch_async(queue, ^{        NSLog(@"asyncSerial---download1-----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"asyncSerial---download2-----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"asyncSerial---download3-----%@",[NSThread currentThread]);    });}

3.2 结果

[57261:318823] --------asClick---------[57261:327801] asyncSerial---download1-----<NSThread: 0x60000026ab80>{number = 9, name = (null)}[57261:327801] asyncSerial---download2-----<NSThread: 0x60000026ab80>{number = 9, name = (null)}[57261:327801] asyncSerial---download3-----<NSThread: 0x60000026ab80>{number = 9, name = (null)}

四、同步函数 + 并发队列

4.1 代码示例

- (IBAction)scClick:(id)sender {    NSLog(@"--------scClick---------");    [self syncConcurrent];}/* 3.同步函数 + 并发队列 */-(void)syncConcurrent{    dispatch_queue_t queue = dispatch_queue_create("com.wiming.syncConcurrent", DISPATCH_QUEUE_CONCURRENT);    dispatch_sync(queue, ^{        NSLog(@"syncConcurrent---download1-----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"syncConcurrent---download2-----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"syncConcurrent---download3-----%@",[NSThread currentThread]);    });}

4.2 结果

[57261:318823] --------scClick---------[57261:318823] syncConcurrent---download1-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncConcurrent---download2-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncConcurrent---download3-----<NSThread: 0x608000263600>{number = 1, name = main}

五、同步函数 + 串行队列

5.1 代码示例

- (IBAction)ssClick:(id)sender {    NSLog(@"--------ssClick---------");    [self syncSerial];}/* 4.同步函数 + 串行队列*/-(void)syncSerial{    dispatch_queue_t queue = dispatch_queue_create("com.wiming.syncConcurrent", DISPATCH_QUEUE_SERIAL);    dispatch_sync(queue, ^{        NSLog(@"syncConcurrent---download1-----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"syncConcurrent---download2-----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"syncConcurrent---download3-----%@",[NSThread currentThread]);    });}

5.2 结果

[57261:318823] --------ssClick---------[57261:318823] syncConcurrent---download1-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncConcurrent---download2-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncConcurrent---download3-----<NSThread: 0x608000263600>{number = 1, name = main}

六、异步函数 + 主队列

6.1 代码示例

- (IBAction)amClick:(id)sender {    NSLog(@"--------amClick---------");    [self asyncMain];}/* 5.异步函数 + 主队列*/-(void)asyncMain{    dispatch_queue_t queue = dispatch_get_main_queue();    dispatch_async(queue, ^{        NSLog(@"asyncMain---download1-----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"asyncMain---download2-----%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"asyncMain---download3-----%@",[NSThread currentThread]);    });}

6.2 结果

[57261:318823] --------amClick---------[57261:318823] asyncMain---download1-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] asyncMain---download2-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] asyncMain---download3-----<NSThread: 0x608000263600>{number = 1, name = main}

七、同步函数 + 主队列 :死锁。除非该方法在子线程中执行

7.1 代码示例

- (IBAction)smClick:(id)sender {    NSLog(@"--------smClick---------");    [self syncMain];}/* 6.同步函数 + 主队列 :死锁。除非该方法在子线程中执行*/-(void)syncMain{    dispatch_queue_t queue = dispatch_get_main_queue();    dispatch_sync(queue, ^{        NSLog(@"syncMain---download1-----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"syncMain---download2-----%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"syncMain---download3-----%@",[NSThread currentThread]);    });}

7.2 结果

2017-09-02 23:09:45.567 03_UIview81多线程GCD[57261:318823] --------smClick---------然后程序就挂了

八、同步函数(子线程) + 主队列

8.1 代码示例

- (IBAction)smNewThreadClick:(id)sender {    NSLog(@"--------smNewThreadClick---------");    [self syncMainSonThread];}/* 7.同步函数(子线程) + 主队列 */-(void)syncMainSonThread{    dispatch_queue_t queue = dispatch_get_main_queue();    [NSThread detachNewThreadWithBlock:^{        dispatch_sync(queue, ^{            NSLog(@"syncMainSonThread---download1-----%@",[NSThread currentThread]);        });        dispatch_sync(queue, ^{            NSLog(@"syncMainSonThread---download2-----%@",[NSThread currentThread]);        });        dispatch_sync(queue, ^{            NSLog(@"syncMainSonThread---download3-----%@",[NSThread currentThread]);        });    }];}

8.2 结果

[57261:318823] --------smNewThreadClick---------[57261:318823] syncMainSonThread---download1-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncMainSonThread---download2-----<NSThread: 0x608000263600>{number = 1, name = main}[57261:318823] syncMainSonThread---download3-----<NSThread: 0x608000263600>{number = 1, name = main}

阅读全文
0 0
原创粉丝点击