This workshop will be retired on May 31, 2020.
What Is Concurrency?4:45 with Amit Bijlani
Concurrency is dividing up the execution paths of your program so that they are possibly running at the same time. Learn about the common terminology: process, thread, multithreading, and others.
Process An instance of an executing app
Thread Path of execution for code
Multithreading Multiple threads or multiple paths of execution running at the same time.
Concurrency Execute multiple tasks at the same time in a scalable manner.
Queues Queues are lightweight data structures that manage objects in the order of First-in, First-out (FIFO).
Synchronous vs Asynchronous tasks
Synchronous: waits until the task has completed Asynchronous: completes a task in background and can notify you when complete
[MUSIC] 0:00 Hi, I'm Amit, an Iris teacher and developer. 0:04 Today we're gonna talk about a very scary topic called concurrency. 0:08 My goal is that at the end of this workshop, you're no longer afraid, and 0:12 can fearlessly implement it in your projects. 0:15 So what is concurrency? 0:18 It is dividing up the execution paths of your program, so 0:21 that they are possibly running at the same time. 0:24 For example, your app responds to touch events to make the UI scroll, and 0:27 at the same time, it fetches data from the network. 0:31 To the end user, it feels seamless, 0:34 as though everything was running at the same time, but in theory, it is not. 0:36 The amount of work that can be performed by a device like a phone or 0:42 a laptop is dependent on the processing power of the CPU. 0:46 In the early days of computing the CPU, or the brains of the computer, 0:50 were not powerful, so the computer was only capable of doing one task at a time. 0:55 As the technology advanced manufacturers increased the performance 1:01 of the CPU's by increasing the number of processor cores on each chip. 1:05 Increasing the number of cores meant a single chip could execute 1:11 more instructions per second. 1:16 Multiple cores meant the ability to do multiple things at the same time. 1:18 Modern operating systems like iOS and 1:24 OS 10 take advantage of the multiple cores by creating multiple threads. 1:26 So what's a process? 1:32 An instance of an executing app. 1:33 For example, your notes app, your clock app, your mail app, and 1:36 the app within your device. 1:40 So what is a thread? 1:42 A thread is a path of execution for your code. 1:45 Multiple threads or 1:49 multiple paths of execution running at the same time is known as multithreading. 1:50 A process contains multiple threads or multiple tasks running at the same time. 1:56 Threading is complicated, because as a developer you need to figure out 2:02 how to take advantage of all the cores and memory effectively so 2:06 that your application runs efficiently. 2:10 Concurrency means executing multiple tasks at the same time in a scalable manner. 2:13 Thankfully iOS and OS 10 have a better solution to execute multiple tasks 2:19 at the same time which is called concurrency. 2:24 Apple has created high level APIs that abstract away 2:27 all the nuances of multithreading programming. 2:30 They help us concurrently run tasks within our application. 2:34 Behind the scenes these APIs manage thread pools needed to run the tasks. 2:37 Furthermore to simplify multithreading, 2:43 Apple has moved away from the notion of threads giving us queues. 2:45 Queues are lightweight data structures that manage 2:50 objects in the order of first-in, first-out. 2:53 The main queue is for user interface related tasks like drawing responding to 2:57 touch of events, which should be done on the main queue. 3:01 While things like parsing JSON, fetching data from the network, or 3:05 any other CPU intensive task are carried on in the background queue. 3:09 Now we'll come back to these two types of queues. 3:14 We also need to understand the distinction between synchronous and 3:16 asynchronous tasks. 3:20 A synchronous task waits until the task has completed. 3:23 While an asynchronous task completes in the background and 3:28 can notify you when complete. 3:32 Now think of this scenario. 3:35 You need to purchase a movie ticket. 3:36 There are two ways you can go about it. 3:39 You can drive to the theater, 3:41 stand in line at the box office, and purchase a ticket. 3:42 This is a synchronous task, because you're preventing yourself from doing other tasks 3:46 until you have purchased the movie ticket. 3:51 Or you can purchase it online and have them email you the ticket. 3:55 This is an asynchronous task, because you have purchased the ticket and 3:59 in the background the theater has to figure out the logistics 4:03 to send you the ticket via email. 4:07 This is an asynchronous task, because you have made the request and 4:11 you're going about your business. 4:15 The task is completed in the background, and you're notified when complete. 4:17 Now if this is your first time being exposed to these concepts, 4:21 then all of this stuff is foreign to you. 4:25 We can go on and talk about several other concepts related to concurrency, but 4:28 it won't make any sense until you have some context. 4:32 Analogies and theories aside, let's take a look at a concrete example and 4:37 then dive a little deeper into some of the concepts. 4:42
You need to sign up for Treehouse in order to download course files.Sign up