GCD Swift 用法

作者: hwhjxjs | 来源:发表于2019-03-29 15:28 被阅读0次

参考https://blog.csdn.net/Hello_Hwc/article/details/54293280

进程/线程

进程:进程指在系统中能独立运行并作为资源分配的基本单位,它是由一组机器指令、数据和堆栈等组成的,是一个能独立运行的活动实体。
线程:线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行。
操作系统引入进程的目的:为了使多个程序能并发执行,以提高资源的利用率和系统的吞吐量。
操作系统引入线程的目的:在操作系统中再引入线程,则是为了减少程序在并发执行时所付出的时空开销,使OS具有更好的并发性。多线程技术可以提高程序的执行效率。

1. Gcd的概念

Grand Central Dispatch(GCD) 是 Apple 开发的一个多核编程的较新的解决方法。它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统。它是一个在线程池模式的基础上执行的并发任务。在 Mac OS X 10.6 雪豹中首次推出,也可在 iOS 4 及以上版本使用

为什么要用 GCD 呢?

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

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

2. GCD 任务和队列

学习 GCD 之前,先来了解 GCD 中两个核心概念:任务和队列。
任务:就是执行操作的意思,换句话说就是你在线程中执行的那段代码。在 GCD 中是放在 block 中的。执行任务有两种方式:同步执行(sync)和异步执行(async)。两者的主要区别是:是否等待队列的任务执行结束,以及是否具备开启新线程的能力。

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

注意: 异步执行(async) 虽然具有开启新线程的能力,但是并不一定开启新线程。这跟任务所指定的队列类型有关(下面会讲)。

队列(Dispatch Queue):这里的队列指执行任务的等待队列,即用来存放任务的队列。队列是一种特殊的线性表,采用 FIFO(先进先出)的原则,即新任务总是被插入到队列的末尾,而读取任务的时候总是从队列的头部开始读取。每读取一个任务,则从队列中释放一个任务。队列的结构可参考下图:

队列

在 GCD 中有两种队列:串行队列和并发队列。两者都符合 FIFO(先进先出)的原则。两者的主要区别是:执行顺序不同,以及开启线程数不同。

  • 串行队列(Serial Dispatch Queue)
    1.每次只有一个任务被执行。让任务一个接着一个地执行。(只开启一个线程,一个任务执行完毕后,再执行下一个任务)
  • 并发队列(Concurrent Dispatch Queue
    1.可以让多个任务并发(同时)执行。(可以开启多个线程,并且同时执行任务)

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

两者具体区别如下两图所示。



3. GCD 的使用步骤

3.1 GCD 的使用步骤其实很简单,只有两步。

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

let queue = DispatchQueue(label: "hwhQuene1")  //表示串行队列
let queue = DispatchQueue(label: "hwhQuene1",  attributes: .concurrent)  //并行队列
queue.async {
   print("线程1运行")
}
queue.sync {
   print("线程2运行")
}
  • 对于串行队列,GCD 提供了的一种特殊的串行队列:主队列(Main Dispatch Queue)。
    1.所有放在主队列中的任务,都会放到主线程中执行。
    2.可使用DispatchQueue.main获得主队列。
let mainQuene = DispatchQueue.main
  • 对于并发队列,GCD 默认提供了全局并发队列(Global Dispatch Queue)。
    1.可以使用DispatchQueue.global()来获取
let globalQuene = DispatchQueue.global()

3.2 任务的创建方法

GCD 提供了同步执行任务的创建方法sync和异步执行任务创建方法async。

queue.async {
   print("线程1运行")
}
queue.sync {
   print("线程2运行")
}

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

1.同步执行 + 并发队列
2.异步执行 + 并发队列
3.同步执行 + 串行队列
4.异步执行 + 串行队列

并发串发

4. GCD 的基本使用

先来讲讲并发队列的两种执行方式。

4.1 同步执行 + 并发队列

  • 在当前线程中执行任务,不会开启新线程,执行完一个任务,再执行下一个任务。
    func syncConcurrent()  {
        
        print("syncConcurrent---%@",Thread.current)
        print("syncConcurrent--begin")
        
        let queue = DispatchQueue(label: "hwhQuene1",  attributes: .concurrent)
        queue.sync {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程1运行---%@",Thread.current)
            }
            
        }
        
        queue.sync {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程2运行---%@",Thread.current)
            }
            
        }
        
        queue.sync {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程3运行---%@",Thread.current)
            }
            
        }
        
         print("syncConcurrent--end")
    }

输出结果
syncConcurrent---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
syncConcurrent--begin
线程1运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
线程1运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
线程1运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
线程2运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
线程2运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
线程2运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
线程3运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
线程3运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
线程3运行---%@ <NSThread: 0x281b9ae40>{number = 1, name = main}
syncConcurrent--end

从同步执行 + 并发队列中可看到:

  • 所有任务都是在当前线程(主线程)中执行的,没有开启新的线程(同步执行不具备开启新线程的能力)。
  • 所有任务都在打印的syncConcurrent---begin和syncConcurrent---end之间执行的(同步任务需要等待队列的任务执行结束)。
  • 任务按顺序执行的。按顺序执行的原因:虽然并发队列可以开启多个线程,并且同时执行多个任务。但是因为本身不能创建新线程,只有当前线程这一个线程(同步任务不具备开启新线程的能力),所以也就不存在并发。而且当前线程只有等待当前队列中正在执行的任务执行完毕之后,才能继续接着执行下面的操作(同步任务需要等待队列的任务执行结束)。所以任务只能一个接一个按顺序执行,不能同时被执行。

4.2 异步执行 + 并发队列

可以开启多个线程,任务交替(同时)执行。

    func syncConcurrent()  {
        
        print("syncConcurrent---%@",Thread.current)
        print("syncConcurrent--begin")
        
        let queue = DispatchQueue(label: "hwhQuene1",  attributes: .concurrent)
        queue.async {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程1运行---%@",Thread.current)
            }
            
        }
        
        queue.async {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程2运行---%@",Thread.current)
            }
            
        }
        
        queue.async {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程3运行---%@",Thread.current)
            }
            
        }
        
         print("syncConcurrent--end")
    }

syncConcurrent---%@ <NSThread: 0x28386ee40>{number = 1, name = main}
syncConcurrent--begin
syncConcurrent--end
线程2运行---%@ <NSThread: 0x283803d00>{number = 4, name = (null)}
线程1运行---%@ <NSThread: 0x283835200>{number = 3, name = (null)}
线程3运行---%@ <NSThread: 0x283837540>{number = 5, name = (null)}
线程2运行---%@ <NSThread: 0x283803d00>{number = 4, name = (null)}
线程1运行---%@ <NSThread: 0x283835200>{number = 3, name = (null)}
线程3运行---%@ <NSThread: 0x283837540>{number = 5, name = (null)}
线程2运行---%@ <NSThread: 0x283803d00>{number = 4, name = (null)}
线程1运行---%@ <NSThread: 0x283835200>{number = 3, name = (null)}
线程3运行---%@ <NSThread: 0x283837540>{number = 5, name = (null)}

在异步执行 + 并发队列中可以看出:

  • 除了当前线程(主线程),系统又开启了3个线程,并且任务是交替/同时执行的。(异步执行具备开启新线程的能力。且并发队列可开启多个线程,同时执行多个任务)。
  • 所有任务是在打印的syncConcurrent---begin和syncConcurrent---end之后才执行的。说明当前线程没有等待,而是直接开启了新线程,在新线程中执行任务(异步执行不做等待,可以继续执行任务)。

接下来再来讲讲串行队列的两种执行方式。

4.3 同步执行 + 串行队列

  • 不会开启新线程,在当前线程执行任务。任务是串行的,执行完一个任务,再执行下一个任务。
func syncSerial()  {
        
        print("syncConcurrent---%@",Thread.current)
        print("syncConcurrent--begin")
        
        let queue = DispatchQueue(label: "hwhQuene1")
        queue.sync {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程1运行---%@",Thread.current)
            }
            
        }
        
        queue.sync {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程2运行---%@",Thread.current)
            }
            
        }
        
        queue.sync {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程3运行---%@",Thread.current)
            }
            
        }
        
        print("syncConcurrent--end")
    }

syncConcurrent---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
syncConcurrent--begin
线程1运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
线程1运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
线程1运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
线程2运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
线程2运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
线程2运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
线程3运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
线程3运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
线程3运行---%@ <NSThread: 0x28323ee00>{number = 1, name = main}
syncConcurrent--end

在同步执行 + 串行队列可以看到:

  • 所有任务都是在当前线程(主线程)中执行的,并没有开启新的线程(同步执行不具备开启新线程的能力)。
  • 所有任务都在打印的syncConcurrent---begin和syncConcurrent---end之间执行(同步任务需要等待队列的任务执行结束。
  • 任务是按顺序执行的(串行队列每次只有一个任务被执行,任务一个接一个按顺序执行)。

4.4 异步执行 + 串行队列

  • 会开启新线程,但是因为任务是串行的,执行完一个任务,再执行下一个任务
 func asyncSerial()  {
        
        print("syncConcurrent---%@",Thread.current)
        print("syncConcurrent--begin")
        
        let queue = DispatchQueue(label: "hwhQuene1")
        queue.async {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程1运行---%@",Thread.current)
            }
            
        }
        
        queue.async {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程2运行---%@",Thread.current)
            }
            
        }
        
        queue.async {
            for _ in 0...2{
                Thread.sleep(forTimeInterval: 2)
                print("线程3运行---%@",Thread.current)
            }
            
        }
        
        print("syncConcurrent--end")
    }

syncConcurrent---%@ <NSThread: 0x282032e40>{number = 1, name = main}
syncConcurrent--begin
syncConcurrent--end
线程1运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}
线程1运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}
线程1运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}
线程2运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}
线程2运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}
线程2运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}
线程3运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}
线程3运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}
线程3运行---%@ <NSThread: 0x282073e80>{number = 4, name = (null)}

在异步执行 + 串行队列可以看到:

  • 开启了一条新线程(异步执行具备开启新线程的能力,串行队列只开启一个线程)。
  • 所有任务是在打印的syncConcurrent---begin和syncConcurrent---end之后才开始执行的(异步执行不会做任何等待,可以继续执行任务)。
  • 任务是按顺序执行的(串行队列每次只有一个任务被执行,任务一个接一个按顺序执行)。

相关文章

  • GCD

    iOS多线程 Swift4 GCD深入解析swift GCD 的一些高级用法GCD 之线程组(Dispatch G...

  • Swift GCD用法

  • GCD Swift 用法

    参考https://blog.csdn.net/Hello_Hwc/article/details/5429328...

  • GCD-Swift 3 常见用法

    Swift 是完美支持GCD的,由于Swift的发展写法也一直在变动,下面列举一些常见的用法。 全局队列异步执行 ...

  • Swift-GCD

    对原Swift-GCD升级 SwiftGCD Swift-GCD Package GCDGroup, GCDQue...

  • Swift 3 - GCD高级用法

    GCD是Apple开发的一个多核编程的解决方法,基本概念就是dispatch queue(调度队列),queue是...

  • Swift GCD的进阶用法

    1、信号量(Semaphore) 定义:信号量就是一个资源计数器,就是一种可用来控制访问资源数量的标识。 通过添加...

  • swift GCD-定时器(DispatchSourceTime

    前言: 了解swift GCD 与OC GCD请看我的其他总结文章,这篇文章只是对swift GCD定时器的一个实...

  • swift中GCD的使用详情

    想看swift3.0使用GCD,请点击GCD详解 想看swift3.0闭包的使用和介绍,请点击Swift版闭包使用...

  • Swift 3必看:从使用场景了解GCD新API

    Swift 3必看:从使用场景了解GCD新API Swift 3必看:从使用场景了解GCD新API

网友评论

    本文标题:GCD Swift 用法

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