Slide 1

Slide 1 text

Brief history of concurrency Dmytro Danylyk Atlassian

Slide 2

Slide 2 text

At the very beginning... - computers did not have operating systems - computers were designed to execute a single program from beginning to end - each program had access to all of the machine’s resources

Slide 3

Slide 3 text

Over time... Operating systems evolved to allow multiple programs to execute at once, each within a process — an independently isolated program that is assigned resources like memory, file handles and security controls.

Slide 4

Slide 4 text

Single-threaded process fun main(args: Array) { val a = 1 val b = 2 println(a + b) }

Slide 5

Slide 5 text

Over time... The same factors which motivated the development of processes, motivated the development of concurrency.

Slide 6

Slide 6 text

What is concurrency? In computer science, concurrency is the ability of different parts or units of a program to be executed out-of-order or in partial order, without affecting the final outcome. Concurrency is achieved via multithreading. iconst_0 ... iload_2 ... iadd ... iconst_5 ... ... goto 4 ... iinc

Slide 7

Slide 7 text

What is multithreading? When multiple threads execute bytecode instruction sequences in the same program, that action is known as multithreading. iconst_0 ... iload_2 ... iadd ... iconst_5 ... ... goto 4 ... iinc Thread 2 Thread 1

Slide 8

Slide 8 text

Why do we need multithreading? - Resource sharing - Responsiveness - Utilization of Multiprocessor Architecture

Slide 9

Slide 9 text

Why do we need multithreading? - Resource sharing - Responsiveness - Utilization of Multiprocessor Architecture

Slide 10

Slide 10 text

Why do we need multithreading? - Resource sharing - Responsiveness - Utilization of Multiprocessor Architecture

Slide 11

Slide 11 text

How multithreading is achieved? In single-processor systems, only a single thread of execution occurs at a given instant. The CPU quickly switches back and forth between several threads to create the illusion that the threads are executing at the same time. CPU I Thread 1 Thread 2 Thread 1 Thread 2 context switching

Slide 12

Slide 12 text

How multithreading is achieved? On multiprocessor systems, several threads do, in fact, execute at the same time. CPU I Thread 1 CPU II Thread 2 CPU III Thread 3 parallelism

Slide 13

Slide 13 text

What is a “thread”? A thread is an execution context, which is all the information CPU needs to execute an independent sequence of instructions. Executes as an independent sequence of instructions. val runnable = Runnable { val a = 1 val b = 2 println(a + b) } Thread(runnable).start() Java 1.1.4

Slide 14

Slide 14 text

What is “main” and “UI” thread? - Thread which is executed when your program starts is called main thread - Thread which is used to work with GUI is called UI thread - On Android main and UI thread are the same thing

Slide 15

Slide 15 text

What is “main” and “UI” thread? - Thread which is executed when your program starts is called main thread - Thread which is used to work with GUI is called UI thread - On Android main and UI thread are the same thing

Slide 16

Slide 16 text

What is “main” and “UI” thread? - Thread which is executed when your program starts is called main thread - Thread which is used to work with GUI is called UI thread - On Android main and UI thread are the same thing

Slide 17

Slide 17 text

What is “main” and “UI” thread?

Slide 18

Slide 18 text

Why there is only one “UI” thread? - Creating a thread for every UI update is expensive - By design, Android View objects are not thread-safe

Slide 19

Slide 19 text

Why there is only one “UI” thread? - Creating a thread for every UI update is expensive - By design, Android View objects are not thread-safe

Slide 20

Slide 20 text

Android “main” thread Looper Message Queue Message 2 Message 1 Message 3 Message 4 Message 2 Message 1 Handler 1 Handler 2 for(;;) Thread 2 Thread 1 Main Thread

Slide 21

Slide 21 text

The issues with threads - java threads are one-time use only - creating a thread takes time and resources - creating too many threads at the same time can cause the system to run out of memory

Slide 22

Slide 22 text

The issues with threads - java threads are one-time use only - creating a thread takes time and resources - creating too many threads at the same time can cause the system to run out of memory

Slide 23

Slide 23 text

The issues with threads - java threads are one-time use only - creating a thread takes time and resources - creating too many threads at the same time can cause the system to run out of memory

Slide 24

Slide 24 text

Thread Pools A thread pool reuses previously created threads to execute current tasks. Task Queue Task 2 Task 1 Task 3 Task 4 Thread 2 Thread 1 Task 2 Task 1 Thread 3 Thread 4 running running idle idle Java 1.5

Slide 25

Slide 25 text

Example of a thread pool val executor: ExecutorService = Executors.newFixedThreadPool(2) val task1 = Runnable { println("Task 1") } executor.execute(task1) val task2 = Runnable { println("Task 2") } executor.execute(task2) Executes as an independent sequence of instructions.

Slide 26

Slide 26 text

Types of thread pools. - Fixed Thread Pool - Cached Thread Pool (unbound, thread has 60 seconds lifetime) - Scheduled Thread Pool - ForkJoinPool (Java 7, used in Coroutines, all threads in the pool attempt to find and execute subtasks created by other active tasks)

Slide 27

Slide 27 text

Types of thread pools. - Fixed Thread Pool - Cached Thread Pool (unbound, thread has 60 seconds lifetime) - Scheduled Thread Pool - ForkJoinPool (Java 7, used in Coroutines, all threads in the pool attempt to find and execute subtasks created by other active tasks)

Slide 28

Slide 28 text

Types of thread pools. - Fixed Thread Pool - Cached Thread Pool (unbound, thread has 60 seconds lifetime) - Scheduled Thread Pool - ForkJoinPool (Java 7, used in Coroutines, all threads in the pool attempt to find and execute subtasks created by other active tasks)

Slide 29

Slide 29 text

Types of thread pools. - Fixed Thread Pool - Cached Thread Pool (unbound, thread has 60 seconds lifetime) - Scheduled Thread Pool - ForkJoinPool (Java 7, used in Coroutines, all threads in the pool attempt to find and execute subtasks created by other active tasks)

Slide 30

Slide 30 text

Coroutines A coroutine — is an instance of suspendable computation not bound to any particular thread. It may suspend its execution in one thread and resume in another one. Process→Thread (lightweight process)→Coroutine (lightweight thread)

Slide 31

Slide 31 text

I/O-bound and CPU-bound operations I/O operations - reading or writing to a file, database, or network - involves a lot of waiting for other hardware systems to complete their work - the upper bound for thread pool is fairly large (64 threads or unbound) CPU-intensive operations - compressing or decompressing, encryption or decryption in-memory data, image transformation - CPU won’t pause the execution as it does for an I/O operation - the upper bound for thread pool is to the number of cores the CPU has (and thus the number of threads it can run in parallel)

Slide 32

Slide 32 text

I/O-bound and CPU-bound operations I/O operations - reading or writing to a file, database, or network - involves a lot of waiting for other hardware systems to complete their work - the upper bound for thread pool is fairly large (64 threads or unbound) CPU-intensive operations - compressing or decompressing, encryption or decryption in-memory data, image transformation - CPU won’t pause the execution as it does for an I/O operation - the upper bound for thread pool is to the number of cores the CPU has (and thus the number of threads it can run in parallel)

Slide 33

Slide 33 text

I/O-bound and CPU-bound operations I/O operations - reading or writing to a file, database, or network - involves a lot of waiting for other hardware systems to complete their work - the upper bound for thread pool is fairly large (64 threads or unbound) CPU-intensive operations - compressing or decompressing, encryption or decryption in-memory data, image transformation - CPU won’t pause the execution as it does for an I/O operation - the upper bound for thread pool is to the number of cores the CPU has (and thus the number of threads it can run in parallel)

Slide 34

Slide 34 text

Remember It doesn’t matter what you use for concurrency, under the hood it’s still treads. Thread Thread Pools Loaders RxJava AsyncTask Coroutines Intent Service Job Scheduler

Slide 35

Slide 35 text

@dmytrodanylyk slides.com/dmytrodanylyk medium.com/@dmytrodanylyk S speakerdeck.com/dmytrodanylyk S