iOS 多线程之任务和队列

iOS 多线程之任务和队列

前言

学习多线程,肯定要了解GCD,GCD两个最核心的概念就是:任务和队列。所以学习好多线程,首先要把任务和队列吃透,才能能好的使用多线程。

为什么使用GCD?

因为使用 GCD 有很多好处啊,具体如下:

  • GCD 可用于多核的并行运算;
  • GCD 会自动利用更多的 CPU 内核(比如双核、四核);
  • GCD 会自动管理线程的生命周期(创建线程、调度任务、销毁线程);
  • 程序员只需要告诉 GCD 想要执行什么任务,不需要编写任何线程管理代码。

任务

任务:就是执行操作的意思,换句话说就是你在线程中执行的那段代码。在 GCD 中是放在 block 中的。

执行任务有两种方式:

  1. 同步执行
    • 同步添加任务到指定的队列中,在添加的任务执行结束之前,会一直等待,直到队列里面的任务完成之后再继续执行。
    • 只能在当前线程中执行任务,不具备开启新线程的能力。
  2. 异步执行
    • 异步添加任务到指定的队列中,它不会做任何等待,可以继续执行任务。
    • 可以在新的线程中执行任务,具备开启新线程的能力。

两者的主要区别是:是否等待队列的任务执行结束,以及是否具备开启新线程的能力。

队列

这里的队列指执行任务的等待队列,即用来存放任务的队列。

  • 队列是一种特殊的线性表

  • 采用 FIFO(先进先出)的原则,即新任务总是被插入到队列的末尾,而读取任务的时候总是从队列的头部开始读取。

  • 每读取一个任务,则从队列中释放一个任务。队列的结构可参考下图:

队列.png

队列有两种:

  • 串行队列(Serial Dispatch Queue)

    • 每次只有一个任务被执行。让任务一个接着一个地执行。(只开启一个线程,一个任务执行完毕后,再执行下一个任务)
  • 并行队列 (Concurrent Dispatch Queue)

    • 可以让多个任务并发(同时)执行。(可以开启多个线程,并且同时执行任务)

并发队列.png

注意:并发队列 的并发功能只有在异步(dispatch_async)方法下才有效。

GCD使用步骤

GCD 的使用步骤其实很简单只有两步:

  1. 首先创建一个队列(串行队列或并发队列);
  2. 最后将任务追加到任务的等待队列中,然后系统就会根据任务类型执行任务(同步执行或异步执行)

队列使用

可以使用 dispatch_queue_create 方法来创建队列。

  • 第一个参数表示队列的唯一标识符,用于 DEBUG,可为空。队列的名称推荐使用应用程序 ID 这种逆序全程域名。
  • 第二个参数用来识别是串行队列还是并发队列。DISPATCH_QUEUE_SERIAL 表示串行队列,DISPATCH_QUEUE_CONCURRENT 表示并发队列。
串行队列创建
  • 创建串行队列

    // 串行队列的创建方法
    dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_SERIAL);
    
  • 系统提供串行队列-主队列(Main Dispatch Queue)

    // 主队列的获取方法
    dispatch_queue_t queue = dispatch_get_main_queue();
    
  • 所有放在主队列中的任务,都会放到主线程中执行。

  • 系统提供dispatch_get_main_queue() 方法获得主队列。

注意:主队列其实并不特殊。 主队列的实质上就是一个普通的串行队列,只是因为默认情况下,当前代码是放在主队列中的,然后主队列中的代码,有都会放到主线程中去执行,所以才造成了主队列特殊的现象。

并行队列创建
  • 创建并行队列

    // 并发队列的创建方法
    dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
    

  • 系统提供的-全局并发队列(Global Dispatch Queue)

    // 全局并发队列的获取方法
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    ```
    
  • 可以使用 dispatch_get_global_queue 方法来获取全局并发队列。

  • 需要传入两个参数。第一个参数表示队列优先级,一般用 DISPATCH_QUEUE_PRIORITY_DEFAULT。第二个参数暂时没用,用 0 即可。

创建任务方法

  • 同步执行任务

    // 同步执行任务创建方法
    dispatch_sync(queue, ^{
    // 这里放同步执行任务代码
    });
    
  • 异步执行任务

    // 异步执行任务创建方法
    dispatch_async(queue, ^{
    // 这里放异步执行任务代码
    });
    

任务和队列的组合

既然我们有两种队列(串行队列 / 并发队列),两种任务执行方式(同步执行 / 异步执行),那么我们就有了四种不同的组合方式。
这四种不同的组合方式是。

两种默认队列:全局并发队列、主队列:

  • 全局并发队列可以作为普通并发队列来使用。

  • 当前代码默认放在主队列中,所以主队列很有必要专门来研究一下,所以我们就又多了两种组合方式。

这样就有六种不同的组合方式了。

  1. 同步执行 + 串行队列
  2. 同步执行 + 并行队列
  3. 异步执行 + 串行队列
  4. 异步执行 + 并行队列
  5. 同步执行 + 主队列
  6. 异步执行 + 主队列

1.同步执行+串行队列

- (void)syncSerial {

    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"syncSerial---begin");
    //创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("testSerialQueue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_sync(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
          // 模拟耗时操作
            [NSThread sleepForTimeInterval:2];             
            // 打印当前线程
            NSLog(@"1---%@",[NSThread currentThread]);      
        }
    });
    dispatch_sync(queue, ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"2---%@",[NSThread currentThread]);     
        }
    });
    dispatch_sync(queue, ^{
        // 追加任务3
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"3---%@",[NSThread currentThread]);    
        }
    });
    
    NSLog(@"syncSerial---end");
}

执行打印结果:

输出结果:
currentThread---<NSThread: 0x604000079400>{number = 1, name = main}
syncSerial---begin
1---<NSThread: 0x604000079400>{number = 1, name = main}
1---<NSThread: 0x604000079400>{number = 1, name = main}
2---<NSThread: 0x604000079400>{number = 1, name = main}
2---<NSThread: 0x604000079400>{number = 1, name = main}
3---<NSThread: 0x604000079400>{number = 1, name = main}
3---<NSThread: 0x604000079400>{number = 1, name = main}
syncSerial---end

同步+串行的特点:不会开启新线程,在当前线程执行任务。任务是串行的,执行完一个任务,再执行下一个任务。

2.同步执行+并行队列

- (void)syncConcurrent {

    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"syncConcurrent---begin");
    //创建并行队列
    dispatch_queue_t queue = dispatch_queue_create("testConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);

    dispatch_sync(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
          // 模拟耗时操作
            [NSThread sleepForTimeInterval:2];             
            // 打印当前线程
            NSLog(@"1---%@",[NSThread currentThread]);      
        }
    });
    
    dispatch_sync(queue, ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"2---%@",[NSThread currentThread]);     
        }
    });
    
    dispatch_sync(queue, ^{
        // 追加任务3
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"3---%@",[NSThread currentThread]);    
        }
    });
    
    NSLog(@"syncConcurrent---end");
}

打印结果:

currentThread---<NSThread: 0x60400006bbc0>{number = 1, name = main}
syncConcurrent---begin
1---<NSThread: 0x60400006bbc0>{number = 1, name = main}
1---<NSThread: 0x60400006bbc0>{number = 1, name = main}
2---<NSThread: 0x60400006bbc0>{number = 1, name = main}
2---<NSThread: 0x60400006bbc0>{number = 1, name = main}
3---<NSThread: 0x60400006bbc0>{number = 1, name = main}
3---<NSThread: 0x60400006bbc0>{number = 1, name = main}
syncConcurrent---end

打印结果表明:
在当前线程中执行任务,不会开启新线程,执行完一个任务,再执行下一个任务。

执行完同步+串行、同步+并行结果表明:

  • 同步情况串行和并行执行结果一样
  • 同步不具有开启新线程的能力
  • 并行队列虽然后并行执行任务的能力,但是在同步的情况下,没有开启新线程的能力,所以相当于串行执行(并行在同步的情况下,难免想起英雄无用武之地)。

3.异步执行+串行队列

- (void)asyncSerial {
    //打印当前线程
    NSLog(@"currentThread---%@",[NSThread currentThread]);   打印当前线程
    NSLog(@"asyncSerial---begin");
    
    dispatch_queue_t queue = dispatch_queue_create("testSerialQueue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];     
            NSLog(@"1---%@",[NSThread currentThread]);    
        }
    });
    dispatch_async(queue, ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"2---%@",[NSThread currentThread]); 
        }
    });
    dispatch_async(queue, ^{
        // 追加任务3
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"3---%@",[NSThread currentThread]);
        }
    });
    
    NSLog(@"asyncSerial---end");
}

打印结果:

currentThread---<NSThread: 0x604000070440>{number = 1, name = main}
asyncSerial---begin
asyncSerial---end
1---<NSThread: 0x60000026e100>{number = 3, name = (null)}
1---<NSThread: 0x60000026e100>{number = 3, name = (null)}
2---<NSThread: 0x60000026e100>{number = 3, name = (null)}
2---<NSThread: 0x60000026e100>{number = 3, name = (null)}
3---<NSThread: 0x60000026e100>{number = 3, name = (null)}
3---<NSThread: 0x60000026e100>{number = 3, name = (null)}

直接结果表明:异步情况下会开启新线程,但是因为任务是串行的,执行完一个任务,再执行下一个任务。

4.异步执行+并行队列

- (void)asyncConcurrent {
    // 打印当前线程
    NSLog(@"currentThread---%@",[NSThread currentThread]); 
    NSLog(@"asyncConcurrent---begin");
    
    dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"1---%@",[NSThread currentThread]);    
        }
    });
    
    dispatch_async(queue, ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"2---%@",[NSThread currentThread]);     
        }
    });
    
    dispatch_async(queue, ^{
        // 追加任务3
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"3---%@",[NSThread currentThread]); 
        }
    });
    
    NSLog(@"asyncConcurrent---end");
}

执行打印结果:

currentThread---<NSThread: 0x604000062d80>{number = 1, name = main}
asyncConcurrent---begin
2---<NSThread: 0x604000266f00>{number = 5, name = (null)}
3---<NSThread: 0x60000026f200>{number = 4, name = (null)}
1---<NSThread: 0x600000264800>{number = 3, name = (null)}
3---<NSThread: 0x60000026f200>{number = 4, name = (null)}
1---<NSThread: 0x600000264800>{number = 3, name = (null)}
2---<NSThread: 0x604000266f00>{number = 5, name = (null)}

执行结果表明:可以开启多个子线程,任务同时并行交替执行

在异步执行任务的情况,串行队列和并行队列表明:

  • 异步情况下是开启新线程
  • 异步下的串行也是是执行完一个任务,再执行下一个任务
  • 异步下的并行开启多个子线程同时交替执行多个任务

4.同步执行+主队列

- (void)syncMain {
   
   NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
   NSLog(@"syncMain---begin");
   dispatch_queue_t syncMain = dispatch_get_main_queue();
   
   dispatch_sync(syncMain, ^{
       // 追加任务1
       for (int i = 0; i < 2; ++i) {
           [NSThread sleepForTimeInterval:2];             
           NSLog(@"1---%@",[NSThread currentThread]);     
       }
   });
   
   dispatch_sync(syncMain, ^{
       // 追加任务2
       for (int i = 0; i < 2; ++i) {
           [NSThread sleepForTimeInterval:2];             
           NSLog(@"2---%@",[NSThread currentThread]);      
       }
   });
   
   dispatch_sync(syncMain, ^{
       // 追加任务3
       for (int i = 0; i < 2; ++i) {
           [NSThread sleepForTimeInterval:2];              
           NSLog(@"3---%@",[NSThread currentThread]); 
       }
   });
   
   NSLog(@"syncMain---end");
}

执行结果:

currentThread---<NSThread: 0x600000078a00>{number = 1, name = main}
syncMain---begin

直接结果表明:互等卡主不执行,产生死锁崩溃。

默认主线程在等待syncMain执行完任务1再往下执行,syncMain在等待默认主线程执行完再执行syncMain中任务1,所以互相等待产生死锁。

6.异步执行+主队列

- (void)asyncMain {
    // 打印当前线程
    NSLog(@"currentThread---%@",[NSThread currentThread]); 
    NSLog(@"asyncMain---begin");
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"1---%@",[NSThread currentThread]);  
        }
    });
    
    dispatch_async(queue, ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"2---%@",[NSThread currentThread]);     
        }
    });
    
    dispatch_async(queue, ^{
        // 追加任务3
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              
            NSLog(@"3---%@",[NSThread currentThread]);     
        }
    });
    
    NSLog(@"asyncMain---end");
}

执行打印结果:

currentThread---<NSThread: 0x60000006d440>{number = 1, name = main}
asyncMain---begin
asyncMain---end
1---<NSThread: 0x60000006d440>{number = 1, name = main}
1---<NSThread: 0x60000006d440>{number = 1, name = main}
2---<NSThread: 0x60000006d440>{number = 1, name = main}
2---<NSThread: 0x60000006d440>{number = 1, name = main}
3---<NSThread: 0x60000006d440>{number = 1, name = main}
3---<NSThread: 0x60000006d440>{number = 1, name = main}

因为主线程是串行队列,所以在主线程中执行任务,执行完一个任务,再执行下一个任务。

总结

  • 串行队列的特点:

    • 无论同步任务或是异步任务,任务按顺序执行,一个执行完毕执行下一个任务
    • 串行队列 执行同步任务不开辟线程
    • 执行异步任务开辟最多开辟一条线程并且按顺序执行
  • 并行队列的特点:

    • 执行异步任务具备开辟多条线程的能力
    • 执行同步任务,顺序执行,因为同步不具有开辟线程的能力
  • 同步任务特点:

    • 没有开启新线程的能力
    • 在当前线程任务按顺序一个一个执行
  • 异步任务的特点:

    • 具有开启新线程的能力
    • 开几条新线程取决于队列,串行队列开启一条线程,并行队列在执行多个异步任务时会开辟多条线程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值