中央调度器与监听器_为什么您应该关心中央中央调度

中央调度器与监听器

If you’re an iOS developer, you have likely used Grand Central Dispatch (GCD) at some point. Maybe you’ve read Apple’s documentation which states that GCD “execute[s] code concurrently on multicore hardware by submitting work to dispatch queues managed by the system.” But what in the heck does that actually mean? Well, in order to understand GCD, we have to first understand the problem that it solves.

如果您是iOS开发人员,则可能在某个时候使用了Grand Central Dispatch(GCD)。 也许您已经阅读了Apple的文档,其中指出GCD“通过提交工作以分派系统管理的队列,在多核硬件上同时执行代码”。 但这到底意味着什么呢? 好了,为了了解GCD,我们必须首先了解它解决的问题。

iOS中的多线程 (Multithreading in iOS)

What is a thread? The best description I found in researching this article came from pwnall on Stack Overflow. Here it is in full:

什么是线程? 我在研究本文时发现的最好描述来自Stack Overflow上的pwnall 。 这是完整的:

“A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.

“线程是执行上下文,它是CPU执行指令流所需的所有信息。

Suppose you’re reading a book, and you want to take a break right now, but you want to be able to come back and resume reading from the exact point where you stopped. One way to achieve that is by jotting down the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.

假设您正在读书,现在想休息一下,但是您希望能够回来并从停下的确切位置继续阅读。 实现此目的的一种方法是记下页码,行号和单词号。 因此,您阅读本书的执行上下文是这3个数字。

If you have a roommate, and she’s using the same technique, she can take the book while you’re not using it, and resume reading from where she stopped. Then you can take it back, and resume it from where you were.

如果您有一个室友,并且她正在使用相同的技术,那么您可以在不使用它时拿起这本书,然后从停下来的地方继续阅读。 然后,您可以将其取回,并从您所在的位置恢复。

Threads work in the same way. A CPU is giving you the illusion that it’s doing multiple computations at the same time. It does that by spending a bit of time on each computation. It can do that because it has an execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.

线程以相同的方式工作。 CPU给您一种错觉,即它同时进行多个计算。 它通过在每次计算上花费一些时间来做到这一点。 之所以能够做到这一点,是因为它具有每次计算的执行上下文。 就像您可以与朋友共享一本书一样,许多任务可以共享一个CPU。

On a more technical level, an execution context (therefore a thread) consists of the values of the CPU’s registers.

从技术上讲,执行上下文(因此是线程)由CPU寄存器的值组成。

Last: threads are different from processes. A thread is a context of execution, while a process is a bunch of resources associated with a computation. A process can have one or many threads.”

最后:线程与进程不同。 线程是执行的上下文,而进程是与计算相关联的一堆资源。 一个进程可以有一个或多个线程。”

In iOS, processes and applications are executed on multiple threads. This could be the result of multiple cores or a single processor performing context switches (running one thread and then switching to run another thread). Having multiple threads, or multithreading, gives the processor access to many threads at the same time so that it can execute tasks concurrently.

在iOS中,进程和应用程序在多个线程上执行。 这可能是由于多个内核或单个处理器执行上下文切换(运行一个线程,然后切换为运行另一个线程)的结果。 具有多个线程或多线程使处理器可以同时访问多个线程,以便它可以并行执行任务。

Note: It is tempting to say that concurrent tasks execute at the same time, but it’s important to keep the real definition in mind:

注意:很容易说并发任务是同时执行的,但是牢记真正的定义很重要:

“Two events are concurrent if we cannot tell by looking at the program which will happen first.” (Downey, 2005)

“如果我们不能通过先看哪个程序来判断,那么两个事件是并发的。” (唐尼,2005年)

Importantly, it is up to the operating system scheduler, not the programmer, to determine if, when, and how to switch to another thread. This raises some significant issues when we want to set constraints on the order or manner in which tasks are executed, so iOS uses GCD to help us create and enforce those constraints.

重要的是,由操作系统调度程序(而不是程序员)来确定是否何时以及如何切换到另一个线程。 当我们想对执行任务的顺序或方式设置约束时,这会引发一些重大问题,因此iOS使用GCD帮助我们创建和实施这些约束。

GCD如何运作? (How does GCD work?)

GCD is built on threads and manages a shared thread pool, a collection of threads on which tasks can be performed. When you add tasks to dispatch queues, GCD executes them on a thread of its choosing and, importantly, in FIFO (first in, first out) order. There are three main queues in GCD:

GCD建立在线程上,并管理共享线程池,共享线程池是可以在其上执行任务的线程的集合。 当您将任务添加到调度队列时,GCD在其选择的线程上执行它们,并且重要的是,按照FIFO(先进先出)的顺序执行任务。 GCD中有三个主要队列:

Main Queue: Serial queue that runs on the main thread

主队列 :在主线程上运行的串行队列

Global Queues: Concurrent queues shared by the entire system. These queues come with four different priority levels. From highest priority to lowest, they are high, default, low, and background.

全局队列 :整个系统共享的并发队列。 这些队列具有四个不同的优先级。 从最高优先级到最低优先级,它们分别是高,默认,低和背景。

Custom Queues: Can be serial or concurrent and will end up in one of the global queues based on the Quality of Service (or QoS) property, rather than directly specifying the priority.

自定义队列 :可以是串行队列,也可以是并发队列 ,它们将基于服务质量(或QoS)属性而不是直接指定优先级而最终位于全局队列之一中。

What does it mean for a queue to be serial or concurrent? Both guarantee that tasks start in the order that they were added. Serial queues can only run one task at a time, but you won’t know how much time passes between one task ending and the next beginning. The main queue is a serial queue, which means we can be sure that tasks in the main queue execute one at a time and in the same order that they were put in the queue. Concurrent queues can start one task and then also start the next task without the first one finishing. With a concurrent queue, we don’t have the ability to know the order in which tasks finish, the time in between them, even or the number of tasks running at any given time. And that’s okay! As programmers, we shouldn’t lend importance to those details. It’s our job to ensure that our code runs smoothly anyway. So what does that look like in practice?

队列是串行的还是并发的意味着什么? 两者都保证任务按添加顺序开始。 串行队列一次只能运行一个任务,但是您不知道在一个任务结束与下一个任务开始之间经过了多少时间。 主队列是一个串行队列,这意味着我们可以确保主队列中的任务一次执行一次,并且执行顺序与放入队列中的顺序相同。 并发队列可以启动一个任务,然后又可以启动下一个任务而无需完成第一个任务。 对于并发队列,我们​​无法知道任务完成的顺序,它们之间的时间,甚至是在任何给定时间运行的任务数量。 没关系! 作为程序员,我们不应该重视这些细节。 确保我们的代码无论如何都能平稳运行是我们的工作。 那么在实践中这是什么样的呢?

执行任务 (Executing Tasks)

Tasks can be executed either synchronously or asynchronously, which determines whether the function waits to return until the task is complete (synchronous), or returns immediately after being called regardless of the state of the task (asynchronous). They are submitted to a dispatch queue using DispatchQueue.sync(execute:) or DispatchQueue.async(execute:). For example:

任务可以同步执行,也可以异步执行,这确定函数是等待返回直到任务完成(同步),还是被调用后立即返回,而不管任务的状态如何(异步)。 使用DispatchQueue.sync(execute :)DispatchQueue.async(execute :)将它们提交到调度队列。 例如:

Significantly, GCD dispatch queues themselves are thread safe, which means you can add tasks to a dispatch queue from any thread. This small guarantee is actually saving us a ton of headaches. It would take crazy amounts of extra time and effort to manually lock and unlock each thread and synchronize access to each queue. (If you are interested in what it takes to manage thread execution and the problems that can arise, I highly recommend the Little Book of Semaphores.) The ability to harness control over the order and manner in which tasks are executed is huge and has the potential to significantly increase the efficiency and performance of your iOS applications. Thank you, GCD!

重要的是,GCD调度队列本身是线程安全的,这意味着您可以从任何线程将任务添加到调度队列中。 这个小小的保证实际上为我们节省了很多麻烦。 手动锁定和解锁每个线程以及同步对每个队列的访问将花费大量的时间和精力。 (如果您对管理线程执行需要什么以及可能出现的问题感兴趣,我强烈建议您阅读《信号量手册》 。)利用对执行任务的顺序和方式进行控制的能力非常强大,并且具有潜在地显着提高iOS应用程序的效率和性能。 谢谢,GCD!

被引作品/进一步阅读 (Works Cited / Further Reading)

Dekhayser, Evan. “Grand Central Dispatch Tutorial for Swift 4: Part 1/2.” Raywenderlich.com, 15 Aug. 2018, www.raywenderlich.com/5370-grand-central-dispatch-tutorial-for-swift-4-part-1–2#toc-anchor-005.

埃文·德哈伊瑟 “ Swift 4的大型中央调度教程:第1/2部分。” Raywenderlich.com ,2018年8月15日, www.raywenderlich.com/5370 - grand - central - dispatch - tutorial - for - swift -4-part- 1 –2#toc-anchor-005。

Dekhayser, Evan. “Grand Central Dispatch Tutorial for Swift 4: Part 2/2.” Raywenderlich.com, 15 Aug. 2018, www.raywenderlich.com/5371-grand-central-dispatch-tutorial-for-swift-4-part-2-2#toc-anchor-004.

埃文·德哈伊瑟(Dekhayser)。 “ Swift 4的大型中央调度教程:第2/2部分。” Raywenderlich.com ,2018年8月15日, www.raywenderlich.com / 5371-grand-central-dispatch-tutorial-for-swift-4-part-2-2#toc-anchor-004

Downey, Allen B. “The Little Book of Semaphores.” Green Tea Press, 2005, https://greenteapress.com/wp/semaphores.

唐尼(Allen B),唐尼(Downey),“信号量小书”。 绿茶出版社 ,2005年, https://greenteapress.com/wp/semaphores

Lewis, Gabriel. “Threading in Swift Simply Explained.” Medium, Better Programming, 2 July 2019, https://medium.com/better-programming/threading-in-swift-simply-explained-5c8dd680b9b2.

刘易斯,加百列。 “简单解释Swift中的线程。” 中等 ,更好的编程,2019年7月2日, https://medium.com/better-programming/threading-in-swift-simply-explained-5c8dd680b9b2

翻译自: https://medium.com/@anika.morris/why-you-should-care-about-grand-central-dispatch-4572f94a9fa1

中央调度器与监听器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值