iOS 并行队列、串行队列和线程

在开发过程中经常用到队列和线程结合使用,给队列中添加任务有四种使用方法:串行队列中执行同步任务、串行队列中执行异步任务、并行队列中执行同步任务、并行队列中执行异步任务,还有主队列,下面先说下主队列

一 .  主队列

主队列:专门负责调度主线程度的任务,没有办法开辟新的线程。所以,在主队列下的任务不管是异步任务还是同步任务都不会开辟线程,任务只会在主线程顺序执行。

1 . 主队列异步任务

主队列中放入异步任务,不是马上执行,而是等到主队列中的其它所有除我们使用代码添加到主队列的任务都执行完毕之后,才会执行我们使用代码添加的任务。

// 主队列异步
    NSLog(@"------------------1");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        NSLog(@"主队列异步   %@",[NSThread currentThread]);
    });
    NSLog(@"------------------2");

执行结果:

2018-05-10 14:12:49.069959+0800 QueueAndThread[363:77419] ------------------1
2018-05-10 14:12:49.070013+0800 QueueAndThread[363:77419] ------------------2
2018-05-10 14:12:49.077403+0800 QueueAndThread[363:77419] 主队列异步   <NSThread: 0x100e06230>{number = 1, name = main}

2 . 主队列同步任务

容易阻塞主线程,因为代码任务需要马上执行,但是主线程正在执行代码任务的方法体,因此代码任务就必须等待,而主线程又在等待代码任务的完成好去完成下面的任务,因此就形成了相互等待,造成了死锁

// 主队列同步
    NSLog(@"------------------1");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        NSLog(@"主队列同步   %@",[NSThread currentThread]);
    });
    NSLog(@"------------------2");

执行结果:造成主线程堵塞,程序奔溃

二 . 串行队列

1 . 串行队列执行同步任务

在主线程中依次执行任务,不会开启新线程

    dispatch_queue_t queue =dispatch_queue_create("serial",DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task1------%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task2------%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task3------%@", [NSThread currentThread]);
        }
    });

执行结果:

2018-05-10 14:22:01.526245+0800 QueueAndThread[366:79694] task1------<NSThread: 0x10200bb10>{number = 1, name = main}
2018-05-10 14:22:01.526387+0800 QueueAndThread[366:79694] task1------<NSThread: 0x10200bb10>{number = 1, name = main}
2018-05-10 14:22:01.526536+0800 QueueAndThread[366:79694] task1------<NSThread: 0x10200bb10>{number = 1, name = main}
2018-05-10 14:22:01.526600+0800 QueueAndThread[366:79694] task2------<NSThread: 0x10200bb10>{number = 1, name = main}
2018-05-10 14:22:01.526656+0800 QueueAndThread[366:79694] task2------<NSThread: 0x10200bb10>{number = 1, name = main}
2018-05-10 14:22:01.526712+0800 QueueAndThread[366:79694] task2------<NSThread: 0x10200bb10>{number = 1, name = main}
2018-05-10 14:22:01.526769+0800 QueueAndThread[366:79694] task3------<NSThread: 0x10200bb10>{number = 1, name = main}
2018-05-10 14:22:01.526825+0800 QueueAndThread[366:79694] task3------<NSThread: 0x10200bb10>{number = 1, name = main}
2018-05-10 14:22:01.526881+0800 QueueAndThread[366:79694] task3------<NSThread: 0x10200bb10>{number = 1, name = main}

2 . 串行队列执行异步任务

在主线程之外新建一个线程,在新建的线程中依次执行任务

    dispatch_queue_t queue =dispatch_queue_create("serial",DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task1------%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task2------%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task3------%@", [NSThread currentThread]);
        }
    });

执行结果:

2018-05-10 14:25:31.433976+0800 QueueAndThread[369:80781] task1------<NSThread: 0x151dacb80>{number = 3, name = (null)}
2018-05-10 14:25:31.437090+0800 QueueAndThread[369:80781] task1------<NSThread: 0x151dacb80>{number = 3, name = (null)}
2018-05-10 14:25:31.437206+0800 QueueAndThread[369:80781] task1------<NSThread: 0x151dacb80>{number = 3, name = (null)}
2018-05-10 14:25:31.437279+0800 QueueAndThread[369:80781] task2------<NSThread: 0x151dacb80>{number = 3, name = (null)}
2018-05-10 14:25:31.437338+0800 QueueAndThread[369:80781] task2------<NSThread: 0x151dacb80>{number = 3, name = (null)}
2018-05-10 14:25:31.437395+0800 QueueAndThread[369:80781] task2------<NSThread: 0x151dacb80>{number = 3, name = (null)}
2018-05-10 14:25:31.437458+0800 QueueAndThread[369:80781] task3------<NSThread: 0x151dacb80>{number = 3, name = (null)}
2018-05-10 14:25:31.437520+0800 QueueAndThread[369:80781] task3------<NSThread: 0x151dacb80>{number = 3, name = (null)}
2018-05-10 14:25:31.437578+0800 QueueAndThread[369:80781] task3------<NSThread: 0x151dacb80>{number = 3, name = (null)}

三 . 并行队列

1 . 并行队列执行同步任务

主线程中依次执行任务,不会开启新线程

    dispatch_queue_t queue =dispatch_queue_create("concurrent",DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task1------%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task2------%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task3------%@", [NSThread currentThread]);
        }
    });

执行结果:

2018-05-10 14:27:45.302096+0800 QueueAndThread[371:81436] task1------<NSThread: 0x103d08290>{number = 1, name = main}
2018-05-10 14:27:45.302242+0800 QueueAndThread[371:81436] task1------<NSThread: 0x103d08290>{number = 1, name = main}
2018-05-10 14:27:45.302304+0800 QueueAndThread[371:81436] task1------<NSThread: 0x103d08290>{number = 1, name = main}
2018-05-10 14:27:45.302365+0800 QueueAndThread[371:81436] task2------<NSThread: 0x103d08290>{number = 1, name = main}
2018-05-10 14:27:45.302423+0800 QueueAndThread[371:81436] task2------<NSThread: 0x103d08290>{number = 1, name = main}
2018-05-10 14:27:45.302479+0800 QueueAndThread[371:81436] task2------<NSThread: 0x103d08290>{number = 1, name = main}
2018-05-10 14:27:45.302593+0800 QueueAndThread[371:81436] task3------<NSThread: 0x103d08290>{number = 1, name = main}
2018-05-10 14:27:45.302672+0800 QueueAndThread[371:81436] task3------<NSThread: 0x103d08290>{number = 1, name = main}
2018-05-10 14:27:45.302731+0800 QueueAndThread[371:81436] task3------<NSThread: 0x103d08290>{number = 1, name = main}

2 . 并行队列执行异步任务

三个任务同时执行

dispatch_queue_t queue =dispatch_queue_create("concurrent",DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task1------%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task2------%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i =0; i <3; i ++) {
            NSLog(@"task3------%@", [NSThread currentThread]);
        }
    });

执行结果:

2018-05-10 14:32:18.543310+0800 QueueAndThread[379:83194] task1------<NSThread: 0x10097b0e0>{number = 3, name = (null)}
2018-05-10 14:32:18.543483+0800 QueueAndThread[379:83194] task1------<NSThread: 0x10097b0e0>{number = 3, name = (null)}
2018-05-10 14:32:18.543548+0800 QueueAndThread[379:83194] task1------<NSThread: 0x10097b0e0>{number = 3, name = (null)}
2018-05-10 14:32:18.543607+0800 QueueAndThread[379:83190] task2------<NSThread: 0x10096f1d0>{number = 4, name = (null)}
2018-05-10 14:32:18.545328+0800 QueueAndThread[379:83194] task3------<NSThread: 0x10097b0e0>{number = 3, name = (null)}
2018-05-10 14:32:18.545428+0800 QueueAndThread[379:83194] task3------<NSThread: 0x10097b0e0>{number = 3, name = (null)}
2018-05-10 14:32:18.545490+0800 QueueAndThread[379:83194] task3------<NSThread: 0x10097b0e0>{number = 3, name = (null)}
2018-05-10 14:32:18.545571+0800 QueueAndThread[379:83190] task2------<NSThread: 0x10096f1d0>{number = 4, name = (null)}
2018-05-10 14:32:18.545637+0800 QueueAndThread[379:83190] task2------<NSThread: 0x10096f1d0>{number = 4, name = (null)}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值