iOS开发中的并发、串行队列,同步、异步任务

在多线程开发中我们经常会遇到这些概念:并发队列、串行队列、同步任务、异步任务。我们将这四个概念进行组合会有四种结果:串行队列+同步任务、串行队列+异步任务、并发队列+同步任务、并发队列+异步任务。我们对这四种结果进行解释:

1.串行队列+同步任务:不会开启新的线程,任务逐步完成。

2.串行队列+异步任务:开启新的线程,任务逐步完成。

3.并发队列+同步任务:不会开启新的线程,任务逐步完成。

4.并发队列+异步任务:开启新的线程,任务同步完成。

我们如果要让任务在新的线程中完成,应该使用异步线程。为了提高效率,我们还应该将任务放在并发队列中。因此在开发中使用最多的是并发队列+异步任务。看代码:

// 串行队列+同步任务

- (void)serialSyn{

    dispatch_queue_t queue =dispatch_queue_create("serial",DISPATCH_QUEUE_SERIAL);

    dispatch_sync(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"3---%@", [NSThreadcurrentThread]);

        }

    });

}

// 串行队列+异步任务

- (void)serialAsyn{

    dispatch_queue_t queue =dispatch_queue_create("serial",DISPATCH_QUEUE_SERIAL);

    dispatch_async(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"3---%@", [NSThreadcurrentThread]);

        }

    });

}

// 并发队列+同步任务

- (void)concurrenSyn{

    dispatch_queue_t queue =dispatch_queue_create("concurrent",DISPATCH_QUEUE_CONCURRENT);

    dispatch_sync(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"3---%@", [NSThreadcurrentThread]);

        }

    });

    

}

// 并发队列+异步任务

- (void)concurrentAsyn{

    dispatch_queue_t queue =dispatch_queue_create("concurrent",DISPATCH_QUEUE_SERIAL);

    dispatch_async(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i <3; i ++) {

            NSLog(@"3---%@", [NSThreadcurrentThread]);

        }

    });

}


输出结果分别为:
//  串行队列+同步任务

2017-12-09 15:50:18.427989+0800 GCD[1044:27175] 1---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:20.912799+0800 GCD[1044:27277] XPC connection interrupted

2017-12-09 15:50:21.429204+0800 GCD[1044:27175] 1---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:24.430559+0800 GCD[1044:27175] 1---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:25.431937+0800 GCD[1044:27175] 2---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:26.433460+0800 GCD[1044:27175] 2---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:27.433913+0800 GCD[1044:27175] 2---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:28.435443+0800 GCD[1044:27175] 3---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:29.435987+0800 GCD[1044:27175] 3---<NSThread: 0x600000073600>{number = 1, name = main}

2017-12-09 15:50:30.437512+0800 GCD[1044:27175] 3---<NSThread: 0x600000073600>{number = 1, name = main}


// 串行队列+异步任务

2017-12-09 16:08:03.242688+0800 GCD[1252:41773] 1---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:06.246989+0800 GCD[1252:41773] 1---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:09.249206+0800 GCD[1252:41773] 1---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:10.250193+0800 GCD[1252:41773] 2---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:11.250878+0800 GCD[1252:41773] 2---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:12.255908+0800 GCD[1252:41773] 2---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:13.258215+0800 GCD[1252:41773] 3---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:14.258708+0800 GCD[1252:41773] 3---<NSThread: 0x600000276ec0>{number = 3, name = (null)}

2017-12-09 16:08:15.259192+0800 GCD[1252:41773] 3---<NSThread: 0x600000276ec0>{number = 3, name = (null)}


// 串行队列+异步任务

2017-12-09 16:09:23.548551+0800 GCD[1283:42913] 1---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:25.547003+0800 GCD[1283:43153] XPC connection interrupted

2017-12-09 16:09:26.549830+0800 GCD[1283:42913] 1---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:29.550406+0800 GCD[1283:42913] 1---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:30.551134+0800 GCD[1283:42913] 2---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:31.552655+0800 GCD[1283:42913] 2---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:32.553861+0800 GCD[1283:42913] 2---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:33.555326+0800 GCD[1283:42913] 3---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:34.555842+0800 GCD[1283:42913] 3---<NSThread: 0x604000260540>{number = 1, name = main}

2017-12-09 16:09:35.556267+0800 GCD[1283:42913] 3---<NSThread: 0x604000260540>{number = 1, name = main}


// 并发队列+异步任务

2017-12-09 16:10:26.676320+0800 GCD[1309:44938] 3---<NSThread: 0x604000466c40>{number = 3, name = (null)}

2017-12-09 16:10:26.676320+0800 GCD[1309:44937] 2---<NSThread: 0x60000066e380>{number = 4, name = (null)}

2017-12-09 16:10:27.678310+0800 GCD[1309:44938] 3---<NSThread: 0x604000466c40>{number = 3, name = (null)}

2017-12-09 16:10:27.678316+0800 GCD[1309:44937] 2---<NSThread: 0x60000066e380>{number = 4, name = (null)}

2017-12-09 16:10:28.674756+0800 GCD[1309:44935] 1---<NSThread: 0x60000066fa80>{number = 5, name = (null)}

2017-12-09 16:10:28.679805+0800 GCD[1309:44938] 3---<NSThread: 0x604000466c40>{number = 3, name = (null)}

2017-12-09 16:10:28.679798+0800 GCD[1309:44937] 2---<NSThread: 0x60000066e380>{number = 4, name = (null)}

2017-12-09 16:10:31.675355+0800 GCD[1309:44935] 1---<NSThread: 0x60000066fa80>{number = 5, name = (null)}

2017-12-09 16:10:34.678046+0800 GCD[1309:44935] 1---<NSThread: 0x60000066fa80>{number = 5, name = (null)}



看表格:


注意:

在主队列中添加同步任务会产生死锁,进而导致程序崩溃。

代码:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    NSLog(@"===========1");

    

    dispatch_sync(dispatch_get_main_queue(), ^{

       NSLog(@"===========2");

    });

    

    NSLog(@"===========3");

    

}

以上代码在打印出1之后就卡死在了dispatch_sync...的代码上。







  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值