GCD的队列和任务理解

作者: 泰好笑勒 | 来源:发表于2017-05-18 11:29 被阅读90次

任务和队列

GCD 的两个核心:任务和队列。

任务:要执行的操作或方法函数。

队列:存放任务的集合,而我们要做的就是将任务添加到队列然后执行,GCD会自动将队列中的任务按先进先出的方式取出并交给对应线程执行。

1. 任务

加入任务时有两种形式:同步任务和异步任务。

同步任务:不会开启新的线程。会阻塞当前线程。

异步任务:会开启新的线程。不会阻塞当前线程。

2. 队列

队列有两种队列,串行队列和并行队列。

串行队列:是指队列中的任务是一个接一个的执行,队首的任务执行完毕后才能执行其后面的任务,直至执行队尾的任务。尽可能使用一个线程。

并行队列:是指队列中的任务可以并发的执行,即开始执行队首的任务后,不必等其执行完毕就可以接着开始执行队首之后的任务,因此在某一个时刻可能存在同时执行的多个任务。

队列和任务的四种组合方式:(swift)

// attributes 参数被指定为 concurrent 时,该队列为并发队列,不设置时,默认串行队列

let concurrentQueue = DispatchQueue(label: "com.concurrent.queue", qos: .utility, attributes: .concurrent)

let serailQueue = DispatchQueue(label: "com.serail.queue")

1.串行同步

func serialWithSync() {
    serailQueue.sync {
       print(Thread.current)
        for  i in 0 ..< 3 {
            print("aa = ",i)
        }
    }
    
    serailQueue.sync {
        print(Thread.current)
        for  i in 0 ..< 3 {
            print("bb = ",i) 
        }
    }
    
    serailQueue.sync {
        print(Thread.current)
        for  i in 0 ..< 3 {
            print("cc = ",i)
        }
    }
    print("end1")
}

// <NSThread: 0x600000071d80>{number = 1, name = main}
// aa =  0
// aa =  1
// aa =  2
// <NSThread: 0x600000071d80>{number = 1, name = main}
// bb =  0
// bb =  1
// bb =  2
// <NSThread: 0x600000071d80>{number = 1, name = main}
// cc =  0
// cc =  1
// cc =  2
// end1

分析:串行,顺序执行。同步,不会开启新的线程。

2.串行异步

func serialWithAsync() {
    serailQueue.async {
        print(Thread.current)
        for i in 0 ..< 3 {
            print("aa = ",i)
        }
    }
    serailQueue.async {
        print(Thread.current)
        for  i in 0 ..< 3 {
            print("bb = ",i)
        }
    }
    
    serailQueue.async {
        print(Thread.current)
        for  i in 0 ..< 3 {
            print("cc = ",i)
        }
    }
    print("end2,",Thread.current)
}
// <NSThread: 0x608000261c00>{number = 3, name = (null)}
// aa =  0
// aa =  1
// end2, <NSThread: 0x608000074f40>{number = 1, name = main}
// aa =  2
// <NSThread: 0x608000261c00>{number = 3, name = (null)}
// bb =  0
// bb =  1
// bb =  2
// <NSThread: 0x608000261c00>{number = 3, name = (null)}
// cc =  0
// cc =  1
// cc =  2

分析:串行队列中的任务按顺序执行,然异步任务会开启新的线程,所以三个任务是在新的线程中依次执行。且异步任务不会阻塞当前线程(main),所以结束标志语句在主线程中不受阻塞执行。

3.并行同步

func concurrentWithSync() {
    concurrentQueue.sync {
        print(Thread.current)
        for i in 0 ..< 3 {
            print("aa = ",i)
        }
    }
    concurrentQueue.sync {
        print(Thread.current)
        for i in 0 ..< 3 {
            print("bb = ",i)
        }
    }
    
    concurrentQueue.sync {
        print(Thread.current)
        for i in 0 ..< 3 {
            print("cc = ",i)
        }
    }
    print("end3")
}

// <NSThread: 0x608000071e80>{number = 1, name = main}
// aa =  0
// aa =  1
// aa =  2
// <NSThread: 0x608000071e80>{number = 1, name = main}
// bb =  0
// bb =  1
// bb =  2
// <NSThread: 0x608000071e80>{number = 1, name = main}
// cc =  0
// cc =  1
// cc =  2
// end3

分析:并行,任务会并发执行。同步,不会开启新的线程,且会阻塞当前线程。此时虽然用的是并行队列,但同步任务并没有开启新的线程,所以并行执行也就无从说起,就会在当前线程按顺序完成任务。

4.并行异步

func concurrentWithAsync() {
    concurrentQueue.async {
        print(Thread.current)
        for i in 0 ..< 3 {
            print("aa = ",i)
        }
    }
    concurrentQueue.async {
        print(Thread.current)
        for i in 0 ..< 3 {
            print("bb = ",i)
        }
    }
    concurrentQueue.async {
        print(Thread.current)
        for i in 0 ..< 3 {
            print("cc = ",i)
        }
    }
    print("end4,",Thread.current)
}
// aa, <NSThread: 0x600000263380>{number = 3, name = (null)}
// end4, <NSThread: 0x600000077700>{number = 1, name = main}
// bb, <NSThread: 0x600000263800>{number = 4, name = (null)}
// aa =  0
// cc, <NSThread: 0x6000002636c0>{number = 5, name = (null)}
// bb =  0
// aa =  1
// cc =  0
// aa =  2
// cc =  1
// bb =  1
// cc =  2
// bb =  2

分析:异步任务负责开启新的线程,并发队列负责在新的线程中多任务执行。

类似的学习demo,建议使用playground,很是方便。且能顺便学习下swift,哈。

相关文章

  • GCD的学习笔记(One)

    并行和并发 GCD简介 GCD的任务 GCD的队列 GCD创建队列或获取队列的方法 任务的执行方式:同步执行(同步...

  • iOS多线程--GCD篇

    GCD 文章目录GCD简介任务和队列GCD的使用步骤队列的创建方法任务的创建方法GCD的基本使用并行队列 + 同步...

  • 06进阶之路-多线程管理

    1. GCD相关 学习链接 GCD 简介 (多核编程管理线程) GCD 任务和队列(同步和异步任务 并发和串行队列...

  • GCD的队列和任务理解

    任务和队列 GCD 的两个核心:任务和队列。 任务:要执行的操作或方法函数。 队列:存放任务的集合,而我们要做的就...

  • ios GCD

    1.GCD 任务和队列学习 GCD 之前,先来了解 GCD 中两个核心概念:任务和队列。任务:就是执行操作的意思,...

  • iOS多线程--彻底学会多线程之『GCD』

    GCD 文章目录 GCD简介 任务和队列 GCD的使用步骤 队列的创建方法 任务的创建方法 GCD的基本使用 并行...

  • 多线程【转】

    文章目录GCD简介任务和队列GCD的使用步骤队列的创建方法任务的创建方法GCD的基本使用并行队列 + 同步执行并行...

  • NSOperation相关

    GCD和NSOperation比较 GCD :任务+队列步骤:创建队列--->使用函数将封装的任务添加到队列中 N...

  • 一、GCD 串行并行、同步异步

    GCD 一、 两个核心概念:任务和队列 任务:执行什么操作 队列:用来存放任务 将任务添加到队列中,GCD会自动...

  • 多线程

    iOS中的几种多线程GCD1、GCD分为任务和队列,任务(同步,异步)队列(串行,并发),同步串行,同步主队列的情...

网友评论

    本文标题:GCD的队列和任务理解

    本文链接:https://www.haomeiwen.com/subject/fyhsxxtx.html